我想有序链表合并,但是编译不通过 ,不知道如何定义链表,忘高手解答
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
List<int> listFirst;
List<int> listSecond;
listFirst.AddTail(1);
listFirst.AddTail(6);
listFirst.AddTail(9);
listFirst.AddTail(10);
listFirst.AddTail(12) ;
listSecond.AssTail(0);
listSecond.AssTail(3);
listSecond.AssTail(8);
listSecond.AssTail(11);
listSecond.AssTail(30);
while(listSecond.GetCount()!=0)
{
int indexFirst=0;
while (listSecond.GetAt(0)>listFirst.GetAt(indexFirst))
{
indexFirst++;
if(indexFirst==listFirst.GetCount())
break;
}
if(indexFirst==listFirst.GetCount()
{
listFirst.AddTail(listSecond.GetAt(0));
listSecond.RemoveAt(0);
}
else
{
listFirst.InsertAt(indexFirst,listSecond.GetAt(0));
listSecond.RemoveAt(0);
}
}
system("PAUSE");
return EXIT_SUCCESS;
}
[解决办法]
有什么提示信息?
[解决办法]
有序整型链表a和b,假设都是从小到大排序,请写出将两链表合并到链表c的程序
#include <stdio.h>
#include <malloc.h>
struct NODE
{
int value;
NODE *next;
};
int main(int argc, char* argv[])
{
int i;
struct NODE * LK_a,*LK_b,*LK_c,*N_current_a,*N_current_b,*N_current_c,*N_before_a,*N_before_b,*N_before_c,*pa,*pb,*pc;
//init linktable a and b for testing
LK_a=(struct NODE *)calloc(1,sizeof(NODE));
N_current_a=LK_a;
N_before_a=NULL;
LK_b=(struct NODE *)calloc(1,sizeof(NODE));
N_current_b=LK_b;
N_before_b=NULL;
for (i=0;i<10;i++)
{
N_current_a->value=i+1;
if (N_before_a)
{
N_before_a->next=N_current_a;
}
N_before_a=N_current_a;
N_current_b->value=2*(i+1);
if (N_before_b)
{
N_before_b->next=N_current_b;
}
N_before_b=N_current_b;
N_current_a=(struct NODE *)calloc(1,sizeof(NODE));
N_current_b=(struct NODE *)calloc(1,sizeof(NODE));
}
N_before_a->next=NULL;
N_before_b->next=NULL;
//free the memory used by N_current_a and N_current_b
free((void *)N_current_a);
free((void *)N_current_b);
printf("Print the value of linktable A:\n");
pa=LK_a;
while (pa)
{
printf("%d\n",pa->value);
pa=pa->next;
}
printf("Print the value of linktable B:\n");
pb=LK_b;
while (pb)
{
printf("%d\n",pb->value);
pb=pb->next;
}
pa=LK_a;
pb=LK_b;
//unite a and b to c
LK_c=(struct NODE *)calloc(1,sizeof(NODE));
N_current_c=LK_c;
N_before_c=NULL;
while (pa&&pb)
{
if (pa->value<pb->value)
{
N_current_c->value=pa->value;
if (N_before_c)
{
N_before_c->next=N_current_c;
}
N_before_c=N_current_c;
pa=pa->next;
}
else if (pa->value>pb->value)
{
N_current_c->value=pb->value;
if (N_before_c)
{
N_before_c->next=N_current_c;
}
N_before_c=N_current_c;
pb=pb->next;
}
else
{
N_current_c->value=pa->value;
if (N_before_c)
{
N_before_c->next=N_current_c;
}
N_before_c=N_current_c;
pa=pa->next;
pb=pb->next;
}
N_current_c=(struct NODE *)calloc(1,sizeof(NODE));
}
while (pa)
{
N_current_c->value=pa->value;
if (N_before_c)
{
N_before_c->next=pa;
}
N_before_c=N_current_c;
pa=pa->next;
N_current_c=(struct NODE *)calloc(1,sizeof(NODE));
}
while (pb)
{
N_current_c->value=pb->value;
if (N_before_c)
{
N_before_c->next=pb;
}
N_before_c=N_current_c;
pb=pb->next;
N_current_c=(struct NODE *)calloc(1,sizeof(NODE));
}
N_before_c->next=NULL;
free((void *)N_current_c);
printf("Print the value of linktable C:\n");
pc=LK_c;
//print linktable c
while (pc)
{
printf("%d\n",pc->value);
pc=pc->next;
}
return 0;
}
[解决办法]
最简单的就是把链表A的尾指向链表B的头,接下来怎么操作应该就看自己的需求了