用VS2008 Debug一个链表出现的问题

chater 2009-01-07 01:22:09
我用VS2008编译<C程序设计教程>上的一个例子时出现的问题:
源代码如下:

#include<stdio.h>
#include<stdlib.h>

struct listNode {
char data;
struct listNode* nextPtr;
};

typedef struct listNode LISTNODE;
typedef LISTNODE* LISTNODEPTR;

void insert(LISTNODEPTR* ,char);
char delete(LISTNODEPTR *,char);
int isEmpty(LISTNODEPTR);
void printList(LISTNODEPTR);
void instructions(void);

int main()
{
LISTNODEPTR startPtr=NULL;
int choice;
char item;

instructions();
printf("?");
scanf("%d",&choice);

while(choice!=3)
{
switch (choice){
case 1:
printf("Enter a character:");
scanf("\n %c",&item);
insert(&startPtr,item);
printList(startPtr);
break;
case 2:
if (!isEmpty(startPtr)){
printf("Enter character to be delete:");
scanf("\n %c",&item);

if (delete(&startPtr,item)) {
printf("%c delete. \n",item);
printList(startPtr);
}
else
printf("%c not found .\n \n",item);
}
else
printf("List is empty. \n \n");
break;
default:
printf("Invalid choice. \n \n");
instructions();
break;
}


printf("?");
scanf("%d",&choice);

}

printf("End of run. \n");

return 0;
}

void instructions(void)
{
printf("Enter your choice: \n"
"1 to insert an element into the list. \n"
"2 to delete an element from the list. \n"
"3 to end. \n");
}

void insert (LISTNODEPTR* sPtr,char value)
{
LISTNODEPTR newPtr,previousPtr,currentPtr;
newPtr=malloc(sizeof(LISTNODEPTR));

if (newPtr!=NULL)
{
newPtr->data=value;
newPtr->nextPtr=NULL;

previousPtr=NULL;
currentPtr=*sPtr;

while (currentPtr!=NULL && value>currentPtr->data)
{
previousPtr=currentPtr;
currentPtr=currentPtr->nextPtr;
}

if (previousPtr==NULL)
{
newPtr->nextPtr=*sPtr;
*sPtr=newPtr;
}
else
{
previousPtr->nextPtr=newPtr;
newPtr->nextPtr=currentPtr;
}

}
else{

printf("%c not insert.No memory avaliable. \n",value);
}

}

void printList(LISTNODEPTR currPtr)
{
if (currPtr==NULL)
{
printf("List is empty. \n\n");
}
else
{
printf("The list is: \n");
while (currPtr!=NULL)
{
printf("%c-->",currPtr->data);
currPtr=currPtr->nextPtr;
}

printf("NULL \n \n");
}

}


int isEmpty(LISTNODEPTR currPtr)
{
return currPtr==NULL;
}


char delete(LISTNODEPTR * sPtr,char value)
{
LISTNODEPTR previousPtr,currentPtr,tempPtr;

if (value==(*sPtr)->data)
{
tempPtr=*sPtr;
*sPtr=(*sPtr)->nextPtr;
free(tempPtr);
return value;
}
else
{
previousPtr=*sPtr;
currentPtr=(*sPtr)->nextPtr;

while (currentPtr->nextPtr!=NULL && currentPtr->data!=value)
{
previousPtr=currentPtr;
currentPtr=currentPtr->nextPtr;
}

if (currentPtr!=NULL)
{
tempPtr=currentPtr;
previousPtr->nextPtr=currentPtr->nextPtr;
free(tempPtr);
return value;
}
}

return "\0";
}


在Debug时在free(tempPtr)这句代码时,出现了错误:


如果点“忽略”,也能得到正确的结果。请大家帮我看一下,为什么free出现这样的错误,谢谢。
...全文
135 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
elegant87 2009-01-09
  • 打赏
  • 举报
回复
这个问题我也经常遇到,从逻辑上看是没有问题的!
期待!交流一下: QQ: 627686595
elegant87 2009-01-09
  • 打赏
  • 举报
回复
程序逻辑上没有问题的!我在Dev编译器上能够运行成功!不知什么原因!
期待高手的解答!
我发现在插入的时候有个小问题!
你是按照由小到大插入的!


void insert (LISTNODEPTR* sPtr,char value)
{
LISTNODEPTR newPtr,previousPtr,currentPtr;
newPtr=(listNode*)malloc(sizeof(LISTNODEPTR)); //注意malloc的用法

if (newPtr)
{
newPtr->data=value;
newPtr->nextPtr=NULL;

previousPtr=NULL;
currentPtr=*sPtr; //第一个节点

while (currentPtr!=NULL && value>currentPtr->data)
{
previousPtr=currentPtr;
currentPtr=currentPtr->nextPtr;
}

if (*sPtr==NULL)
{
newPtr->nextPtr=*sPtr;
*sPtr=newPtr;
}
else
{
if(previousPtr) //若previousPtrr!=NULL时
{
previousPtr->nextPtr=newPtr;
newPtr->nextPtr=currentPtr;
}
else //若preiousPtr==NULL时 在前面插入
{
newPtr->nextPtr=(*sPtr);
*sPtr=newPtr;
}
}

}
else
{
printf("%c not insert.No memory avaliable. \n",value);
}

}

