静态数组
最近看到数据结构静态数组部分,自认为听懂了,但是一上手发现问题很多,搞了很久好像弄完了,故把代码贴上来,抛砖引玉,希望大家提出不足和改进#define MAXSIZE 1000
typedef struct component
{
int data;
int cur;
}component;
component StaticLinkList;
void InitList()
{
for(int i=0;i<MAXSIZE-1;i++)
{
StaticLinkList.cur = i+1;
}
StaticLinkList.cur = 0;
}
void Print()
{
printf("\n--------------------------\n");
for(int i=StaticLinkList.cur;i;i = StaticLinkList.cur)
{
printf("%d\t",StaticLinkList.data);
}
printf("\n-----------------------------\n");
}
void ListInsert(int cur,int data)
{
if(cur<1 || cur > MAXSIZE-2)
return;
if(StaticLinkList.cur==0)
{
int reserve = StaticLinkList.cur;
StaticLinkList.cur = StaticLinkList.cur;
StaticLinkList.data = data;
StaticLinkList.cur = 0;
StaticLinkList.cur = reserve;
}
else if(cur == 1)
{
int reserve = StaticLinkList.cur;
StaticLinkList.data = data;
StaticLinkList.cur = StaticLinkList.cur;
StaticLinkList.cur = StaticLinkList.cur;
StaticLinkList.cur = reserve;
}
else
{
int tmp = StaticLinkList.cur;
for(int i=1;i<cur-1 && StaticLinkList.cur;i++,tmp = StaticLinkList.cur);
int reserve = StaticLinkList.cur;
StaticLinkList.data = data;
StaticLinkList.cur = StaticLinkList.cur;
StaticLinkList.cur = StaticLinkList.cur;
StaticLinkList.cur = reserve;
}
}
void ListDelete(int cur)
{
if(cur < 1 || cur > MAXSIZE-2)
return;
int tmp = StaticLinkList.cur;
if(tmp == 0)
{
printf("\nempty\n\n");
}
if(cur == 1)
{
StaticLinkList.cur = StaticLinkList.cur;
StaticLinkList.cur = StaticLinkList.cur;
StaticLinkList.cur = tmp;
}
else
{
for(int i=1;i<cur-1 && StaticLinkList.cur;i++,tmp = StaticLinkList.cur);
if(tmp == 0)
{
printf("\n\n\n cur error!\n\n\n");
return;
}
int del = StaticLinkList.cur;
StaticLinkList.cur = StaticLinkList.cur;
StaticLinkList.cur = StaticLinkList.cur;
StaticLinkList.cur = del;
}
}
页:
[1]