数据结构之静态链表

#include
using namespace std;
#define MAXSIZE 1000
typedef struct DuLNode
{
char date;
int cur;
}component, SLinkList[MAXSIZE];
void InitSpace_SL(SLinkList&space)//将一维数组space中各分量连接成一个备用链表,space[0].cur为头指针
{
int i;
for (i = 0; i < MAXSIZE - 1; ++i)

space[i].cur = i + 1;
space[MAXSIZE - 1].cur = 0;

}
int MaLLoc_SL(SLinkList&space)//若备用空间链表非空,则返回分配的结点下标,否则返回0
{
int i;
i = space[0].cur;
if (space[0].cur)
space[0].cur = space[i].cur;
return i;
}
void free_SL(SLinkList&space, int k)//将下标为k的空闲结点回收到备用链表
{
space[k].cur = space[0].cur;
space[0].cur = k;
}
void difference(SLinkList&space, int &S)
{
int r;
InitSpace_SL(space);
S = MaLLoc_SL(space);
r = S;
int m, n;
cout << "请输入A和B元素的个数:" << endl;
cin >> m >> n;
int i, j;
cout << "请输入A的元素值:" << endl;
for (j = 1; j <= m; ++j)
{
i = MaLLoc_SL(space);
cin >> space[i].date;
space[r].cur = i;
r = i;
}
space[r].cur = 0;
int b, p, k;
cout << "请输入B的元素:" << endl;
for (j = 1; j <= n; ++j)
{

cin >> b;
p = S;
k = space[S].cur;
while (k != space[r].cur&&space[k].date != b)
{
p = k;
k = space[k].cur;
}
if (k == space[r].cur)
{
i = MaLLoc_SL(space);
space[i].date = b;
space[i].cur = space[r].cur;
space[r].cur = i;
}
else
{
space[p].cur = space[k].cur;
free_SL(space, k);
if (r == k) r = p;
}
}

}
void display(SLinkList &space,int &S)
{
int i;
i = S;
while (space[i-1].cur)

{
cout << space[i+1].date<<" ";
i++;

}
cout << endl;
}
int main()
{
SLinkList space;
InitSpace_SL(space);
int S;
difference(space, S);
display(space,S);
system("pause");
return 0;
}

你可能感兴趣的:(数据结构之静态链表)