线性表的插入与删除
希望大家能给我一个中文注释,来叙述这个程序,谢谢#include<stdio.h>
#define NULL 0
typedef int elemtype;
typedef struct node
{elemtype data;
struct node *next;
}linklist;
int n;
/*创建单链表*/
linklist *creat(void)
{
linklist *head,*p1,*p2;
n=0;
p1=p2=(linklist*)malloc(sizeof(linklist));
printf("qing shu ru lian biao nei rong:\n");
scanf("%d",&p1->data);
head=NULL;
while(p1->data!=0)
{n=n+1;
if(n==1)
head=p1;
else p2->next=p1;
p2=p1;
p1=(linklist*)malloc(sizeof(linklist));
scanf("%d",&p1->data);
}
p2->next=NULL;
return head;
}
/*单链表的插入*/
linklist *insert(linklist *head,linklist *stud)
{ linklist *p1,*p2,*p0;
p1=head;
p0=stud;
if(head==NULL)
{ head=p0;p0->next=NULL;}
else
{ while((p0->data>=p1->data)&&(p1->next!=NULL))
{ p2=p1;p1=p1->next; }
if(p0->data<p1->data)
{ if(head==p1) head =p0;
else p2->next=p0;
p0->next=p1;}
else
{ p1->next=p0;
p0->next=NULL;
}
}
n=n+1;
return(head);
}
/*单链表的删除*/
linklist *del(linklist *head,int num)
{
linklist *p1,*p2;
if(head==NULL)
{printf("\n list is NULL!");
return (head);
}
else
{
p1=head;
while((num!=p1->data)&&(p1->next!=NULL))
{
p2=p1;
p1=p1->next;
}
if(num==p1->data)
{
if(p1==head)
head=p1->next;
else
p2->next=p1->next;
free(p1);
printf("delete:%d\n",num);
n=n-1;
}
else
printf("%d not been found!\n",num);
}
return (head);
}
/*打印单链表*/
void display(linklist *p)
{linklist *q;
q=(linklist*)malloc(sizeof(linklist));
q=p;
printf("\n dan lian biao xian shi :");
if(q==NULL)
printf("lianbiao wei kong!");
else
while(q!=NULL)
{
printf("%d ",q->data);
q=q->next;
}
printf("\n");
}
/*实现单链表*/
main()
{int i,j;
linklist *q,*p;
p=q=(linklist*)malloc(sizeof(linklist));
q=creat();
display(q);
printf("qing shu ru yao cha ru de zhi:\n");
scanf("%d",&p->data);
q=insert(q,p);
display(q);
printf("qing shu ru yao shanchu de jie dian :");
scanf("%d",&j);
del(q,j);
display(q);
getch();
}