C-链表3-增加

 #include "stdio.h"

#include "stdlib.h"
 
struct grade{
  int score;
  struct grade *next;
};
 
int main()
{
 int score;
 struct grade *head,*p1,*p2,*head1;
 p1=(struct grade *)malloc(sizeof(struct grade));
 scanf("%d",&score);
 p1->score=score;
 head=p1;
  p2=p1;
 while(1)
  {
     p1=(struct grade *)malloc(sizeof(struct grade));
    scanf("%d",&score);
    if(score <= 0) break;
    p1->score=score;
    p2->next=p1;
    p2=p1;
  }
 p2->next=NULL;
 free(p1);
 
 printf("Plz input the insert pos and value:\n");
 int pos,value;
  scanf("%d,%d",&pos,&value);
  if(pos < 1 )
   {
      exit;
   }
  struct grade *add;
  add=(struct grade *)malloc(sizeof(struct grade));
  add->score=value;
  printf("The result is:\n");
  head1=head;
  
  int k=1;
  while(head != NULL)
   {
      if(k == pos)
       {
         struct grade *tmp;
         tmp=head->next;   
         head->next=add;
         add->next=tmp;
         break;
       }
      head=head->next;
      k++;
   }
 
 
 
 
 
  while(head1 !=NULL)
   {
       printf("%d\n",head1->score);
       head1=head1->next;
   }
 
 
}
 

你可能感兴趣的:(链表,增加)