| Sample code for Linked ListSun Mar 23, 2008 7:17 pm by Lion Well just for those who still have NO IDEA about what linked list is going about, this is the source code for the food list I did in last tutorial, hope u can get something from it...
- Code:
#include<iostream>
#include<cstring>
using namespace std;
struct Food
{ string name;
int price;
void PrintPrice()
{ cout<<name<<"'s price is "<<price<<endl;
}
Food();
Food(string name, int price);
Food *nxt;
};
Food *HEAD = NULL, *TAIL = NULL;
void AddFoodToHead(Food *fud)
{ if(TAIL==NULL)
TAIL = fud;
if(HEAD!=NULL)
fud->nxt = HEAD;
else
fud->nxt = NULL;
HEAD = fud;
}
void AddFoodToTail(Food *fud)
{ if(HEAD==NULL)
HEAD = fud;
if(TAIL!=NULL)
TAIL->nxt = fud;
TAIL = fud;
fud->nxt = NULL;
}
void AddFoodToMiddle(Food *fud)
{ int x = 0;
cout<<"Selete which position of food to add: "; cin>>x;
Food *temp = HEAD;
if(x==1 || temp==NULL)
{ return AddFoodToHead(fud);
}
else
{ int counter = 1;
while(temp!=NULL)
{ counter++;
if(counter==x)
{ fud->nxt = temp->nxt;
temp->nxt = fud;
return;
}
temp=temp->nxt;
}
return AddFoodToTail(fud);
}
}
Food::Food(string n,int p)
{ name = n;
price = p;
}
void PrintFoodList()
{ Food *temp = HEAD;
int x=1;
while(temp!=NULL)
{ cout<<x<<". "; x++;
temp->PrintPrice();
temp = temp->nxt;
}
}
int main()
{ Food *fud;
fud = new Food("banana",4); AddFoodToTail(fud);
fud = new Food("durian",10); AddFoodToTail(fud);
fud = new Food("papaya",3); AddFoodToTail(fud);
fud = new Food("mango",2); AddFoodToTail(fud);
fud = new Food("CharKuiTiao",4); AddFoodToTail(fud);
fud = new Food("ChaoFan",3); AddFoodToTail(fud);
fud = new Food("Patayya",4); AddFoodToTail(fud);
int x = 0; char y = 'n';
while(true)
{ system("cls");
PrintFoodList();
cout<<endl<<"Add a food? (y/n): "; cin>>y;
if(y=='y'||y=='Y')
{ string a; int b;
cout<<"input food name "; cin>>a;
cout<<"input food price "; cin>>b;
fud = new Food(a,b);
cout<<endl<<endl<<"Do you wish to: "<<endl;
cout<<"(1) Add a food to the first of list"<<endl;
cout<<"(2) Add a food to the end of the list"<<endl;
cout<<"(3) Add a food to the middle of the list"<<endl;
cin>>b;
if(b==1)
AddFoodToHead(fud);
else if(b==2)
AddFoodToTail(fud);
else
AddFoodToMiddle(fud);
}
else
break;
}
while(true)
{ system("cls");
PrintFoodList();
cout<<endl<<"Detele a food? (y/n): "; cin>>y;
if(y=='y'||y=='Y')
{ cout<<"Selete which food to delete: "; cin>>x;
Food *temp = HEAD;
if(x==1)
{ HEAD = HEAD->nxt;
delete temp;
}
else
{ int counter = 1;
while(temp!=NULL)
{ counter++;
if(counter==x)
{ Food *killing;
killing = temp->nxt;
if(temp->nxt!=NULL && temp->nxt->nxt!=NULL)
{ temp->nxt = temp->nxt->nxt;
}
else
{ temp->nxt = NULL;
}
delete killing;
}
temp=temp->nxt;
}
}
}
else
break;
}
cout<<endl<<endl;
system("pause");
return 0;
}
Comments: 4 | Who is Online ? In total there is 1 user online :: 0 Registered, 0 Hidden and 1 Guest Registered Users: None [ View the whole list ] Most users ever online was 21 on Tue Aug 12, 2008 11:59 pm Statistics We have 27 registered users The newest registered user is alanOur users have posted a total of 248 messages in 27 subjects |