C++ 实现的链表,就是无法实现输入,各位帮忙一下,谢谢了
C++ 实现的链表,就是无法实现输入,各位帮忙一下,多谢了!
#include"stdio.h"
#include"stdlib.h"
#include"iostream.h"
//Class LIST
class LIST
{
private:
typedef struct LISTER
{
char date;
struct LISTER *next;
}LISTNODER,*LISTPTR;
public:
LIST();
void ListInsert(char );
int ListDelete(char );
void printlist();
int Listempty();
private:
LISTPTR sPtr;
char value;
};
LIST::LIST()
{
sPtr=NULL;
}
void LIST::ListInsert(char value)
{
LISTPTR newPtr,currentPtr,previousPtr;
newPtr=new LISTNODER;
if(newPtr!=NULL)
{
newPtr->date=value;
newPtr->next=NULL;
previousPtr=NULL;
currentPtr=sPtr;
while((value>currentPtr->date)&&(currentPtr!=NULL))
{
previousPtr=currentPtr;
currentPtr=currentPtr->next;
}
if(previousPtr==NULL)
{
newPtr->next=sPtr; //
sPtr=newPtr;
}
else
{
previousPtr->next=newPtr;
newPtr->next=currentPtr;
}
}
else
{
cout<<"invaild memory!"<<endl;
}
}
int LIST::ListDelete(char value)
{
LISTPTR tempPtr,currentPtr,previousPtr;
if(!sPtr)
{
return 0;
}
if(value==sPtr->date)
{
tempPtr=sPtr;
sPtr=sPtr->next; //
delete tempPtr;
return 1;
}
else
{
previousPtr=sPtr;
currentPtr=sPtr->next;//
while((currentPtr->date!=value)&&(currentPtr!=NULL))
{
previousPtr=currentPtr;
currentPtr=currentPtr->next;
}
if(currentPtr!=NULL)
{
tempPtr=currentPtr;
previousPtr->next=currentPtr->next;
delete tempPtr;
return 1;
}
}
return 0;
}
int LIST::Listempty()
{
return sPtr==NULL;
}
void LIST::printlist()
{
LISTPTR temp;
temp=sPtr; //
if(sPtr==NULL)
{
cout<<"invaild List!"<<endl;
}
else
{
while(temp!=NULL)
{
cout<<temp->date<<"-->";
temp=temp->next;
}
cout<<"NULL"<<endl;
}
}
void main()
{
LIST bbb;
int choice;
char item;
cout << "?" ;
cin >> choice;
while(choice!=3){
switch(choice){
case 1: //插入某值入链表
cout << "Enter a character:";
cin >> item;
bbb.ListInsert(item);
bbb.printlist();
break;
case 2: //从链表删除某值
if(!bbb.Listempty())
{
printf("Enter character to be deleted:");
scanf("\n%c",&item);
if(bbb.ListDelete(item))
{
printf("%c deleted.\n",item);
bbb.printlist();
}
else
printf("%c not found.\n\n",item);
}
else
{
cout<<"list is empty!"<<endl;
}
break;
default:
printf("Invalid choice.\n\n");
break;
}
printf("?");
scanf("%d",&choice);
}
printf("End of run.\n");
}
------解决方案--------------------
#include"stdio.h"
#include"stdlib.h"
#include"iostream.h"
//Class LIST
class LIST
{
private:
typedef struct LISTER
{
char date;
struct LISTER *next;
}LISTNODER,*LISTPTR;
public:
LIST();
void ListInsert(char );
int ListDelete(char );
void printlist();
int Listempty();
private:
LISTPTR sPtr;
char value;
};
LIST::LIST()
{
sPtr=NULL;
}
void LIST::ListInsert(char value)
{
LISTPTR newPtr,currentPtr,previousPtr;
newPtr=new LISTNODER;
if(newPtr!=NULL)
{
newPtr->date=value;
newPtr->next=NULL;
previousPtr=NULL;
currentPtr=sPtr;
while((value>currentPtr->date)&&(currentPtr!=NULL))
{
previousPtr=currentPtr;
currentPtr=currentPtr->next;
}
if(previousPtr==NULL)
{
newPtr->next=sPtr; //
sPtr=newPtr;
}
else
{
previousPtr->next=newPtr;
newPtr->next=currentPtr;
}
}
else
{
cout<<"invaild memory!"<<endl;
}
}
int LIST::ListDelete(char value)
{
LISTPTR tempPtr,currentPtr,previousPtr;
if(!sPtr)
{
return 0;
}
if(value==sPtr->date)
{
tempPtr=sPtr;
sPtr=sPtr->next; //
delete tempPtr;
return 1;
}
else
{
previousPtr=sPtr;
currentPtr=sPtr->next;//
while((currentPtr->date!=value)&&(currentPtr!=NULL))
{
previousPtr=currentPtr;
currentPtr=currentPtr->next;
}
if(currentPtr!=NULL)
{
tempPtr=currentPtr;
previousPtr->next=currentPtr->next;
delete tempPtr;
return 1;
}
}
return 0;
}
int LIST::Listempty()
{
return sPtr==NULL;
}
void LIST::printlist()
{
LISTPTR temp;
temp=sPtr; //
if(sPtr==NULL)
{
cout<<"invaild List!"<<endl;
}
else
{
while(temp!=NULL)
{
cout<<temp->date<<"-->";
temp=temp->next;
}
cout<<"NULL"<<endl;
}
}
void main()
{
LIST bbb;
int choice;
char item;
cout << "?" ;
cin >> choice;
while(choice!=3){
switch(choice){
case 1: //插入某值入链表
cout << "Enter a character:";
cin >> item;
bbb.ListInsert(item);
bbb.printlist();
break;
case 2: //从链表删除某值
if(!bbb.Listempty())
{
printf("Enter character to be deleted:");
scanf("\n%c",&item);
if(bbb.ListDelete(item))
{
printf("%c deleted.\n",item);
bbb.printlist();
}
else
printf("%c not found.\n\n",item);
}
else
{
cout<<"list is empty!"<<endl;
}
break;
default:
printf("Invalid choice.\n\n");
break;
}
printf("?");
scanf("%d",&choice);
}
printf("End of run.\n");
}
------解决方案--------------------
- C/C++ code
void LIST::ListInsert(char value) { LISTPTR newPtr,currentPtr,previousPtr; newPtr=new LISTNODER; if(newPtr!=NULL) { newPtr-> date=value; newPtr-> next=NULL; previousPtr=NULL; currentPtr=sPtr; while((currentPtr!=NULL)&&(value> currentPtr-> date)) { //条件判断先后不对 previousPtr=currentPtr; currentPtr=currentPtr-> next; } if(previousPtr==NULL) { newPtr-> next=sPtr; // sPtr=newPtr; } else { previousPtr-> next=newPtr; newPtr-> next=currentPtr; } } else { cout <<"invaild memory!" <<endl; } }