hi all.
I have a question need your all help.
1.what is the problem for my delete function,I can't delete the list that i select(but no error in that case)
2.how to build the search function with that prototype?
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
struct ProductNode{
char code[5];
char description[30];
double price;
ProductNode *Next;
};
typedef ProductNode *Productptr;
void Display(Productptr );
Productptr Search(Productptr, char []);
void Delete(Productptr *, char []);
void Insert(Productptr *, ProductNode);
main(){
Productptr PhoneList,temp;
ProductNode p;
int sel;
char id[5];
PhoneList = NULL;
while (sel != 5){
system("cls");
cout<<"\t1. Add Item."<<endl;
cout<<"\t2. Add Delete."<<endl;
cout<<"\t3. Display Item."<<endl;
cout<<"\t4. Search."<<endl;
cout<<"\t5. Exit"<<endl;
cout<<"\t? : ";
cin>>sel;
system("cls");
switch (sel){
case 1: cout<<"Item Code :";
cin>>p.code ;
cout<<"Description : ";
cin>>p.description;
cout<<"Price : RM ";
cin>>p.price;
Insert(&PhoneList, p);
break;
case 2: cout<<"Item Code :";
cin>>id ;
Delete(&PhoneList, id);
break;
case 3: Display(PhoneList);
break;
case 4: cout<<"Enter item's Code : ";
cin>>id;
temp = Search(PhoneList, id);
if (temp == NULL)
cout<<"Error : Item not Found."<<endl;
else{
cout<<"Code\tDescription\tPrice\n";
// cout<<"====\t===========\t=====\n";
// cout<<temp->code<<"\t"
// <<temp->description<<"\t\t"
// <<temp->price<<endl;
// }
system("pause");
}
}
return 0;
}
void Insert(Productptr *h, ProductNode p){
Productptr newNode, pre, cur;
bool found = 0;
newNode = new ProductNode;
if (newNode == NULL){
cout<<"Error : Insertion Failed.";
}else {
*newNode=p;
newNode->Next = NULL;
if(*h==NULL){
*h = newNode;
}else{
pre=NULL;
cur=*h;
while(cur!=NULL) {
if (strcmp(cur->description, p.description )<0) {
pre=cur;
cur=cur->Next;
}
else
break; }
if(pre==NULL){
newNode->Next = *h;
*h = newNode;
}
else{
newNode->Next = cur;
pre->Next = newNode;
}
}
}
}
void Delete(Productptr *r, char id[5])
{
Productptr *temp;
Productptr *temp2;
bool found;
if(r==NULL)
cout<<"Cannot delete from and empty list."<<endl;
else
{
temp=r;
found= false;
while(temp!=NULL && !found)
if (strcmp((*temp)->code,id)<=0)
found=true;
else
{
temp2=temp;
(*temp)=(*temp)->Next;
}
if(temp==NULL)
cout<<"Item to be deleted is not in the list."<<endl;
else
if(strcmp((*temp)->code,id)==0)
{
if(r==temp)
{
(*r)=(*r)->Next;
delete temp;
}
else
{
(*temp2)->Next=(*temp)->Next;
delete temp;
}
}
else
cout<<"Item to be deleted not in list."<<endl;
}
void Display(Productptr t)
{
while(t!= NULL)
{
cout<<t->code<<" "<<t->description<<" "<<t->price<<endl;
t=t->Next;
}
cout<<"End of the list"<<endl;
system("pause");
}