nicholas100 2009-01-08
  • 打赏
  • 举报
回复
fanyuanwaifdl 2009-01-08
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 hqin6 的回复:]
C/C++ code#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>

struct listNode {
char data;
struct listNode* nextPtr;
};

typedef struct listNode LISTNODE;
typedef LISTNODE* LISTNODEPTR;

void insert(LISTNODEPTR* ,char);
char myDelete(LISTNODEPTR *,char);
int isEmpty(LISTNODEPTR);
void printList(LISTNODEPTR);
void instructions(void);

int main()
{ …
[/Quote]up
chater 2009-01-07
  • 打赏
  • 举报
回复
对啊,我现在就是想知道崩掉的原因和解决办法。
太乙 2009-01-07
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 chater 的回复:]
我把4楼的粘过去,还是出现同样的现象。
[/Quote]




Enter your choice:
1 to insert an element into the list.
2 to delete an element from the list.
3 to end.
?1
Enter a character:3
The list is:
3-->NULL

?1
Enter a character:2
The list is:
2-->3-->NULL

?1
Enter a character:5
The list is:
2-->3-->5-->NULL

?2
Enter character to be delete:2



我没改你的逻辑,就是delete的逻辑!

你自己看看,如果按照上的输入,会崩掉!!

单步跟踪试试!
chater 2009-01-07
  • 打赏
  • 举报
回复
我把4楼的粘过去,还是出现同样的现象。
xiaoyisnail 2009-01-07
  • 打赏
  • 举报
回复
up一下
4楼没错啊
chater 2009-01-07
  • 打赏
  • 举报
回复
谢谢各位,特别是hqin6

可是我照你的总结修改后,还是出现同样的错误。

帅得不敢出门 2009-01-07
  • 打赏
  • 举报
回复
命名冲突问题
icc编译:
5 error(s), 1 warning(s)
太乙 2009-01-07
  • 打赏
  • 举报
回复


1.delete函数的 冲突问题
2.返回值的问题,应该是'\0'字符,而不是"\0"字符串
3.在newPtr = (LISTNODEPTR)malloc(sizeof(LISTNODEPTR)); 要进行强制转换!
4.逻辑方面,在delete一个不存在的值时,会有bug!

太乙 2009-01-07
  • 打赏
  • 举报
回复
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>

struct listNode {
char data;
struct listNode* nextPtr;
};

typedef struct listNode LISTNODE;
typedef LISTNODE* LISTNODEPTR;

void insert(LISTNODEPTR* ,char);
char myDelete(LISTNODEPTR *,char);
int isEmpty(LISTNODEPTR);
void printList(LISTNODEPTR);
void instructions(void);

int main()
{
LISTNODEPTR startPtr=NULL;
int choice;
char item;

instructions();
printf("?");
scanf("%d",&choice);

while(choice!=3)
{
switch (choice){
case 1:
printf("Enter a character:");
scanf("\n %c",&item);
insert(&startPtr,item);
printList(startPtr);
break;
case 2:
if (!isEmpty(startPtr)){
printf("Enter character to be delete:");
scanf("\n %c",&item);

if (myDelete(&startPtr,item)) {
printf("%c delete. \n",item);
printList(startPtr);
}
else
printf("%c not found .\n \n",item);
}
else
printf("List is empty. \n \n");
break;
default:
printf("Invalid choice. \n \n");
instructions();
break;
}


printf("?");
scanf("%d",&choice);

}

printf("End of run. \n");

return 0;
}

void instructions(void)
{
printf("Enter your choice: \n"
"1 to insert an element into the list. \n"
"2 to delete an element from the list. \n"
"3 to end. \n");
}

void insert (LISTNODEPTR* sPtr,char value)
{
LISTNODEPTR newPtr,previousPtr,currentPtr;
newPtr = (LISTNODEPTR)malloc(sizeof(LISTNODEPTR));

if (newPtr!=NULL)
{
newPtr->data=value;
newPtr->nextPtr=NULL;

previousPtr=NULL;
currentPtr=*sPtr;

while (currentPtr!=NULL && value>currentPtr->data)
{
previousPtr=currentPtr;
currentPtr=currentPtr->nextPtr;
}

if (previousPtr==NULL)
{
newPtr->nextPtr=*sPtr;
*sPtr=newPtr;
}
else
{
previousPtr->nextPtr=newPtr;
newPtr->nextPtr=currentPtr;
}

}
else{

printf("%c not insert.No memory avaliable. \n",value);
}

}

