1138- 数据结构上机测试2-1:单链表操作A
2017-08-29 17:28
260 查看
数据结构上机测试2-1:单链表操作A
Problem Description
输入n个整数,先按照数据输入的顺序建立一个带头结点的单链表,再输入一个数据m,将单链表中的值为m的结点全部删除。分别输出建立的初始单链表和完成删除后的单链表。Input
第一行输入数据个数n;第二行依次输入n个整数;
第三行输入欲删除数据m。
Output
第一行输出原始单链表的长度;第二行依次输出原始单链表的数据;
第三行输出完成删除后的单链表长度;
第四行依次输出完成删除后的单链表数据。
Example Input
1056 25 12 33 66 54 7 12 33 12
12
Example Output
1056 25 12 33 66 54 7 12 33 12
7
56 25 33 66 54 7 33
#include<stdio.h> #include<iostream> #include<string.h> #include<stdlib.h> using namespace std; typedef struct node { int data; struct node *next; }node; node *creat(int n)//建立单链表 { node *s,*p,*l; int i; l=(node *)malloc(sizeof(node));//头结点 l->next=NULL; p=l; for(i=1;i<=n;i++) { s=(node *)malloc(sizeof(node)); scanf("%d",&s->data); p->next=s; p=s; } p->next=NULL; return l; } void de(node *l,int n,int m)//删除数据 { node *p,*q; p=l; while(p->next!=NULL) { if(p->next->data==m) { n--; q=p->next; p->next=q->next; free(q); } else p=p->next; } cout<<n<<endl; } void shuchu(node *l)//链表的输出 { node *r; r=l->next; while(r&&r->next!=NULL) { printf("%d ",r->data); r=r->next; } printf("%d",r->data); } int main() { int n,m; node *l; cin>>n;//输入n l=creat(n);//链表的建立 cin>>m;//输入要删除的元素 cout<<n<<endl; shuchu(l); cout<<endl; de(l,n,m); shuchu(l); return 0; }
相关文章推荐
- 1138 数据结构上机测试2-1:单链表操作A
- 【1138】数据结构上机测试2-1:单链表操作A(顺序建表+删除节点)(SDUT)
- 【1138】数据结构上机测试2-1:单链表操作A
- 【1138】数据结构上机测试2-1:单链表操作A(顺序建表+删除节点)(SDUT)
- 数据结构上机测试2-1:单链表操作A
- 数据结构上机测试2-1:单链表操作A
- 暑假集训第1天链表-数据结构上机测试2-1:单链表操作A
- 【1139】数据结构上机测试2-2:单链表操作B
- 数据结构上机测试2-1:单链表操作A
- 【1139】数据结构上机测试2-2:单链表操作B (逆序建表+重复元素删除)
- 数据结构上机测试2-2:单链表操作B
- 【1139】数据结构上机测试2-2:单链表操作B (逆序建表+重复元素删除)
- 数据结构上机测试2-1:单链表操作A
- 数据结构上机测试2-1:单链表操作A
- 数据结构上机测试2-1:单链表操作A
- 寒假第二天--线性表--数据结构上机测试2-1:单链表操作A
- 数据结构上机测试2-1:单链表操作A
- 数据结构上机测试2-2:单链表操作B
- 寒假第二天--线性表--数据结构上机测试2-2:单链表操作B
- 数据结构上机测试2-2:单链表操作B