两单链表合并有关问题
两单链表合并问题
头文件:
[code=C/C++][/code]
#ifndef LNODE_H_
#define LNODE_H_
typedef int ElemType;
typedef struct LNode
{
ElemType elem;
struct LNode *next;
}node, *LinkNode;
int CreateList(LinkNode L);
int GetLeagth(LinkNode L);
int DisplayList(LinkNode L, int x);
int SortList(LinkNode L, int x);
int InsertList(LinkNode l);
LNode* FindNode(LinkNode l);
int CombinaList(LinkNode la, LinkNode lb, LinkNode lc);
#endif
CreateList.cpp:
[code=C/C++][/code]#include <stdio.h>
#include <malloc.h>
#include "LNode.h"
int CreateList(LinkNode L)
{
int i = 0;
LinkNode tail;
LinkNode head;
int x = 0;
int k = 0;
tail = L;
printf("how many number to you want to input?");
scanf("%d", &x);
for(i = 0;i < x;i++)
{
printf("please input the number:");
head = (LinkNode)malloc(sizeof(LNode));
scanf("%d",&k);
head->elem = k;
tail->next = head;
tail = head;
}
tail->next = NULL;
return 0;
}
GetLeagth.cpp
[code=C/C++][/code]
#include <stdio.h>
#include "LNode.h"
int GetLeagth(LinkNode L)
{
int i = 0;
LinkNode node;
node = L;
while(node->next != NULL)
{
i++;
node = (node)->next;
}
return i;
}
CombinaList.cpp
[code=C/C++][/code]
#include <stdio.h>
#include <malloc.h>
#include "LNode.h"
int CombinaList(LinkNode la, LinkNode lb, LinkNode lc)//合并la与lb,生成新链表lc
{
lc = la;
LinkNode node = la;
//node = lc;
while(node->next != NULL)
{
node = node->next;
}
node->next = lb;
//lc = la;
return 0;
}
DiaplayList.cpp
[code=C/C++][/code]
#include <stdio.h>
#include "LNode.h"
int DisplayList(LinkNode L, int x)
{
int i = 0;
LNode *node;
node = L;
printf("what your input is:");
for(i = 0;i < x;i++)
{
printf("%d", node->next->elem); //带头结点,需从下一个开始打印
node = node->next;
}
printf("\n");
return 0;
}
main.cpp
[code=C/C++][/code]
#include <stdio.h>
#include <malloc.h>
#include "LNode.h"
int main()
{
int i = 0;
int h = 0;
LinkNode la = (LinkNode)malloc(sizeof(node)); //初始化一个头结点
la->next = NULL;
LinkNode lb = (LinkNode)malloc(sizeof(node)); //初始化一个头结点
lb->next = NULL;
LinkNode lc = (LinkNode)malloc(sizeof(node)); //初始化一个头结点
lc->next = NULL;
CreateList(la);
CreateList(lb);
i = GetLeagth(la);
h = GetLeagth(lb);
DisplayList(la,i);
DisplayList(lb,h);
CombinaList(la, lb, lc);
h = GetLeagth(lc);
printf("%d", h);
DisplayList(lc,h);
scanf("%d", &i);
return 0;
}
问题出现在CombinaList中,为何函数运行完成后,la的值就被改变,lc的值未变(返回值是初始化时的值)?合并失败
------解决方案--------------------
给你改好了,主要有下面几个问题:
1,CombinaList中lc没有使用引用,函数返回时,实参不会改变
2,lb链表的头结点没有删除
潜在的问题:
1,链表函数都没有检查输入出错的情况,可能会在调用的时候一不小心就莫名其妙的死了。
其他一些在注释里面有详细说明。
另外最好不要使用这么多的文件,初学者一个就够了。
头文件:
[code=C/C++][/code]
#ifndef LNODE_H_
#define LNODE_H_
typedef int ElemType;
typedef struct LNode
{
ElemType elem;
struct LNode *next;
}node, *LinkNode;
int CreateList(LinkNode L);
int GetLeagth(LinkNode L);
int DisplayList(LinkNode L, int x);
int SortList(LinkNode L, int x);
int InsertList(LinkNode l);
LNode* FindNode(LinkNode l);
int CombinaList(LinkNode la, LinkNode lb, LinkNode lc);
#endif
CreateList.cpp:
[code=C/C++][/code]#include <stdio.h>
#include <malloc.h>
#include "LNode.h"
int CreateList(LinkNode L)
{
int i = 0;
LinkNode tail;
LinkNode head;
int x = 0;
int k = 0;
tail = L;
printf("how many number to you want to input?");
scanf("%d", &x);
for(i = 0;i < x;i++)
{
printf("please input the number:");
head = (LinkNode)malloc(sizeof(LNode));
scanf("%d",&k);
head->elem = k;
tail->next = head;
tail = head;
}
tail->next = NULL;
return 0;
}
GetLeagth.cpp
[code=C/C++][/code]
#include <stdio.h>
#include "LNode.h"
int GetLeagth(LinkNode L)
{
int i = 0;
LinkNode node;
node = L;
while(node->next != NULL)
{
i++;
node = (node)->next;
}
return i;
}
CombinaList.cpp
[code=C/C++][/code]
#include <stdio.h>
#include <malloc.h>
#include "LNode.h"
int CombinaList(LinkNode la, LinkNode lb, LinkNode lc)//合并la与lb,生成新链表lc
{
lc = la;
LinkNode node = la;
//node = lc;
while(node->next != NULL)
{
node = node->next;
}
node->next = lb;
//lc = la;
return 0;
}
DiaplayList.cpp
[code=C/C++][/code]
#include <stdio.h>
#include "LNode.h"
int DisplayList(LinkNode L, int x)
{
int i = 0;
LNode *node;
node = L;
printf("what your input is:");
for(i = 0;i < x;i++)
{
printf("%d", node->next->elem); //带头结点,需从下一个开始打印
node = node->next;
}
printf("\n");
return 0;
}
main.cpp
[code=C/C++][/code]
#include <stdio.h>
#include <malloc.h>
#include "LNode.h"
int main()
{
int i = 0;
int h = 0;
LinkNode la = (LinkNode)malloc(sizeof(node)); //初始化一个头结点
la->next = NULL;
LinkNode lb = (LinkNode)malloc(sizeof(node)); //初始化一个头结点
lb->next = NULL;
LinkNode lc = (LinkNode)malloc(sizeof(node)); //初始化一个头结点
lc->next = NULL;
CreateList(la);
CreateList(lb);
i = GetLeagth(la);
h = GetLeagth(lb);
DisplayList(la,i);
DisplayList(lb,h);
CombinaList(la, lb, lc);
h = GetLeagth(lc);
printf("%d", h);
DisplayList(lc,h);
scanf("%d", &i);
return 0;
}
问题出现在CombinaList中,为何函数运行完成后,la的值就被改变,lc的值未变(返回值是初始化时的值)?合并失败
------解决方案--------------------
给你改好了,主要有下面几个问题:
1,CombinaList中lc没有使用引用,函数返回时,实参不会改变
2,lb链表的头结点没有删除
潜在的问题:
1,链表函数都没有检查输入出错的情况,可能会在调用的时候一不小心就莫名其妙的死了。
其他一些在注释里面有详细说明。
另外最好不要使用这么多的文件,初学者一个就够了。
- C/C++ code
#include <iostream> #include <string> #include <stdio.h> #include <malloc.h> using namespace std; typedef int ElemType; typedef struct LNode { ElemType elem; struct LNode *next; }node, *LinkNode; int CreateList(LinkNode L); int GetLeagth(LinkNode L); int DisplayList(LinkNode L, int x); int SortList(LinkNode L, int x); int InsertList(LinkNode l); LNode* FindNode(LinkNode l); void CombinaList(LinkNode la, LinkNode lb, LinkNode &lc); int CreateList(LinkNode L) { int i = 0; LinkNode tail; LinkNode head; int x = 0; int k = 0; tail = L; printf("how many number to you want to input?\n"); //有换行的话用起来会舒服一些 scanf("%d", &x); for(i = 0;i < x;i++) { printf("please input the number:\n"); head = (LinkNode)malloc(sizeof(LNode)); scanf("%d",&k); head->elem = k; tail->next = head; tail = head; } tail->next = NULL; return 0; } int GetLeagth(LinkNode L) { int i = 0; LinkNode node; node = L; if( node == NULL ) return 0; //node 是一个指针,提前指针中的元素之前一定要检查指针是否为空指针 while(node->next != NULL) { i++; node = (node)->next; } return i; } void CombinaList(LinkNode la, LinkNode lb, LinkNode &lc)//合并la与lb,生成新链表lc lc必须使用引用,否则不会改变实参 { if( la == NULL ) { //使用指针前,要考虑到指针为空的情况 lc = lb; return; } if( lb == NULL ) { lc = la; return; } lc = la; LinkNode node = la; //node = lc; while(node->next != NULL) { node = node->next; } //node->next = lb; //注意lb是头结点,头结点不是元素结点,所以应该去掉头结点 node->next = lb->next; free(lb); //释放空间 lb = NULL; //return 0; //返回值没有意义 } int DisplayList(LinkNode L, int x) { if( !L ){ //编写链表的函数,第一点就是考虑为空的情况 cout << " The link table is NULL" <<endl; return -1; } int i = 0; LNode *node; node = L; printf("what your input is:"); for(i = 0;i < x;i++) { printf("%4d ", node->next->elem); //带头结点,需从下一个开始打印, 结点之间有空格较好 node = node->next; } printf("\n"); return 0; } int main() { int i = 0; int h = 0; LinkNode la = (LinkNode)malloc(sizeof(node)); //初始化一个头结点 la->next = NULL; LinkNode lb = (LinkNode)malloc(sizeof(node)); //初始化一个头结点 lb->next = NULL; LinkNode lc = (LinkNode)malloc(sizeof(node)); //初始化一个头结点 lc->next = NULL; CreateList(la); CreateList(lb); i = GetLeagth(la); h = GetLeagth(lb); DisplayList(la,i); DisplayList(lb,h); CombinaList(la, lb, lc); h = GetLeagth(lc); printf("The length of c is: %d\n", h); //最好有说明 DisplayList(lc,h); scanf("%d", &i); return 0; }