注意:带头结点
储存:
typedef struct linklist
{
int data;
struct linklist *next;
}linklist;
插入实现:
void insert(linklist *L,int i,int e)
{
linklist *p=L;
int k=0;
while(p&&k!=i-1)
{
p=p->next;
k++;
}
if(p&&k==i-1)
{
linklist *s=(linklist *)malloc(sizeof(linklist));
s->data=e;
s->next=p->next;
p->next=s;
}
else
printf("error:the location of i is not leagel\n");
}
删除实现
void delete(linklist *L,int i,int *e)
{
linklist *p=L;
int k=0;
while(p&&k!=i-1)
{
p=p->next;
k++;
}
if(p&&p->next&&k==i-1)
{
linklist *q=p->next;
*e=q->data;
p->next=q->next;
free(q);
}
else
printf("error:the location of i is not leagel\n");
}
其他实现:
1.遍历
void traverse(linklist*L)
{
linklist *p=L->next;
if(!p)
{
printf("empty list\n");
return;
}
while(p)
{
printf("%d ",p->data);
p=p->next;
}
}
2.isempty
int isempty(linklist *L)
{
linklist *p=L->next;
if(!p)return 1;
else return 0;
}
3.initlist
linklist *Initlist(void)
{
linklist *L;
L=(linklist *)malloc(sizeof(linklist));
L->next=NULL;
return L;
}