数据结构上机测试1:顺序表的应用
来源:互联网 发布:蒙文软件免费下载 编辑:程序博客网 时间:2024/06/06 18:26
点击打开链接
数据结构上机测试1:顺序表的应用
Problem Description
Input
第二行依次输入顺序表初始存放的n个元素值。
Output
第二行依次输出完成删除后的顺序表元素。
Example Input
125 2 5 3 3 4 2 5 7 5 4 3
Example Output
55 2 3 4 7
Hint
Author
/****************一开始没注意到要输出元素的个数 错了 20多发 后来。。。。。。。。。。恨死自己了*********/
#include <bits/stdc++.h>
using namespace std;
struct node
{
int data;
struct node *next,*left;
};
struct node * creat(int n)///顺序建立双向链表
{
struct node *head=new node,*tail,*q;
head->next=head->left=NULL;///初始化
tail=head;
while(n--)
{
q=new node ;
cin>>q->data;
q->next=NULL;
q->left=NULL;
tail->next=q;
q->left=tail;
tail=q;
}
tail->next=NULL;
return head;
}
void dell(struct node *head,int *n)///删除链表中的重复元素
{
struct node *q=head->next,*p;
if(head->next==NULL)
return;
while(q->next)
{
p=q->next;///用q来查找相同元素
while(p)
{
if( p->data == q->data )///找到相同元素 删除
{
if(p->next==NULL)///p是最后一个节点
{
p->left->next=NULL;
free(p);
(*n)--;
break;
}
else
{
struct node *t=p->next;///相tongjiedian得下一个值
struct node *t2=p->left;///相同节点的上一个节点
t2->next=t;
t->left=t2;
free(p);
p=t;
(*n)--;
}
}
else p=p->next;///不是相同元素向后移
}
if(q->next)///如果 p不是链表的最后一个继续向后移
q=q->next;
else break;
}
}
void output(struct node *head)///遍历链表
{
struct node *p=head->next;
while(p)
{
cout<<p->data;
if( p->next )
{
cout<<" ";
}
else cout<<'\n';
p=p->next;
}
}
int main()
{
int n;
cin>>n;
struct node *head=creat(n);///顺序建立双向链表
dell(head,&n);///删除链表中的重复元素
cout<<n<<endl;
output(head);///遍历双向链表
return 0;
}
- 数据结构上机测试1:顺序表的应用
- 1130数据结构上机测试1:顺序表的应用
- 1130 数据结构上机测试1:顺序表的应用
- 数据结构上机测试1:顺序表的应用
- 数据结构上机测试1:顺序表的应用
- 数据结构上机测试1:顺序表的应用
- 数据结构上机测试1:顺序表的应用
- 数据结构上机测试1:顺序表的应用
- 数据结构上机测试1:顺序表的应用!!!
- 数据结构上机测试1:顺序表的应用
- 数据结构上机测试1:顺序表的应用
- 数据结构上机测试1:顺序表的应用
- 数据结构上机测试1:顺序表的应用
- 数据结构上机测试1:顺序表的应用
- 数据结构上机测试1:顺序表的应用
- SDUToj1130数据结构上机测试1:顺序表的应用
- 数据结构上机测试1:顺序表的应用
- 【1130】数据结构上机测试1:顺序表的应用
- Unity全面优化
- 在Servlet中直接调用Spring的getBean引起的HTTP Status 500
- 2027
- 对ac自动机的一些个人理解
- cordova的安装与配置
- 数据结构上机测试1:顺序表的应用
- 【LeetCode】11. Container With Most Water
- 2028
- 资金流入流出预测-挑战Baseline
- OJ -1272 写数游戏 求助指导
- Sysbench出图
- 2029
- 一维数组名与二维数组名的差别
- OAuth2