博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
数据结构实验之链表七:单链表中重复元素的删除
阅读量:3948 次
发布时间:2019-05-24

本文共 1497 字,大约阅读时间需要 4 分钟。

数据结构实验之链表七:单链表中重复元素的删除

Time Limit: 1000 ms Memory Limit: 65536 KiB

Problem Description

按照数据输入的相反顺序(逆位序)建立一个单链表,并将单链表中重复的元素删除(值相同的元素只保留最后输入的一个)。

Input

第一行输入元素个数 n (1 <= n <= 15);
第二行输入 n 个整数,保证在 int 范围内。

Output

第一行输出初始链表元素个数;
第二行输出按照逆位序所建立的初始链表;
第三行输出删除重复元素后的单链表元素个数;
第四行输出删除重复元素后的单链表。

Sample Input

10
21 30 14 55 32 63 11 30 55 30
Sample Output
10
30 55 30 11 63 32 55 14 30 21
7
30 55 11 63 32 14 21
Hint
Source
不得使用数组!

代码如下:

#include
#include
#include
struct node{ int data; struct node *next;};void delete1(struct node *head,int n){ int i,count=0; struct node *p,*q,*r; p=head->next; q=p; while(p) { while(q->next) { if(p->data==q->next->data) { r=q->next; q->next=r->next; free(r); count++; } else q=q->next; } p=p->next; q=p; } printf("%d\n",n-count); head=head->next; for(i=0;i
data); if(i==n-count-1) printf("\n"); else printf(" "); head=head->next; }}int main(){ int n,i; struct node *head,*p,*q; head=(struct node*)malloc(sizeof(struct node)); head->next=NULL; q=head; scanf("%d",&n); for(i=0;i
data); p->next=head->next; head->next=p; } printf("%d\n",n); q=q->next; for(i=0;i
data); if(i==n-1) printf("\n"); else printf(" "); q=q->next; } delete1(head,n); return 0;}

转载地址:http://rwhwi.baihongyu.com/

你可能感兴趣的文章
Defining and Launching the Query 定义和启动查询
查看>>
Handling the Results 处理结果
查看>>
如何内置iperf到手机中
查看>>
如何adb shell进入ctia模式
查看>>
Contacts Provider 联系人存储
查看>>
android 图库播放幻灯片时灭屏再亮屏显示keyguard
查看>>
android 图库语言更新
查看>>
android camera拍照/录像后查看图片/视频并删除所有内容后自动回到camera预览界面
查看>>
android 图库中对非mp4格式的视频去掉"修剪"功能选项
查看>>
how to disable watchdog
查看>>
android SDIO error导致wifi无法打开或者连接热点异常的问题
查看>>
android USB如何修改Serial Number or SN?
查看>>
android 用svn管理的版本编译出来有问题
查看>>
android 如何用jar包代替java代码编译
查看>>
android 数据连接关闭的情况下如何让彩信发不出去
查看>>
android 编辑彩信,加入几页铃声,预览暂停界面,铃声名字不见了
查看>>
android 在新建短信时,加入名称为","(英文逗号)的联系人时,应用崩溃的修改
查看>>
android 关于LCD背光调节渐变过程引起背光闪烁问题
查看>>
android 保存具有不同前缀的同一号码分别为A和B,用其中一个呼叫,通话记录一直显示另一个联系人名字的问题
查看>>
android 在手机中预置联系人/Service Number
查看>>