| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 618 人关注过本帖
标题:[求助]链表合并!找错误!
只看楼主 加入收藏
独角龙
Rank: 1
等 级:新手上路
帖 子:221
专家分:0
注 册:2006-5-5
收藏
 问题点数:0 回复次数:4 
[求助]链表合并!找错误!

#include <stdio.h>
#include <malloc.h>

typedef struct node
{
int data;
struct node *prior, *next;
}*dLinkList, dListNode;

void CreateList(dLinkList *headPtr, dLinkList *tailPtr);
void Conatecate(dLinkList *headPtr1, dLinkList *tailPtr1, dLinkList *headPtr2, dLinkList *tailPtr2);
void VisitList(dLinkList headPtr, dLinkList tailPtr);
void DestroyList(dLinkList *headPtr, dLinkList *tailPtr);

int main(void)
{
dLinkList newhead1 = NULL, newtail1 = NULL;
dLinkList newhead2 = NULL, newtail2 = NULL;

CreateList(&newhead1, &newtail1);
VisitList(newhead1, newtail1);

CreateList(&newhead2, &newtail2);
VisitList(newhead2, newtail2);

Conatecate(&newhead1, &newtail2, &newhead2, &newtail2);

VisitList(newhead1, newtail1);

DestroyList(&newhead1, &newtail2);

return 0;
}

void CreateList(dLinkList *headPtr, dLinkList *tailPtr)
{
int newdata;
dLinkList newPtr = NULL;

printf("Enter one number:\n");
scanf("%d", &newdata);

while (newdata != 0)
{
newPtr = (dLinkList)malloc(sizeof(dListNode));
if (newPtr == NULL)
{
exit(1);
}

newPtr -> data = newdata;
newPtr -> next = NULL;

if (*headPtr == NULL)
{
newPtr -> next = *headPtr;
newPtr -> prior = *headPtr;
*headPtr = newPtr;
}
else
{
(*tailPtr) -> next = newPtr;
newPtr -> prior = *tailPtr;
}
newPtr -> next = NULL;
*tailPtr = newPtr;

printf("Enter one number:\n");
scanf("%d", &newdata);
}
}

void Conatecate(dLinkList *headPtr1, dLinkList *tailPtr1, dLinkList *headPtr2, dLinkList *tailPtr2)
{
(*tailPtr1) -> next = *headPtr2;
(*headPtr2) -> prior = *tailPtr1;
}

void VisitList(dLinkList headPtr, dLinkList tailPtr)
{
while(headPtr != NULL)
{
printf("%d ", headPtr -> data);
headPtr = headPtr -> next;
}
printf("\n");
}

void DestroyList(dLinkList *headPtr, dLinkList *tailPtr)
{
dLinkList temp;
while (*headPtr != NULL)
{
temp = *headPtr;
*headPtr = (*headPtr) -> next;
free(temp);
}
*headPtr = NULL;
*tailPtr = NULL;
}

搜索更多相关主题的帖子: 链表 
2006-05-27 11:47
独角龙
Rank: 1
等 级:新手上路
帖 子:221
专家分:0
注 册:2006-5-5
收藏
得分:0 


奋斗改变一切!!
2006-05-27 12:01
论坛
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:1372
专家分:0
注 册:2006-3-27
收藏
得分:0 

#include <stdio.h>
#include <malloc.h>

typedef struct node
{
int data;
struct node *prior, *next;
}*dLinkList, dListNode;

void CreateList(dLinkList *headPtr, dLinkList *tailPtr);
void Conatecate(dLinkList *tailPtr1, dLinkList headPtr2);
void VisitList(dLinkList headPtr);
void DestroyList(dLinkList *headPtr, dLinkList *tailPtr);

int main(void)
{
dLinkList newhead1 = NULL, newtail1 = NULL;
dLinkList newhead2 = NULL, newtail2 = NULL;

CreateList(&newhead1, &newtail1);

CreateList(&newhead2, &newtail2);

Conatecate(&newtail1, newhead2);

VisitList(newhead1);

DestroyList(&newhead1, &newtail2);

return 0;
}

void CreateList(dLinkList *headPtr, dLinkList *tailPtr)
{
int newdata;
dLinkList newPtr = NULL;

;

while (1)
{
printf("Enter one number:\n");
scanf("%d", &newdata);
if (newdata == 0)
{
break;
}
newPtr = (dLinkList)malloc(sizeof(dListNode));
if (newPtr == NULL)
{
exit(1);
}

newPtr -> data = newdata;
newPtr -> prior = NULL;
newPtr -> next = NULL;

if (*headPtr == NULL)
{
newPtr -> prior = *headPtr;
newPtr -> next = *headPtr;
*headPtr = newPtr;
}
else
{
(*tailPtr) -> next = newPtr;
newPtr -> prior = *tailPtr;
newPtr -> next = NULL;
}
*tailPtr = newPtr;

}
}

void Conatecate(dLinkList *tailPtr1, dLinkList headPtr2)
{
(*tailPtr1) -> next = headPtr2;

}

void VisitList(dLinkList headPtr)
{
while(headPtr != NULL)
{
printf("%d ", headPtr -> data);
headPtr = headPtr -> next;
}
printf("\n");
}

void DestroyList(dLinkList *headPtr, dLinkList *tailPtr)
{
dLinkList temp;
while (*headPtr != NULL)
{
temp = *headPtr;
*headPtr = (*headPtr) -> next;
free(temp);
}
*tailPtr = NULL;
}


日出东方,唯我不败! 做任何东西都是耐得住寂寞,任何一个行业要有十年以上的积累才能成为专家
2006-05-27 12:06
论坛
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:1372
专家分:0
注 册:2006-3-27
收藏
得分:0 
靠,你把参数传错了能连上吗?找10分钟连接函数没找错误

日出东方,唯我不败! 做任何东西都是耐得住寂寞,任何一个行业要有十年以上的积累才能成为专家
2006-05-27 12:07
独角龙
Rank: 1
等 级:新手上路
帖 子:221
专家分:0
注 册:2006-5-5
收藏
得分:0 

Conatecate(&newhead1, &newtail2, &newhead2, &newtail2);
Conatecate(&newhead1, &newtail, &newhead2, &newtail2);

2变1就行了

奋斗改变一切!!
2006-05-27 12:19
快速回复:[求助]链表合并!找错误!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017009 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved