69,336
社区成员
发帖
与我相关
我的任务
分享
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);
}
}
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的逻辑!
你自己看看,如果按照上的输入,会崩掉!!
单步跟踪试试!
1.delete函数的 冲突问题
2.返回值的问题,应该是'\0'字符,而不是"\0"字符串
3.在newPtr = (LISTNODEPTR)malloc(sizeof(LISTNODEPTR)); 要进行强制转换!
4.逻辑方面,在delete一个不存在的值时,会有bug!
#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';
}
#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;
}