#include <stdio.h>
#include<iostream>
#include <vector>
#define max 100
using namespace std;
struct node {
int data;
int cur;
};
vector<node> list;
//初始化链表
void InitList(vector<node>&list)
{
list.resize(max);
list[max - 1].cur = 0;
for (int i = 0; i < max - 2; i++)
{
list[i].cur = i + 1;
}
list[max - 2].cur = 0;
}
//求静态链表中元素个数,不包括头尾节点
int ListLength(vector<node>list)
{
int i = list[max- 1].cur;
int j = 0;
while (i)
{
j++;
i = list[i].cur;
}
return j;
}
//插入元素时,分配空间的下标
int Malloc(vector<node>&list)
{
int i = list[0].cur;
if (i)
list[0].cur = list[i].cur;
return i;
}
//插入元素
bool ListInsert(vector<node>&list,int i,int key)
{
if (i<1 || i>ListLength(list) + 1)
return false;
int j = Malloc(list);
int k = max - 1;
if (j)
{
for (int l = i; l <= j-1; l++)
{
k = list[k].cur;
}
list[j].data = key;
list[j].cur = list[k].cur;
list[k].cur = j;
return true;
}
}
//删除第i个元素,key值改变
bool ListDelete(vector<node>&list, int i, int* key)
{
if (i < 1 || i >= ListLength(list))
return false;
int k = max - 1;
for (int l = 1; l <= i - 1; l++)
k = list[k].cur;
int j =list[k].cur;
*key = list[j].data;
list[k].cur = list[j].cur;
list[j].cur = list[0].cur;
list[0].cur = j;
return true;
}
//查找key
int ListSearch(vector<node>list, int key)
{
int i = list[max - 1].cur;
while (list[i].data != key)
i = list[i].cur;
return i;
}
//遍历
void ListPrint(vector<node>&list)
{
int k = max - 1;
while (list[k].cur)
{
k = list[k].cur;
cout<< (list[k]).data<<'\t';
}
printf("\n");
}
//测试
int main()
{
InitList(list);
ListInsert(list, 1, 1);
ListInsert(list, 1, 2);
ListInsert(list, 1, 3);
ListInsert(list, 1, 4);
ListInsert(list, 1, 5);
ListPrint(list);
cout << endl;
int key;
ListDelete(list,2,&key);
cout << "删除的元素值是" << key << endl;
ListPrint(list);
cout << endl;
int x;
x=ListSearch(list, 3);
if (x)
cout << "这是第" << x << "个元素" << endl;
else
cout << "Error" << endl;
getchar();
return 0;
}
最后一次更新于2019-06-19
0 条评论