C链表2-删除

 

#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 delete number:\n");

 int num;

  scanf("%d",&num);

  if(num <= 1 )

   {

      exit;

   }

  printf("The result is:\n");

  head1=head;

  int k=1;

  while(head !=NULL)

   {

      if(k == num-1){

           head->next=head->next->next;break;

       }

      head =head->next;

      k++;

   }

 

  while(head1 !=NULL)

   {

       printf("%d\n",head1->score);

       head1=head1->next;

   }

 

 

}

你可能感兴趣的:(链表,删除)