學生信息是:姓名,學號,性別,年齡,用一個鏈表,把這些信息連在一起,給出一個age, 在些鏈表中刪除學生年齡等於age的學生信息。

http://blog.csdn.net/dahai_881222/article/details/7873463


typedef struct stu
{
    char name[20];
    char sex;
    int no;
    int age;
    struct stu * next;
}node;

node *create(int n)
{
 int i;
 node *head,*p ,*s ,*s1;
 head = (node*)malloc(sizeof(node));
 head->next = NULL;
 p = head;
 
 while(n)
 {
  s = (node*)malloc(sizeof(node));
  p->next = s;
  printf("請輸入每個人的信息:姓名 性別 學號 年齡 ");
  scanf("%s %c %d %d",s->name ,&s->sex ,&s->no,&s->age);
  s->next = NULL;
  p = s;
  n--;
 }
 return head;
}

 

void del(node *head , int a)
{
   node *p,*s,*str;
   p = head;
   while(p != NULL)
   {   
     s = p;
        p = p->next;
    if( p->age == a )
    {
          s->next = p->next;
    break;
    }
   }
}

void display(node *head)
{
 node *p;
 p = head->next;
 while(p != NULL)
 {
  printf("%s %c %d %d",p->name ,p->sex ,p->no ,p->age);
  p = p->next;
 }
}

void main()
{
 node *head;
 int n;
 printf("請輸入創建的個數:");
 scanf("%d",&n);
 head = create(n);
 display(head);
 int age;
 printf("請輸入刪除的年齡:");
 scanf("%d",&age);
 del(head , age);
 display(head);
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章