快救救我吧!
我自己参照书上遍了一个链表演示程序,在VC6.0中已通过编译无错误,但运行一会就出问题,谁能帮我解决一下不胜感激,提示错误信息如下:
LIST 在 0167:004012e9 的模块
LIST.EXE 中导致无效页错误。
Registers:
EAX=00000000 CS=0167 EIP=004012e9 EFLGS=00010202
EBX=00550000 SS=016f ESP=0065fd34 EBP=0065fd8c
ECX=00000061 DS=016f ESI=8196e1a0 FS=5ef7
EDX=00000000 ES=016f EDI=0065fd8c GS=0000
Bytes at CS:EIP:
8b 48 04 89 4d f8 eb db 83 7d f8 00 75 15 8b 55
Stack dump:
0065fdf8 8196e1a0 00550000 cccccccc cccccccc cccccccc cccccccc cccccccc cccccccc cccccccc cccccccc cccccccc cccccccc cccccccc cccccccc cccccccc 。
程序原代码如下:
/*操作和维护链表*/
#include "stdio.h"
#include "stdlib.h"
struct listnode
{
char data;
struct listnode* nextptr;
};
typedef struct listnode LISTNODE;
typedef LISTNODE* LISTNODEPTR;
void printlist(LISTNODEPTR);
void instructions(void);
void insert (LISTNODEPTR *,char);
int isempty(LISTNODEPTR);
char delete(LISTNODEPTR *,char);
main()
{
LISTNODEPTR startptr=NULL;
int choice;
char item;
instructions();/*显示菜单*/
printf("?");
scanf("%d",&choice);
while(choice!=3){
switch(choice){
case 1:
printf("Enter a char:");
scanf("\n%c",&item);
insert (&startptr,item);
printlist(startptr);
break;
case 2:
if(!isempty(startptr)){
printf("Enter char to be deleted:");
scanf("/n%c",&item);
if(delete(&startptr,item)){
printf("%c deleted.\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()
{
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\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=previousptr;
previousptr=previousptr->nextptr;
}
if(previousptr==NULL){
newptr->nextptr=*sptr;
*sptr=newptr;
}
else{
previousptr->nextptr=newptr;
newptr->nextptr=currentptr;
}
}
else
printf("%c not inserted.No memory available.\n",value);
}
/*删除一个链表元素*/
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!=NULL && currentptr->data!=value){
previousptr=currentptr;
currentptr=currentptr->nextptr;
}
if(currentptr!=NULL){
tempptr=currentptr;
previousptr->nextptr=currentptr->nextptr;
free(tempptr);
return value;
}
}
return '\0';
}
/*判断链表是否为空*/
int isempty(LISTNODEPTR sptr)
{
return sptr==NULL;
}
/*打印*/
void printlist(LISTNODEPTR currentptr)
{
if(currentptr==NULL)
printf("List is empty.\n\n");
else{
printf("The list is:\n");
while(currentptr!=NULL){
printf("%c-->",currentptr->data);
currentptr=currentptr->nextptr;
}
printf("NULL\n\n");
}
}