#include"stdio.h"
typedef struct lnode
{
int date;
struct lnode *next;
}lnode,*linklist;
void deletecpy(linklist &head,int &n)
{
linklist p,q;
p=head->next;
while(p&&p->next)
{
if(p->date==p->next->date)
{
p->next=p->next->next;
n--;
}
else
p=p->next;
}
}
int main()
{
int n,i;
linklist head,p;
head=new lnode;
head->next=NULL;
printf("请输入要输入几个整数\n");
scanf("%d",&n);
printf("请输入%d个数\n",n);
for( i=0;i<n;i++)
{
p=new lnode;
p->next=NULL;
scanf("%d",&p->date);
head->next=p;
}
deletecpy(head,n);
p=head->next;
while(p)
{
printf("%d\t",p->date);
p=p->next;
}
}
生成链表那段有问题,修改如下,供参考:
#include <stdio.h>
typedef struct lnode
{
int date;
struct lnode* next;
}lnode, * linklist;
void deletecpy(linklist& head, int& n)
{
linklist p, q;
p = head->next;
while (p && p->next)
{
if (p->date == p->next->date)
{
p->next = p->next->next;
n--;
}
else
p = p->next;
}
}
int main()
{
int n, i;
linklist head, p, q; //修改
head = new lnode;
head->next = NULL;
printf("请输入要输入几个整数\n");
scanf("%d", &n);
printf("请输入%d个数\n", n);
for (i = 0; i < n; i++)
{
p = new lnode;
p->next = NULL;
scanf("%d", &p->date);
if (i == 0) //修改
head->next = p;
else
q->next = p;
q = p;
}
deletecpy(head, n);
p = head->next;
while (p)
{
printf("%d\t", p->date);
p = p->next;
}
}