240页345题

#include<stdio.h>
#include<stdlib.h>
struct link 
{
    char jnum[10];
    int salary;
    struct link *next;
 } ;
 struct link *invent(void);
 void output(struct link *head);
 struct link *add(struct link *head);
 struct link *exclude(struct link *head);
int main()
{
    struct link *head,*p;
    printf("本程序首先请输入五个员工的工号和工资。
");
    head=invent();
    p=head;
    printf("五个员工的信息如下:
");
    output(p);
    p=add(p);
    if(p==NULL)
    {
        printf("位置错误。
");
    }
    else
    {
        printf("五个员工的信息如下:
");
        output(p);
    }
    p=exclude(p);
    if(p==NULL)
    {
        printf("位置错误。
");
    }
    else
    {
        printf("五个员工的信息如下:
");
        output(p);
    }
    return 0;
}
 struct link *invent(void)
 {
     struct link *head=NULL,*tail,*new;
     int icount=0;
     while(icount<5)
     {
         new=(struct link *)malloc(sizeof(struct link));
         printf("请输入第%d个员工的工号(共五个):",icount+1);
         gets(new->jnum);
         printf("请输入第%d个员工的工资(共五个):",icount+1);
         scanf("%d",&new->salary);
         new->next=NULL;
         icount++;
         if(icount==1)
         {
             head=tail=new;
         }
         else
         {
             tail->next=new;
             tail=new;
         }
         fflush(stdin);
     }
     return head;
 }
  void output(struct link *head)
  {
      struct link *p;
      int n=0;
      p=head;
      while(p!=NULL)
      {
          n++;
          printf("第%d个员工的工号为",n );
          puts(p->jnum);
          printf("第%d个员工的工号资为:%d
",n,p->salary  );
          p=p->next ;
      }
  }
   struct link *add(struct link *head)
   {
           struct link *p,*new;
           int n,i;
           p=head;
           
           new=(struct link *)malloc(sizeof(struct link));
           printf("现在要添加一个工人的信息。");
           printf("请输入所添加的位置:");
           scanf("%d",&n);
           getchar();
           printf("请输入工人的工号:");
           gets(new->jnum);
           printf("请输入工人的工资:");
           scanf("%d",&new->salary);
        if(p==NULL)
           {
               p=new;
               return p;
           }
           if(n==1||n==0)
           {
            new->next=p;
            p=new;
            return p;
        }
           for(i=1;i<n-1&&p!=NULL;i++)
           {
           p=p->next;
        }
        if(p==NULL)
        {
            return NULL;
        }
        else
        {
            new->next=p->next;
            p->next=new;
        }
           return head;
   }
  struct link *exclude(struct link *head)
  {
       struct link *p,*q;
       int n=0,i=0;
       p=head;
       printf("请输入所删除员工信息的位置:");
       scanf("%d",&n);
       if(n==1)
       {
       q=p;
       p=p->next;
       free(q);
       return p;
    }
       i=1;
       while(i<n-1&&p!=NULL)
    {
        p=p->next ;
        i++;
    }
    if(p==NULL)
    {
        return NULL;
    }
    else
    {
        q=p->next;
        p->next=p->next->next;
        free(q);
    }
    return head;
  }

还是不太熟悉,写代码一定要细心,勤练,时隔长了手生。在添加和删除的时候记住是两种情况一种是在链表头部一种是在内部或者尾部,不要丢掉是链表头部这一种情况。定义指针太多了,一定要分清楚那个指针时指向哪里,不要乱了分寸,时刻保持清醒。总体感觉这次写的不太好,还是缺练。

原文地址:https://www.cnblogs.com/TX980502/p/6682285.html