重复元素的删除问题
2016-07-26 20:26
295 查看
代码:
#include<stdio.h>
#include<malloc.h>
struct node
{
int data;
struct node *next;
};
struct node *creat(int n)
{
struct node *head,*p;
head=(struct node*)malloc(sizeof(struct node));
head->next=NULL;
for(int i=0;i<n;i++)
{
p=(struct node*)malloc(sizeof(struct node));
scanf("%d",&p->data);
p->next=NULL;
p->next=head->next;
head->next=p;
}
return head;
}
void print(struct node *h)
{
struct node *p=h->next;
while(p!=NULL)
{
if(p->next==NULL)
printf("%d\n",p->data);
else
printf("%d ",p->data);
p=p->next;
}
}
struct node *chazhao(struct node *head,int n)
{
struct node *p,*q,*h;
h=head->next;
while(h!=NULL)
{
p=h;
while(p->next!=NULL)
{
if(p->next->data==h->data)
{
q=p->next;
p->next=q->next;
free(q);
n--;
}
else
p=p->next;
}
h=h->next;
}
printf("%d\n",n);
return head;
}
int main()
{
int n;
scanf("%d",&n);
struct node *head1,*head2;
head1=creat(n);
printf("%d\n",n);
print(head1);
head2=chazhao(head1,n);
print(head2);
return 0;
}
更多0
#include<stdio.h>
#include<malloc.h>
struct node
{
int data;
struct node *next;
};
struct node *creat(int n)
{
struct node *head,*p;
head=(struct node*)malloc(sizeof(struct node));
head->next=NULL;
for(int i=0;i<n;i++)
{
p=(struct node*)malloc(sizeof(struct node));
scanf("%d",&p->data);
p->next=NULL;
p->next=head->next;
head->next=p;
}
return head;
}
void print(struct node *h)
{
struct node *p=h->next;
while(p!=NULL)
{
if(p->next==NULL)
printf("%d\n",p->data);
else
printf("%d ",p->data);
p=p->next;
}
}
struct node *chazhao(struct node *head,int n)
{
struct node *p,*q,*h;
h=head->next;
while(h!=NULL)
{
p=h;
while(p->next!=NULL)
{
if(p->next->data==h->data)
{
q=p->next;
p->next=q->next;
free(q);
n--;
}
else
p=p->next;
}
h=h->next;
}
printf("%d\n",n);
return head;
}
int main()
{
int n;
scanf("%d",&n);
struct node *head1,*head2;
head1=creat(n);
printf("%d\n",n);
print(head1);
head2=chazhao(head1,n);
print(head2);
return 0;
}
更多0
相关文章推荐
- Failed to instantiate * using constructor public * with arguments **
- 【HDU 1950】Bridging signals(LIS算法求最长上升子序列)
- android ListView 记录当前位置 恢复
- 操蛋的Camera V2
- linux文件系统及文件属性
- MVC中Controller中的跳转
- 再谈‘进程’
- 网编基础(2)
- bootstrap3上传特效
- paper 92:Lena与图像处理
- 在 Laravel 5 中使用 Laravel Excel 实现 Excel/CSV 文件导入导出功能
- iOS资源大全中文版
- 超详细JSTL标签 参考手册
- Android控件属性
- Linux常见知识
- paper 91:边缘检测近期最新进展的讨论
- 何教授去雾算法C++实现(调试)
- [Python] 技术贴系列之——Django部署方案简介
- MAC地址特殊规定--设置MAC地址时需要有非法MAC设置判断,否则会导致网卡无法启动
- 最短路算法spfa hdu 1863