WEB开发网
开发学院软件开发数据结构 链表的建立、插入和删除 阅读

链表的建立、插入和删除

 2007-05-03 11:41:50 来源:WEB开发网   
核心提示:3. 实例[例7 - 7 ]创建包含学号、姓名节点的单链表,其节点数任意个,链表的建立、插入和删除(3),表以学号为序,低学号的在前,要求删除一个给定姓名的节点,并插入一个给定学号和姓名的节点,高学号的在后,以输入姓名为空作结束

3. 实例[例7 - 7 ]
创建包含学号、姓名节点的单链表。其节点数任意个,表以学号为序,低学号的在前,高学号的在后,以输入姓名为空作结束。在此链表中,要求删除一个给定姓名的节点,并插入一个给定学号和姓名的节点。
# include "stdlib.h"
# include "malloc. h"
struct node /*节点的数据结构* /
{
int num;
char str[20];
struct node *next;
} ;
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
main( )
{
/ *函数声明* /
struct node *creat();
struct node *insert();
struct node *delet();
void print( );
struct node *head;
char str[20];
int n;
head=NULL; /*做空表* /
head=creat (head); / *调用函数创建以head 为头的链表* /
p r i n t ( h e a d ) ;/ *调用函数输出节点* /
printf("\n input inserted num,name:\n");
gets(str); /*输入学号* /
n=atoi (str);
gets(str); /*输入姓名* /
head=insert (head, str, n); 将/*节点插入链表*/
print (head); / *调用函数输出节点*/
printf("\n input deleted name:\n");
gets(str); /*输入被删姓名* /
head=delet(head,str); /调*用函数删除节点*/
print (head); /*调用函数输出节点* /
r e t u r n ;
}
/ * * * * * * * * * * * * * * * * * * * * * * /
/ * * * 创建链表* * * * * * * * * * * * /
struct node *creat(struct node *head)
{
char temp[30];
struct node *pl,*p2;
pl=p2=(struct node*) malloc(sizeof(struct node));
printf ("input num, name: \n;")
printf("exit:double times Enter!\n");
g e t s ( t e m p ) ;
gets (p1->str);
pl->num=atoi (temp);
p l - > n e x t = N U L L ;
while (strlen (pl->str)>0
{
if (head==NULL) head=pl;
else p2->next=p1;
P 2 = p l ;
pl=(struct node *)malloc(sizeof(struct node));
printf ("input num, name: \n");
printf("exit:double times Enter!\n");
g e t s ( t e m p ) ;
gets(pl ->str);
p1->num=atoi (temp);
P 1 - > n e x t = N U L L ;
}
return head;
}
/ * * * * * * * * * * * * * * * * * * * * /
/ * * * * * * * * * * 插入节点* * * * * * * * * * /
struct node *insert (head, pstr,n);
struct node *head;
char *pstr;
int n;
{
struct node *pl,*p2,*p3;
p1=(struct node*)malloc(sizeof(struct node));
strcpy (p1->str, pstr);
p 1 - > n u m = n ;
p 2 = h e a d ;
i f ( h e a d = = N U L L )
{
h e a d = p l ; p l - > n e x t = N U L L ;
}
e l s e
{
while (n>p2->num&&p2->next!=NULL)
{
p 3 = P 2
p 2 = p 2 - > n e x t ;
}
if (n<=p2->num)
if (head==p2)
{
h e a d = p l ;
p l - > n e x t = p 2 ;
}
else
{
p 3 - > n e x t = p l ;
p l - > n e x t = p 2 ;
}
else
{
p 2 - > n e x t = p l ;
p l - > n e x t = N U L L ;
}
}
r e t u r n ( h e a d ) ;
}
/ * * * * * * * * * * * * * * * * * * * * * * * * * /
/ * * * * * 删除节点* * * * * * * * * * * * * /
struct node *delet (head, pstr)
struct node *head;
char *pstr;
{
struct node *temp,*p;
t e m p = h e a d ;
if (head==NULL)
printf("\nList is null!\n");
else
{
t e m p = h e a d ;
while (strcmp(temp->str,pstr)!=O&&temp->next!=NULL)
{
p = t e m p ;
t e m p = t e m p - > n e x t ,
}
i f ( s t r c m p ( t e m p - > s t r , p s t r ) = = 0 )
{
if (temp== head)
{
h e a d = h e a d - > n e x t ;
f r e e ( t e m p ) ;
}
else
{
p->next =temp->next;
printf("delete string :%s\n",temp->str);
f r e e ( t e m p ) ;
}
}
else printf("\nno find string!\n");
}
return(head);
}
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
/ * * * * * * * * * * 链表各节点的输出* * * * * * * * * * /
void print (struct node *head)
{
struct node *temp;
t e m p = h e a d ;
printf("\n output strings:\n");
while (temp!=NULL)
{
p r i n t f ( " \ n % d - - - - % s \ n " , t e m p - > n u m ,t e m p - > s t r ) ;
t e m p = t e m p - > n e x t ;
}
r e t u r n ;
}

上一页  1 2 3 

Tags:建立 插入 删除

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接