数据结构上机测试2-2:单链表操作B
来源:互联网 发布:网络著作权论文 编辑:程序博客网 时间:2024/06/03 16:22
Problem Description
按照数据输入的相反顺序(逆位序)建立一个单链表,并将单链表中重复的元素删除(值相同的元素只保留最后输入的一个)。
Input
第一行输入元素个数n;
第二行输入n个整数。
第二行输入n个整数。
Output
第一行输出初始链表元素个数;
第二行输出按照逆位序所建立的初始链表;
第三行输出删除重复元素后的单链表元素个数;
第四行输出删除重复元素后的单链表。
第二行输出按照逆位序所建立的初始链表;
第三行输出删除重复元素后的单链表元素个数;
第四行输出删除重复元素后的单链表。
Example Input
1021 30 14 55 32 63 11 30 55 30
Example Output
1030 55 30 11 63 32 55 14 30 21730 55 11 63 32 14 21
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
using namespace std;
#define Maxsize 1000000
int a[Maxsize];
typedef struct LNode
{
int data;
struct LNode *next;
}LinkList;
void CreateList(LinkList *&L,int n)//头插法建表
{
LinkList *s;
int i;
L=(LinkList *)malloc(sizeof(LinkList));
L->next=NULL;//创建头结点,值为NULL
for(i=0;i<n;i++)//循环建立数据节点
{
s=(LinkList *)malloc(sizeof(LinkList));//创建数据节点s
s->data=a[i];
s->next=L->next;//将s节点插入到L中
L->next=s;
}
}
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 n)
{
LinkList *p,*q,*r;
int count;
count=n;
p=L->next;
while(p!=NULL)//循环进行比较
{
q=p;
while(q->next!=NULL)
{
if(q->next->data==p->data)//如果后面的数据和当前的数据相同,删除相同的节点
{
r=q->next;
int e=r->data;
q->next=r->next;
free(r);
count--;//使总数减一
}
else//否则就继续循环
q=q->next;
}
p=p->next;
}
return count;
}
int main()
{
int n;
int i;
int count;
LinkList *L;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
CreateList(L,n);
printf("%d\n",n);
DispList(L);
count=DeleteList(L,n);
printf("%d\n",count);
DispList(L);
return 0;
}
0 0
- 数据结构上机测试2-2:单链表操作B
- 数据结构上机测试2-2:单链表操作B
- 数据结构上机测试2-2:单链表操作B
- 数据结构上机测试2-2:单链表操作B
- 数据结构上机测试2-2:单链表操作B
- 数据结构上机测试2-2:单链表操作B
- 数据结构上机测试2-2:单链表操作B
- 数据结构上机测试2-2:单链表操作B
- 数据结构上机测试2-2:单链表操作B
- 【1139】数据结构上机测试2-2:单链表操作B
- 数据结构上机测试2-2:单链表操作B
- 数据结构上机测试2-2:单链表操作B
- SDUT1139数据结构上机测试2-2:单链表操作B
- 数据结构上机测试2-2:单链表操作B
- SDUTACM数据结构上机测试2-2:单链表操作B
- 数据结构上机测试2-2:单链表操作B
- sdut oj1139 数据结构上机测试2-2单链表操作B
- 数据结构上机测试2-2:单链表操作B
- tongbu
- 控制线段旋转角度和长度
- 招人以及团队建设
- URAL - 2105 Alice and Bob are on Bikes 整体处理、相遇问题
- angular-filemanager API docs
- 数据结构上机测试2-2:单链表操作B
- rcp(插件开发) 如果强制关闭对话框向导(Wizard)
- Centos6.5服务器环境搭建之安装Tomcat以及开机自启动的配置
- Linux下的ftp命令初探
- ViewPager的BasePagerAdapter
- javascript作用域链的概念解析。
- web服务器返回406的解决方法
- 【C++心路历程20】干草塔高
- 配置pps秒脉冲网址汇总