编写实现链表排序的一种算法。说明为什么你会选择用这样的方法?求高手指点一下,谢谢!

要用C++写才行

?这样做理论上并没有改变原链表的特性,但是少了一些结点。
第二,在删除重复数节点L时并没有将其前趋节点的next指向所删除节点的next.所以你这样做会把链表断开。也就不能构成一个链表了。所以这么做是错误的。
第三、.void delsamenode(LinkList *&L)中L前为何加&,这个问题“&”是不需要加的。

下面是调试通过的代码:
#include <windows.h>
#include <string.h>
#include <malloc.h>
#include <stdio.h>

struct linklist
{
int x;
struct linklist *next;
};

int flag=0; //定义一上标志,声明为全局变量。作为判断是否存在相同数
struct linklist *q ,*h; //q作为移动指针,h作为返回的头指针

int in(int x,struct linklist *l)
{
struct linklist *p,*t;

p=l;

while(p!=NULL&&p->x!=x)
{
p=p->next;
}

if (p==NULL)
{
return 0;
}
else
{
return 1;
}
}

struct linklist *del(struct linklist *l)
{
struct linklist *p;

if (l==NULL||l->next==NULL)
{
return h;
}
if (in(l->x,l->next))
{

p=l;
if (flag) //如果之前已经存在一个数,并且在后面没有和它相同的数了,
那么就将要 删除的这一个数的前一个数的指针指向它的后一数。
{
q->next=p->next;
l=l->next;
}
else //如果不存在这样一个数,则将l下移。
{
l=l->next;
}

free(p);
del(l);
}
else
{

++flag;
if (flag==1) //一旦出现一个后面不存在和它相同的数时,h指向这个数,
//且h作为头指针,只判断一次。
{
h=l;
}
q=l; //q还是指向l,进行下一次的指针移动。
del(l->next);
}

}

int main()
{
struct linklist *l,*p,*t;
int i;

t=p=l=(struct linklist *)malloc(sizeof(struct linklist));

for (i=0;i<6;i++)
{
if (i==0)
{
scanf("%d",&(t->x));
}
else
{
t=(struct linklist *)malloc(sizeof(struct linklist));
scanf("%d",&(t->x));
p->next=t;
p=t;
}

}
p->next=NULL;
l=del(l);
while (l!=NULL)
{
printf("%d ",l->x);
l=l->next;
}
system("pause");
}
我进行了三种可能的输入测试,都没有问题:
第一种:1 2 3 4 5 6,输出还是 1 2 3 4 5 6
第二种:3 0 3 1 0 3,输出是 1 0 3
第三种:1 2 3 4 6 4,输出是 1 2 3 6 4
如果还有其它问题的话,找我!
温馨提示:答案为网友推荐,仅供参考
相似回答