数据结构上机测试2-1:单链表操作A
来源:互联网 发布:网络时间校准 编辑:程序博客网 时间:2024/05/14 18:06
题目描述
输入n个整数,先按照数据输入的顺序建立一个带头结点的单链表,再输入一个数据m,将单链表中的值为m的结点全部删除。分别输出建立的初始单链表和完成删除后的单链表。
输入
第一行输入数据个数n;
第二行依次输入n个整数;
第三行输入欲删除数据m。
第二行依次输入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 <iostream>#include<bits/stdc++.h>using namespace std;struct node{ int data; node *next;};int m;struct node *create(int n){ int i; node *tail,*head,*p; head=new node; head->next=NULL; tail=head; for(i=0;i<n;i++) { p=new node; scanf("%d",&p->data); tail->next=p; tail=p; } return (head);};void print(struct node *p){ struct node *h=p->next; while(h!=NULL) { if(h->next==NULL) printf("%d",h->data); else printf("%d ",h->data); h=h->next; }}void num(node *head){ int i=0; node *p=head->next; while(p) { i++; p=p->next; } printf("%d\n",i);}void del(node *p){ node *h; h=p; node *q; while(h->next) { if(h->next->data==m)//删除指定元素; { q=h->next; h->next=q->next; free(q); } else h=h->next; }}int main(){ int n; scanf("%d",&n); node *p=create(n); scanf("%d",&m); num(p); print(p); printf("\n"); del(p); num(p); print(p); 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
- iOS巅峰之NSString,NSData,NSArray 之间的转换
- Spring 整合 Hibernate
- 关于 Vulkan 简介 —— Android N 引入新的 3D 渲染引擎
- 26. Remove Duplicates from Sorted Array
- GridView的用法、自定义控件入门写法和界面切换动画
- 数据结构上机测试2-1:单链表操作A
- 数组分割
- atoi 原来将字符串02002xzm100转换为int以后是2002
- 百家姓
- Install OpenCV on Ubuntu or Debian
- 非常简单的图片转换成PDF方法
- java对象转URL 键值对(微信支付,签名需要)
- redis主从同步配置和哨兵机制监控master主从切换配置
- leetcode题解日练--2016.8.29