C++ 两个链表合并为第三个链表,并按顺序输出(求大虾指点!)

flyingfish 2012-03-27 09:21:53
#include <stdio.h>
#include <stdlib.h>

typedef struct LNode
{
char data;
struct LNode *next;
}Lnode,*LinkList;

void CreateList(LinkList &L,int n) {
L = (LinkList)malloc(sizeof(LNode));
LinkList p;
L->next = NULL;
for(int i = n; i >0; --i) {
p = (LinkList)malloc(sizeof(LNode));
scanf("%d",&p->data);
p->next = L->next; L->next = p;
}
}

void sortlist(LinkList &L1,LinkList &L2,LinkList &L3)
{
LinkList p1,p2,p3;
p1 = L1->next;
p2 = L2->next;
L3 = p3 = L1;
p3->next=NULL;
while(p1 && p2)
{
if(p1->data <= p2->data){
//p3->next = p1;
p3 = p1;
p1 = p1->next;

}
else
{
//p3->next = p2;
p3 = p2;
p2 = p2->next;

}
//p3->next = p1?p1:p2;

p3->next = L1->next;
L1->next=p3;
}

while(p1)
{
p3=p1;
p1=p1->next;
p3->next=L1->next;
L1->next=p3;
}
while(p2)
{
p3=p2;
p2=p2->next;
p3->next=L1->next;
L1->next=p3;
}

//return L3;
//free(L2);
}

int main()
{
LinkList p,q,l1,l2,l3,z;

CreateList(l1,3);
p = l1->next;
while(p)
{
printf("%d ",p->data);
p=p->next;
}
printf("\n");

CreateList(l2,5);
q = l2->next;
while(q)
{
printf("%d ",q->data);
q=q->next;
}
printf("\n");

sortlist(l1,l2,l3);

z = l3->next;
while(z)
{
printf("%d ",z->data);
z=z->next;
}

return 0;
}

为什么我这个没排序?
...全文
488 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_22580867 2014-10-27
  • 打赏
  • 举报
回复
#include<stdlib.h> #include<stdio.h> #include<conio.h> #include<malloc.h> #define L sizeof(struct Node) struct Node //结构体 { long int number; struct Node *next; }; struct Node *create(int a) //链表创建函数 { int n; struct Node *p1, *p2, *head; head = NULL; n = 0; p2 = p1 = (struct Node *) malloc(L); //分配内存 scanf("%ld", &p1->number); while (a) //录入链表信息 { n = n + 1; if (n == 1) head = p1; else p2->next = p1; p2 = p1; p1 = (struct Node *) malloc(L); if (a != 1) //分配内存 scanf("%ld", &p1->number); a--; //控制输入的个数 } p2->next = NULL; return (head); } //链表创建函数结束 void print(struct Node *head) //输出函数 { struct Node *p; p = head; printf("数字:\n"); if (head != NULL) do //循环实现输出 { printf("%ld", p->number); printf(" "); p = p->next; } while (p != NULL); printf("\n"); } //链表的交叉合并算法 struct Node * inter_link(struct Node * chain1, int a, struct Node * chain2, int b) { int temp; struct Node *head, *p1, *p2, *pos; /*判断a,b大小并合并*/ if (a >= b) { head = p1 = chain1; p2 = chain2; } else/*b>a*/ { head = p1 = chain2; p2 = chain1; temp = a, a = b, b = temp; /*交换a和b*/ } /*下面把p1的每个元素插在p2相应元素之前,p1长a,p2长b*/ pos = head; /*此时pos指向p1中的第一个元素*/ while (p2 != NULL) {//漂亮,蛇形插入 p1 = p1->next; pos->next = p2; pos = p2; p2 = p2->next; pos->next = p1; pos = p1; } return head; } //对合并好的链表进行排序 void InsertSort(struct Node *p, int m)//排序函数 { int i, j, t; struct Node *k; k = p; for (i = 0; i < m - 1; i++) { for (j = 0; j < m - i - 1; j++) { if (p->number > (p->next)->number) { t = p->number; p->number = (p->next)->number; (p->next)->number = t; } p = p->next; } p = k; } } //主函数 int main() //main函数 { struct Node *p1, *p2; int a; int b; int h; printf("请输入第一个链表:\n"); printf("\n输入链表的长度a:\n"); scanf("%d", &a); printf("请输入链表数据:"); p1 = create(a); printf("\n你刚才输入的第一个链表信息:\n "); print(p1); printf("\n 请输入第二个链表:\n"); printf("\n输入链表的长度b:\n"); scanf("%d", &b); printf("请输入链表数据:"); p2 = create(b); printf("\n你刚才输入的第二个链表的信息:\n"); print(p2); p1 = inter_link(p1, a, p2, b); h = a + b; printf("\n合并后的链表\n:"); print(p1); InsertSort(p1, h); printf("\n排序后的链表:\n"); print(p1); return 0; } 能不能给我加一下注释
muyi66 2012-03-28
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 的回复:]