void printList(LISTNODEPTR currPtr)
{
if (currPtr==NULL)
{
printf("List is empty. \n\n");
}
else
{
printf("The list is: \n");
while (currPtr!=NULL)
{
printf("%c-->",currPtr->data);
currPtr=currPtr->nextPtr;
}

printf("NULL \n \n");
}

}


int isEmpty(LISTNODEPTR currPtr)
{
return currPtr==NULL;
}


char myDelete(LISTNODEPTR * sPtr,char value)
{
LISTNODEPTR previousPtr,currentPtr,tempPtr;

if (value==(*sPtr)->data)
{
tempPtr=*sPtr;
*sPtr=(*sPtr)->nextPtr;
free(tempPtr);
return value;
}
else
{
previousPtr=*sPtr;
currentPtr=(*sPtr)->nextPtr;

while (currentPtr->nextPtr!=NULL && currentPtr->data!=value)
{
previousPtr=currentPtr;
currentPtr=currentPtr->nextPtr;
}

if (currentPtr!=NULL)
{
tempPtr=currentPtr;
previousPtr->nextPtr=currentPtr->nextPtr;
free(tempPtr);
return value;
}
}

return '\0';
}

太乙 2009-01-07
  • 打赏
  • 举报
回复
吧你 的函数delete改名!!

和系统的冲突了!
yellowhwb 2009-01-07
  • 打赏
  • 举报
回复
delete在c++中是关键字,最好不要用,避免麻烦的事!
Delete函数中,最后是return '\0'; 或者return 0;
yellowhwb 2009-01-07
  • 打赏
  • 举报
回复

#include <stdio.h>
#include <stdlib.h>

struct listNode {
char data;
struct listNode* nextPtr;
};

typedef struct listNode LISTNODE;
typedef LISTNODE* LISTNODEPTR;

void insert(LISTNODEPTR* ,char);
char Delete(LISTNODEPTR *,char);
int isEmpty(LISTNODEPTR);
void printList(LISTNODEPTR);
void instructions(void);

int main()
{
LISTNODEPTR startPtr=NULL;
int choice;
char item;

instructions();
printf("?");
scanf("%d",&choice);

while(choice!=3)
{
switch (choice){
case 1:
printf("Enter a character:");
scanf("\n %c",&item);
insert(&startPtr,item);
printList(startPtr);
break;
case 2:
if (!isEmpty(startPtr)){
printf("Enter character to be delete:");
scanf("\n %c",&item);

if (Delete(&startPtr,item)) {
printf("%c delete. \n",item);
printList(startPtr);
}
else
printf("%c not found .\n \n",item);
}
else
printf("List is empty. \n \n");
break;
default:
printf("Invalid choice. \n \n");
instructions();
break;
}


printf("?");
scanf("%d",&choice);

}

printf("End of run. \n");

return 0;
}

void instructions(void)
{
printf("Enter your choice: \n"
"1 to insert an element into the list. \n"
"2 to delete an element from the list. \n"
"3 to end. \n");
}

void insert (LISTNODEPTR* sPtr,char value)
{
LISTNODEPTR newPtr,previousPtr,currentPtr;
newPtr=(LISTNODEPTR)malloc(sizeof(LISTNODEPTR));

if (newPtr!=NULL)
{
newPtr->data=value;
newPtr->nextPtr=NULL;

previousPtr=NULL;
currentPtr=*sPtr;

while (currentPtr!=NULL && value>currentPtr->data)
{
previousPtr=currentPtr;
currentPtr=currentPtr->nextPtr;
}

if (previousPtr==NULL)
{
newPtr->nextPtr=*sPtr;
*sPtr=newPtr;
}
else
{
previousPtr->nextPtr=newPtr;
newPtr->nextPtr=currentPtr;
}

}
else{

printf("%c not insert.No memory avaliable. \n",value);
}

}

void printList(LISTNODEPTR currPtr)
{
if (currPtr==NULL)
{
printf("List is empty. \n\n");
}
else
{
printf("The list is: \n");
while (currPtr!=NULL)
{
printf("%c-->",currPtr->data);
currPtr=currPtr->nextPtr;
}

printf("NULL \n \n");
}

}


int isEmpty(LISTNODEPTR currPtr)
{
return currPtr==NULL;
}


char Delete(LISTNODEPTR * sPtr,char value)
{
LISTNODEPTR previousPtr,currentPtr,tempPtr;

if (value==(*sPtr)->data)
{
tempPtr=*sPtr;
*sPtr=(*sPtr)->nextPtr;
free(tempPtr);
return value;
}
else
{
previousPtr=*sPtr;
currentPtr=(*sPtr)->nextPtr;

while (currentPtr->nextPtr!=NULL && currentPtr->data!=value)
{
previousPtr=currentPtr;
currentPtr=currentPtr->nextPtr;
}

if (currentPtr!=NULL)
{
tempPtr=currentPtr;
previousPtr->nextPtr=currentPtr->nextPtr;
free(tempPtr);
return value;
}
}

return 0;
}

69,336

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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