数据结构上机测试2-1:单链表操作A
来源:互联网 发布:网络会员代理营销 编辑:程序博客网 时间:2024/06/05 05:20
就是一个链表的基本操作,注意最后遍历的时候注意空格
Problem Description
输入n个整数,先按照数据输入的顺序建立一个带头结点的单链表,再输入一个数据m,将单链表中的值为m的结点全部删除。分别输出建立的初始单链表和完成删除后的单链表。
Input
第一行输入数据个数n;
第二行依次输入n个整数;
第三行输入欲删除数据m。
第二行依次输入n个整数;
第三行输入欲删除数据m。
Output
第一行输出原始单链表的长度;
第二行依次输出原始单链表的数据;
第三行输出完成删除后的单链表长度;
第四行依次输出完成删除后的单链表数据。
第二行依次输出原始单链表的数据;
第三行输出完成删除后的单链表长度;
第四行依次输出完成删除后的单链表数据。
Example Input
1056 25 12 33 66 54 7 12 33 1212
Example Output
1056 25 12 33 66 54 7 12 33 12756 25 33 66 54 7 33
#include<iostream>#include<stdio.h>#include<string.h>#include<stdlib.h>using namespace std;#define Maxsize 1000000int a[Maxsize];typedef struct LNode{ int data; struct LNode *next;}LinkList;void CreateList(LinkList *&L,int n){ LinkList *s,*r; int i; L=(LinkList *)malloc(sizeof(LinkList)); r=L; for(i=0;i<n;i++) { s=(LinkList *)malloc(sizeof(LinkList)); s->data=a[i]; r->next=s; r=s; } r->next=NULL;}void DispList(LinkList *L){ LinkList *p=L->next; while(p!=NULL) { printf("%d",p->data); if(p->next==NULL) printf("\n"); else printf(" "); p=p->next; }}int DeleteList(LinkList *&L,int m){ LinkList *p=L,*q; int i=0; int e; while(p->next!=NULL) { if(p->next->data!=m) { i++; p=p->next; } else { q=p->next; e=q->data; p->next=q->next; free(q); } } return i;}int main(){ int n; int i; int m; int count; LinkList *L; scanf("%d",&n); for(i=0;i<n;i++) scanf("%d",&a[i]); scanf("%d",&m); CreateList(L,n); printf("%d\n",n); DispList(L); count=DeleteList(L,m); printf("%d\n",count); DispList(L); 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
- activeMQ学习
- 饿了么技术通告
- LINUX环境常用查看硬件设备信息命令小结
- string类(四、字符串比较相关)
- md5加密算法
- 数据结构上机测试2-1:单链表操作A
- 自定义View绘制文字
- C语言指针学习--表示地址
- 成员变量赋值问题【向前引用】
- linux驱动中地址空间转换
- SpringBoot(六)开发环境/测试环境和生产环境
- 7.清除浮动与定位
- 安卓apk文件在手机上安装后看不到图标
- 欢迎使用CSDN-markdown编辑器