是有序的,比如 链表1: 1 3 5 链表2:2 4 6 7 9

两链表经过合并,最后要输出:1 2 3 4 5 6 7 9 或者 逆序的:9 7 6 5 4 3 2 1
[/Quote]既然有序,用5楼的合并算法得到的就是排好序的输出了。按原序的。

while里面会反复比较两个链表,把小的一个先拿出来并推进拿走了节点的那个表。出了while后就会把剩下的一个表彻底合并。
Kevin_qing 2012-03-28
  • 打赏
  • 举报
回复
比较栈顶,然后pop小的push到new表

flyingfish 2012-03-28
  • 打赏
  • 举报
回复
是有序的,比如 链表1: 1 3 5 链表2:2 4 6 7 9

两链表经过合并,最后要输出:1 2 3 4 5 6 7 9 或者 逆序的:9 7 6 5 4 3 2 1
muyi66 2012-03-28
  • 打赏
  • 举报
回复
?你是说你原来的两个链表是无序的?
flyingfish 2012-03-28
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]

C/C++ code
void sortlist(LinkList &L1,LinkList &L2,LinkList &L3)
{
LinkList p1,p2,p3;
p1 = L1->next;
p2 = L2->next;
L3 = p3 = L1;

while(p1 && p2)
{
……
[/Quote]
这样是按输入顺序输出,还是没能排序。
muyi66 2012-03-28
  • 打赏
  • 举报
回复
void sortlist(LinkList &L1,LinkList &L2,LinkList &L3)
{
LinkList p1,p2,p3;
p1 = L1->next;
p2 = L2->next;
L3 = p3 = L1;

while(p1 && p2)
{
if(p1->data <= p2->data)
{
p3->next = p1;
p3 = p1;
p1 = p1->next;
}
else
{
p3->next = p2;
p3 = p2;
p2 = p2->next;
}
}

p3->next=p1?p1:p2;
}
flyingfish 2012-03-28
  • 打赏
  • 举报
回复

void sortlist(LinkList &L1,LinkList &L2,LinkList &L3)
{
LinkList p1,p2,p3;
p1 = L1->next;
p2 = L2->next;
L3 = p3 = L1;
p3->next=NULL;
while(p1 && p2)
{
if(p1->data <= p2->data){
//p3->next = p1;
p3 = p1;
p1 = p1->next;

}
else
{
//p3->next = p2;
p3 = p2;
p2 = p2->next;

}
//p3->next = p1?p1:p2;

p3->next = L1->next;
L1->next=p3;
}

while(p1)
{
p3=p1;
p1=p1->next;
p3->next=L1->next;
L1->next=p3;
}
while(p2)
{
p3=p2;
p2=p2->next;
p3->next=L1->next;
L1->next=p3;
}

//return L3;
//free(L2);
}


//就是这个链表排序的子函数有问题,到底错在哪呢?
flyingfish 2012-03-28
  • 打赏
  • 举报
回复
void sortlist(LinkList &L1,LinkList &L2,LinkList &L3)
{
LinkList p1,p2,p3;
p1 = L1->next;
p2 = L2->next;
L3 = p3 = L1;
p3->next=NULL;
while(p1 && p2)
{
if(p1->data <= p2->data){
//p3->next = p1;
p3 = p1;
p1 = p1->next;

}
else
{
//p3->next = p2;
p3 = p2;
p2 = p2->next;

}
//p3->next = p1?p1:p2;

p3->next = L1->next;
L1->next=p3;
}

while(p1)
{
p3=p1;
p1=p1->next;
p3->next=L1->next;
L1->next=p3;
}
while(p2)
{
p3=p2;
p2=p2->next;
p3->next=L1->next;
L1->next=p3;
}

//return L3;
//free(L2);
}


//就是这个链表排序的子函数有问题,到底错在哪呢?
giant1st 2012-03-27
  • 打赏
  • 举报
回复
合并两个链表的考点 就在于: 引入一个dummy node,这样保证新链表的链表尾即为 插入点。

更多请参考: http://www.geeksforgeeks.org/archives/3622


Merge two sorted linked lists
May 27, 2010

Write a SortedMerge() function that takes two lists, each of which is sorted in increasing order, and merges the two together into one list which is in increasing order. SortedMerge() should return the new list. The new list should be made by splicing together the nodes of the first two lists.

For example if the first linked list a is 5->10->15 and the other linked list b is 2->3->20, then SortedMerge() should return a pointer to the head node of the merged list 2->3->5->10->15->20.

There are many cases to deal with: either ‘a’ or ‘b’ may be empty, during processing either ‘a’ or ‘b’ may run out first, and finally there's the problem of starting the result list empty, and building it up while going through ‘a’ and ‘b’.

Method 1 (Using Dummy Nodes)
The strategy here uses a temporary dummy node as the start of the result list. The pointer Tail always points to the last node in the result list, so appending new nodes is easy.
The dummy node gives tail something to point to initially when the result list is empty. This dummy node is efficient, since it is only temporary, and it is allocated in the stack. The loop proceeds, removing one node from either ‘a’ or ‘b’, and adding it to tail. When
we are done, the result is in dummy.next.

/*Program to alternatively split a linked list into two halves */
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>

/* Link list node */
struct node
{
int data;
struct node* next;
};
/* 说明: 一般的链表数据结构都是有数据元素的,也有全是指针的,称作链。。。*/
/* pull off the front node of the source and put it in dest */
void MoveNode(struct node** destRef, struct node** sourceRef);

/* Takes two lists sorted in increasing order, and splices their nodes together to make one big sorted list which is returned. */
struct node* SortedMerge(struct node* a, struct node* b)
{
/* a dummy first node to hang the result on */
struct node dummy;

/* tail points to the last result node */
struct node* tail = &dummy;

/* so tail->next is the place to add new nodes
to the result. */
dummy.next = NULL;
while(1)
{
if(a == NULL)
{
/* if either list runs out, use the other list */
tail->next = b;
break;
}
else if (b == NULL)
{
tail->next = a;
break;
}
if (a->data <= b->data)
{
MoveNode(&(tail->next), &a);
}
else
{
MoveNode(&(tail->next), &b);
}
tail = tail->next;
}
return(dummy.next);
}

/* UTILITY FUNCTIONS */
/*MoveNode() function takes the node from the front of the source, and move it to the front of the dest.
It is an error to call this with the source list empty.

Before calling MoveNode():
source == {1, 2, 3}
dest == {1, 2, 3}

Affter calling MoveNode():
source == {2, 3}
dest == {1, 1, 2, 3}
*/
void MoveNode(struct node** destRef, struct node** sourceRef)
{
/* the front source node */
struct node* newNode = *sourceRef;
assert(newNode != NULL);

/* Advance the source pointer */
*sourceRef = newNode->next;

/* Link the old dest off the new node */
newNode->next = *destRef;

/* Move dest to point to the new node */
*destRef = newNode;
}

/* Function to insert a node at the beginging of the linked list */
void push(struct node** head_ref, int new_data)
{
/* allocate node */
struct node* new_node =
(struct node*) malloc(sizeof(struct node));

/* put in the data */
new_node->data = new_data;

/* link the old list off the new node */
new_node->next = (*head_ref);

/* move the head to point to the new node */
(*head_ref) = new_node;
}

/* Function to print nodes in a given linked list */
void printList(struct node *node)
{
while(node!=NULL)
{
printf("%d ", node->data);
node = node->next;
}
}

/* Drier program to test above functions*/
int main()
{
/* Start with the empty list */
struct node* res = NULL;
struct node* a = NULL;
struct node* b = NULL;

/* Let us create two sorted linked lists to test the functions
Created lists shall be a: 5->10->15, b: 2->3->20 */
push(&a, 15);
push(&a, 10);
push(&a, 5);

push(&b, 20);
push(&b, 3);
push(&b, 2);

/* Remove duplicates from linked list */
res = SortedMerge(a, b);

printf("\n Merged Linked List is: \n");
printList(res);

getchar();
return 0;
}


Method 2 (Using Local References)
This solution is structurally very similar to the above, but it avoids using a dummy node. Instead, it maintains a struct node** pointer, lastPtrRef, that always points to the last pointer of the result list. This solves the same case that the dummy node did — dealing with the result list when it is empty.

If you are trying to build up a list at its tail, either the dummy node or the struct node** “reference” strategy can be used (see Section 1 for details).

struct node* SortedMerge(struct node* a, struct node* b)
{
struct node* result = NULL;

/* point to the last result pointer */
struct node** lastPtrRef = &result;

while(1)
{
if (a == NULL)
{
*lastPtrRef = b;
break;
}
else if (b==NULL)
{
*lastPtrRef = a;
break;
}
if(a->data <= b->data)
{
MoveNode(lastPtrRef, &a);
}
else
{
MoveNode(lastPtrRef, &b);
}

/* tricky: advance to point to the next ".next" field */
lastPtrRef = &((*lastPtrRef)->next);
}
return(result);
}

Method 3 (Using Recursion)
Merge is one of those nice recursive problems where the recursive solution code is much cleaner than the iterative code. You probably wouldn’t want to use the recursive version for production code however, because it will use stack space which is proportional to the length of the lists.

struct node* SortedMerge(struct node* a, struct node* b)
{
struct node* result = NULL;

/* Base cases */
if (a == NULL)
return(b);
else if (b==NULL)
return(a);

/* Pick either a or b, and recur */
if (a->data <= b->data)
{
result = a;
result->next = SortedMerge(a->next, b);
}
else
{
result = b;
result->next = SortedMerge(a, b->next);
}
return(result);
}
muyi66 2012-03-27
  • 打赏
  • 举报
回复
难道说学会用代码标签贴代码竟然也是一个非常困难的事情吗?

64,642

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