C语言 实现链表的建立、输出,删除和插入,在主函数中指定需要删除和插入的结点的数据。

2016-06-20 09:29:18  分类: c程序设计第四版谭浩强课后答案  参与:

 综合本章例9.9(建立链表的函数creat)、例9.10(输出链表的函数print)和本章习题第7题(删除链表中结点的函数del )、第8题(插入结点的函数insert),再编写一个主函数,先后调用这些函数。用以上5个函数组成一个程序,实现链表的建立、输出,删除和插入,在主函数中指定需要删除和插入的结点的数据。

谭浩强c语言程序设计第四版答案

c源程序
#include <stdio.h>
#include <malloc.h>
#define NULL 0
#define LEN sizeof(struct student)
struct student
  {long num;
   float score;      struct student *next;
  };
int n;

int main()
  {struct student *creat();
   struct student *del(struct student * ,long  );
   struct student *insert(struct student *, struct student *);
   void print(struct student *);
   struct student *head,stu;
   long del_num;
   printf("input records:\n");
   head=creat();
   print(head);                    
   printf("input the deleted number:");
   scanf("%ld",&del_num);
   head=del(head,del_num);
   print(head);               
   printf("input the inserted record:");    
   scanf("%ld,%f",&stu.num,&stu.score);
   head=insert(head,&stu);
   print(head);
   printf("input the inserted record:");    
   scanf("%ld,%f",&stu.num,&stu.score);
   head=insert(head,&stu);
   print(head);
   return 0;
  }

struct student *creat()
  {struct student *head;
   struct student *p1,*p2;
   n=0;
   p1=p2=( struct student*) malloc(LEN);
   scanf("%ld,%f",&p1->num,&p1->score);
   head=NULL;
   while(p1->num!=0)
    {n=n+1;
     if(n==1)head=p1;
  else p2->next=p1;
  p2=p1;
  p1=(struct student*)malloc(LEN);
  scanf("%ld,%f",&p1->num,&p1->score);
 }
   p2->next=NULL;
   return(head);
  }

struct student *del(struct student *head,long num)
  {struct student *p1,*p2;
   if (head==NULL)
     {printf("\nlist null!\n");
      return(head);
     }
   p1=head;
   while(num!=p1->num && p1->next!=NULL)
      {p2=p1;p1=p1->next;}
   if(num==p1->num)
   {if(p1==head)head=p1->next;
    else p2->next=p1->next;
    printf("delete:%ld\n",num);
    n=n-1;
      }
   else printf("%ld not been found!\n",num);   
   return(head);
  }

struct student *insert(struct student *head, struct student *stud)
   {struct student *p0,*p1,*p2;
    p1=head;
    p0=stud;              
    if(head==NULL)
   {head=p0; p0->next=NULL;} 
    else
      {while((p0->num>p1->num) && (p1->next!=NULL))
         {p2=p1;    
          p1=p1->next;
         }         
       if(p0->num<=p1->num)
         {if(head==p1) head=p0;
          else p2->next=p0;     
          p0->next=p1;
         }
       else
         {p1->next=p0; p0->next=NULL;}
       }
     n=n+1;                
     return(head);
    }

void print(struct student *head)
   {struct student *p;
    printf("\nNow,These %d records are:\n",n); 
 p=head;
    if(head!=NULL)
      do
   {printf("%ld %5.1f\n",p->num,p->score);
       p=p->next;
      }while(p!=NULL);
   }

 

来源:c程序设计第四版谭浩强课后答案

本文链接:http://www.wb98.com/c/post/tanhaoqiang_9.9.html

本站文章搜索:

<< 上一篇下一篇 >>

搜索

网站分类

Tags列表

赞助商链接