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

来源:互联网 发布:淘宝里实名认证在哪 编辑:程序博客网 时间:2024/06/07 23:15

题目描述

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

输入

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

输出

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

示例输入

1056 25 12 33 66 54 7 12 33 1212

示例输出

1056 25 12 33 66 54 7 12 33 12756 25 33 66 54 7 33
我的代码:
#include<stdio.h>#include<stdlib.h>struct hh{int a;struct hh *next;};void main(){struct hh *head ,*p,*t,*q;int n,i,m,x;scanf("%d",&n);m=n;head=(struct hh *)malloc(sizeof(struct hh));head->next=NULL;t=head;for(i=0;i<n;i++){p=(struct hh *)malloc(sizeof(struct hh));scanf("%d",&p->a);p->next=NULL;t->next=p;t=p;}    scanf("%d",&x);printf("%d\n",n);p=head->next;for(i=0;i<n-1;i++){printf("%d ",p->a);p=p->next;}printf("%d\n",p->a);q=head;p=q->next;for(i=0;i<n;i++){    if(x==p->a){            q->next=p->next;free(p);m--;p=q->next;}else{q=q->next;p=q->next;}}printf("%d\n",m);p=head->next;for(i=0;i<m-1;i++){printf("%d ",p->a);p=p->next;}printf("%d\n",p->a);}
1 0
原创粉丝点击