顺序表应用1:多余元素删除之移位算法
2016-07-27 14:54
393 查看
链表做法
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct no
{
int data;
struct no *next;
} node;
node *creat(int n)
{
node *head,*p,*tail;
head=(node *)malloc(sizeof(node));
head->next=NULL;
tail=head;
for(int i=1; i<=n; i++)
{
p=(node *)malloc(sizeof(node));
scanf("%d",&p->data);
p->next=NULL;
tail->next=p;
tail=p;
}
return head;
}
node *del(node *head)
{
node *p,*q,*k;
p=head->next;
while(p)
{
k=p;
q=p->next;
while(q)
{
if(p->data==q->data)
{
k->next=q->next;
free(q);
q=k->next;
}
else
{
k=q;
q=q->next;
}
}
p=p->next;
}
return head;
}
node *print(node *p)
{
node*q;
q=p->next;
while(q)
{
if(q->next==NULL)
{
printf("%d\n",q->data);
}
else
{
printf("%d ",q->data);
}
q=q->next;
}
return 0;
}
int main()
{
node *p;
int n,m;
scanf("%d",&n);
while(n--)
{
scanf("%d",&m);
p=creat(m);
del(p);
print(p);
}
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct no
{
int data;
struct no *next;
} node;
node *creat(int n)
{
node *head,*p,*tail;
head=(node *)malloc(sizeof(node));
head->next=NULL;
tail=head;
for(int i=1; i<=n; i++)
{
p=(node *)malloc(sizeof(node));
scanf("%d",&p->data);
p->next=NULL;
tail->next=p;
tail=p;
}
return head;
}
node *del(node *head)
{
node *p,*q,*k;
p=head->next;
while(p)
{
k=p;
q=p->next;
while(q)
{
if(p->data==q->data)
{
k->next=q->next;
free(q);
q=k->next;
}
else
{
k=q;
q=q->next;
}
}
p=p->next;
}
return head;
}
node *print(node *p)
{
node*q;
q=p->next;
while(q)
{
if(q->next==NULL)
{
printf("%d\n",q->data);
}
else
{
printf("%d ",q->data);
}
q=q->next;
}
return 0;
}
int main()
{
node *p;
int n,m;
scanf("%d",&n);
while(n--)
{
scanf("%d",&m);
p=creat(m);
del(p);
print(p);
}
}
相关文章推荐
- c++ 内存管理
- install java on ubuntu and set defalut java version
- 获取drawable目录下图片的uri地址
- C++拷贝构造函数详解
- 错误:fatal error C1189: #error : The C++ Standard Library forbids macroizing keywords. Enable warning
- AFHTTPRequestOperationManager 请求后台获得不到参数解决方法
- 只选对的,不买贵的
- input oncopy用法
- AngularJS入门教程之控制器详解
- Android中的dispatchTouchEvent()、onInterceptTouchEvent()和onTouchEvent()
- Inno Setup入门(八)——有选择性的安装文件
- Android(java方法)上实现mp4的分割和拼接 (二)
- 解决asp.net mvc的跨域请求问题
- 助词复习—3
- while/if的判断
- 基础系列(二):FHS和man的使用
- IOS学习笔记-获取当前的地理位置swift版
- MapReduce 单元测试工具 MRUnit 使用
- iOS开发 ☞ __block,__weak,__strong
- OpenCV2.4.13在VS2012环境的debug模式下的链接库输入配置的文件目录