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 0deleted 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 ) . }