数据结构上机测试2-1:单链表操作A
来源:互联网 发布:网络就是个粪坑 编辑:程序博客网 时间:2024/06/06 05:04
Problem Description输入n个整数,先按照数据输入的顺序建立一个带头结点的单链表,再输入一个数据m,将单链表中的值为m的结点全部删除。分别输出建立的初始单链表和完成删除后的单链表。 Input第一行输入数据个数n;第二行依次输入n个整数;第三行输入欲删除数据m。 Output第一行输出原始单链表的长度;第二行依次输出原始单链表的数据;第三行输出完成删除后的单链表长度;第四行依次输出完成删除后的单链表数据。 Example Input1056 25 12 33 66 54 7 12 33 1212Example Output1056 25 12 33 66 54 7 12 33 12756 25 33 66 54 7 33Hint
#include<stdio.h>#include<malloc.h>struct node{int data;struct node *next;};node *creat(int n){node *head,*tail,*p;head=(node *)malloc(sizeof(node));head->next=NULL;tail=head;for(int i=0;i<n;i++){p=(node *)malloc(sizeof(node));scanf("%d",&p->data);p->next=NULL;tail->next=p;tail=p;}return head;}void del(node *&head,int n,int m){node *p,*q;p=head;while(p->next!=NULL){if(p->next->data==m){n--;q=p->next;p->next=q->next;free(q);}elsep=p->next;}printf("%d\n",n);}void disp(node *head){node *r=head;while(r->next->next!=NULL){printf("%d ",r->next->data);r=r->next;}printf("%d\n",r->next->data);}int main(){int n;scanf("%d",&n);node *head;head=creat(n);int m;scanf("%d",&m);printf("%d\n",n);disp(head);del(head,n,m);disp(head);return 0;}
阅读全文
0 0
- 1138 数据结构上机测试2-1:单链表操作A
- 数据结构上机测试2-1:单链表操作A
- 数据结构上机测试2-1:单链表操作A
- 数据结构上机测试2-1:单链表操作A
- 数据结构上机测试2-1:单链表操作A
- 数据结构上机测试2-1:单链表操作A
- 数据结构上机测试2-1:单链表操作A
- 数据结构上机测试2-1:单链表操作A
- 数据结构上机测试2-1:单链表操作A
- 数据结构上机测试2-1:单链表操作A
- SDUToj1138数据结构上机测试2-1:单链表操作A
- 数据结构上机测试2-1:单链表操作A
- 数据结构上机测试2-1:单链表操作A
- 数据结构上机测试2-1:单链表操作A
- 【1138】数据结构上机测试2-1:单链表操作A
- SDUT1138数据结构上机测试2-1:单链表操作A
- 数据结构上机测试2-1:单链表操作A
- 数据结构上机测试2-1:单链表操作A
- OpenCV学习笔记7-图像算术运算
- Python continue 语句
- 进阶2.JavaScript深入之词法作用域和动态作用域
- POJ
- SecureRandom生成随机数超慢 导致tomcat启动时间过长的解决办法
- 数据结构上机测试2-1:单链表操作A
- 深入理解uboot 2016
- shell中exec解释
- Qt总结
- VM12中CentOS7以NAT的方式连接网络的方法
- 极简Node教程-七天从小白变大神(一:你需要Express)
- index的注释与理解(1)JS遮罩层如何写之(二)
- Python pass 语句
- boost时间的操作