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

调不出来!

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

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

void CreateList(dLinkList *headPtr, dLinkList *tailPtr);
void InsertElem(dLinkList *headPtr, int index, int newelem);
int DeleteElem(dLinkList *headPtr, int index);
int SearchElem(dLinkList headPtr, int key);
void VisitList(dLinkList headPtr);
void DestroyList(dLinkList *headPtr);

int main(void)
{
int index, elem;
dLinkList newhead = NULL, newtail = NULL;
CreateList(&newhead, &newtail);
VisitList(newhead);

InsertElem(&newhead, 4, 9);
VisitList(newhead);

elem = DeleteElem(&newhead, 6);
index = SearchElem(newhead, 4);

VisitList(newhead);
printf("elem = %d\n", elem);
printf("index = %d\n", index);

DestroyList(&newhead);
return 0;
}

void CreateList(dLinkList *headPtr, dLinkList *tailPtr)
{
dLinkList newPtr, newelem;

printf("Enter new elem:\n");
scanf("%d", &newelem);

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

newPtr -> elem = newelem;
if (*headPtr == NULL)
{
*headPtr = newPtr;
*tailPtr = newPtr;
}
else
{
(*tailPtr) -> next = newPtr;
newPtr -> prior = *tailPtr;
*tailPtr = newPtr;
newPtr -> next = NULL;
}
printf("Enter new elem:\n");
scanf("%d", &newelem);
}
if (newPtr != NULL)
newPtr -> next = NULL;
}


void InsertElem(dLinkList *headPtr, int index, int newelem)
{
int pos = 0;
dLinkList newPtr, prev = NULL, cur = NULL;
newPtr = (dLinkList)malloc(sizeof(dListNode));

newPtr -> elem = newelem;
prev = *headPtr;
while (prev && pos < index - 1)
{
prev = prev -> next;
pos ++;
}

prev -> next = cur;

newPtr -> next = prev -> next;
newPtr -> prior = cur -> prior;
prev -> next = newPtr;
cur -> prior = newPtr;


}

int DeleteElem(dLinkList *headPtr, int index)
{
int pos, delelem;
dLinkList prev = NULL, cur = NULL;
prev = *headPtr;
while (prev && pos < index - 1)
{
prev = prev -> next;
pos ++;
}

prev -> next = cur;

prev -> next = cur -> next;
cur -> next -> prior = cur -> prior;

delelem = cur -> elem;
free(cur);
cur = NULL;
return delelem;
}

int SearchElem(dLinkList headPtr, int key)
{
int index = 0;
dLinkList prev;
prev = headPtr;
while (prev && prev -> elem != key)
{
prev = prev -> next;
index ++;
}


return index;
}

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

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

*headPtr = NULL;
}

搜索更多相关主题的帖子: include 
2006-05-24 21:24
论坛
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:1372
专家分:0
注 册:2006-3-27
收藏
得分:0 
错误太多,看的头都大了,你在看看双向的,创建的不对

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

知道了.

奋斗改变一切!!
2006-05-25 09:15
论坛
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:1372
专家分:0
注 册:2006-3-27
收藏
得分:0 
我把程序发到数据结构了

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

好不容易搞定,谢谢老大!

奋斗改变一切!!
2006-05-25 11:51
快速回复:[求助]错误!
数据加载中...
 
   



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

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