数据结构上机测试2-1:单链表操作A

题目描述

输入n个整数,先按照数据输入的顺序建立一个带头结点的单链表,再输入一个数据m,将单链表中的值为m的结点全部删除。分别输出建立的初始单链表和完成删除后的单链表。

输入

第一行输入数据个数n;
第二行依次输入n个整数;
第三行输入欲删除数据m。

输出

第一行输出原始单链表的长度;
第二行依次输出原始单链表的数据;
第三行输出完成删除后的单链表长度;
第四行依次输出完成删除后的单链表数据。

示例输入

10
56 25 12 33 66 54 7 12 33 12
12

示例输出

10
56 25 12 33 66 54 7 12 33 12
7
56 25 33 66 54 7 33
 
 
  
  
  1. #include<stdio.h>   
  2. #include<stdlib.h>   
  3. struct node   
  4. {   
  5.     int date;   
  6.     struct node *next;   
  7. };   
  8. struct node *head=NULL,*p,*q,*tail;   
  9. int main()   
  10. {   
  11.     int n,t,m;   
  12.     scanf("%d",&n);   
  13.     t=n;   
  14.     while(n--)   
  15.     {   
  16.         if((p=(struct node*)malloc(sizeof(struct node)*1))==NULL)   
  17.             return 1;   
  18.         scanf("%d",&p->date);   
  19.         p->next=NULL;   
  20.         if(head==NULL)   
  21.         {   
  22.   
  23.             head=p;   
  24.             q=head;   
  25.         }   
  26.         else  
  27.         {   
  28.             q->next=p;   
  29.             q=q->next;   
  30.         }   
  31.     }   
  32.     scanf("%d",&m);   
  33.     printf("%d\n",t);   
  34.     q=head;   
  35.     while(q!=NULL)   
  36.     {   
  37.         printf("%d",q->date);   
  38.         if(q->next!=NULL)   
  39.             printf(" ");   
  40.         q=q->next;   
  41.     }   
  42.   
  43.     printf("\n");   
  44.   
  45.     p=head;   
  46.     while(p!=NULL)   
  47.     {   
  48.         if (p->date == m)   
  49.             t--;   
  50.         p = p->next;   
  51.     }   
  52.     printf("%d\n",t);   
  53.     tail=head;   
  54.     while(tail!=NULL)   
  55.     {   
  56.         if (tail->date != m)   
  57.         {   
  58.             printf("%d",tail->date);   
  59.             if(tail->next!=NULL)   
  60.                 printf(" ");   
  61.         }   
  62.         tail=tail->next;   
  63.     }   
  64.     printf("\n");   
  65.     return 0;   
  66. }   
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值