Sample code for Linked List
CC People :: Studies :: Programming
Page 1 of 1•
Sample code for Linked List
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;
}
Last edited by Lion on Mon Mar 24, 2008 12:55 am; edited 1 time in total
Re: Sample code for Linked List
This is the advanced Food List from above, with sorting of linked list in it. Try understand the code if you can... Even myself have the difficulty if you were to ask me to explain on how the code works..
- 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, *bfr;
};
Food *HEAD = NULL, *TAIL = NULL;
void AddFoodToHead(Food *fud)
{ if(TAIL==NULL)
TAIL = fud;
if(HEAD!=NULL)
{ fud->nxt = HEAD;
HEAD->bfr = fud;
}
HEAD = fud;
}
void AddFoodToTail(Food *fud)
{ if(HEAD==NULL)
HEAD = fud;
if(TAIL!=NULL)
{ TAIL->nxt = fud;
fud->bfr = TAIL;
}
TAIL = fud;
}
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
{ for(int i=1; temp!=NULL; i++, temp=temp->nxt)
{ if(i==x)
{ fud->nxt = temp;
fud->bfr = temp->bfr;
fud->bfr->nxt = fud;
temp->bfr = fud;
return;
}
}
return AddFoodToTail(fud);
}
}
Food::Food(string n,int p)
{ name = n;
price = p;
nxt = NULL;
bfr = NULL;
}
void PrintFoodList()
{ Food *temp = HEAD;
int x=1;
while(temp!=NULL)
{ cout<<x<<". "; x++;
temp->PrintPrice();
temp = temp->nxt;
}
}
void SortFood() //who the hell will imagine that the code for sorting will be this freaking long...
{ Food *now = HEAD, *check;
while(now!=NULL)
{ check = now;
while(check!=NULL)
{ if(check->price<now->price) //Swap!! After analize... there are a total of 8 cases that need to
{ if(now==HEAD && check==TAIL) //be taken care in the swap...
{ if(now->nxt == check)
{ now->bfr = check;
check->nxt = now;
}
else
{ now->bfr = check->bfr;
now->bfr->nxt = now;
check->nxt = now->nxt;
check->nxt->bfr = check;
}
now->nxt = NULL;
check->bfr = NULL;
HEAD = check;
TAIL = now;
}
else if(now==HEAD)
{ if(now->nxt == check)
{ now->nxt = check->nxt;
now->nxt->bfr = now;
now->bfr = check;
check->nxt = now;
}
else
{ now->bfr = check->bfr;
now->bfr->nxt = now;
Food *temp = check->nxt;
check->nxt = now->nxt;
check->nxt->bfr = check;
now->nxt = temp;
temp->bfr = now;
}
check->bfr = NULL;
HEAD=check;
}
else if(check==TAIL)
{ if(now->nxt == check)
{ check->bfr = now->bfr;
check->bfr->nxt = check;
check->nxt = now;
now->bfr = check;
}
else
{ check->nxt = now->nxt;
check->nxt->bfr = check;
Food *temp = now->bfr;
now->bfr = check->bfr;
now->bfr->nxt = now;
check->bfr = temp;
temp->nxt = check;
}
now->nxt = NULL;
TAIL=now;
}
else
{ if(now->nxt == check)
{ check->bfr = now->bfr;
check->bfr->nxt = check;
now->nxt = check->nxt;
now->nxt->bfr = now;
now->bfr = check;
check->nxt = now;
}
else
{ Food *left = now->bfr, *right = now->nxt;
now->bfr = check->bfr;
now->bfr->nxt = now;
now->nxt = check->nxt;
now->nxt->bfr = now;
check->bfr = left;
left->nxt = check;
check->nxt = right;
right->bfr = check;
}
}
now=check;
}
check=check->nxt;
}
now=now->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);
system("cls");
PrintFoodList();
cout<<endl<<"Sort out the food? (y/n): "; cin>>y;
if(y=='y'||y=='Y')
{ SortFood();
}
}
else
break;
}
while(true)
{ system("cls");
PrintFoodList();
cout<<endl<<"Delete 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;
HEAD->bfr = NULL;
delete temp;
}
else
{ for(int i=1; temp!=NULL; i++, temp=temp->nxt)
{ if(i==x)
{ temp->bfr->nxt = temp->nxt;
if(temp!=TAIL)
{ temp->nxt->bfr = temp->bfr;
}
else
{ TAIL = temp->bfr;
}
}
}
}
}
else
break;
}
cout<<endl<<"~end~"<<endl<<endl;
system("pause");
return 0;
}
Re: Sample code for Linked List
Yay, I've just optimized the sorting function above, here's the new source code, much easier to understand 
cheers~
cheers~
- 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, *bfr;
};
Food *HEAD = NULL, *TAIL = NULL;
void AddFoodToHead(Food *fud)
{ if(TAIL==NULL)
TAIL = fud;
if(HEAD!=NULL)
{ fud->nxt = HEAD;
HEAD->bfr = fud;
}
HEAD = fud;
}
void AddFoodToTail(Food *fud)
{ if(HEAD==NULL)
HEAD = fud;
if(TAIL!=NULL)
{ TAIL->nxt = fud;
fud->bfr = TAIL;
}
TAIL = fud;
}
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
{ for(int i=1; temp!=NULL; i++, temp=temp->nxt)
{ if(i==x)
{ fud->nxt = temp;
fud->bfr = temp->bfr;
fud->bfr->nxt = fud;
temp->bfr = fud;
return;
}
}
return AddFoodToTail(fud);
}
}
Food::Food(string n,int p)
{ name = n;
price = p;
nxt = NULL;
bfr = NULL;
}
void PrintFoodList()
{ Food *temp = HEAD;
int x=1;
while(temp!=NULL)
{ cout<<x<<". "; x++;
temp->PrintPrice();
temp = temp->nxt;
}
}
void SortFood()
{ Food *now = HEAD, *check;
while(now!=NULL)
{ check = now;
while(check!=NULL)
{ if(check->price<now->price)
{ Food *left = now->bfr;
Food *right = check->nxt;
if(left!=NULL)
{ left->nxt = check;
}
else
{ HEAD = check;
}
if(right!=NULL)
{ right->bfr = now;
}
else
{ TAIL = now;
}
if(now->nxt==check)
{ now->bfr = check;
now->nxt = right;
check->nxt = now;
check->bfr = left;
}
else
{ now->nxt->bfr = check;
check->bfr->nxt = now;
check->nxt = now->nxt;
now->bfr = check->bfr;
now->nxt = right;
check->bfr = left;
}
Food* temp = now;
now = check;
check = temp;
}
check=check->nxt;
}
now=now->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);
system("cls");
PrintFoodList();
cout<<endl<<"Sort out the food? (y/n): "; cin>>y;
if(y=='y'||y=='Y')
{ SortFood();
}
}
else
break;
}
while(true)
{ system("cls");
PrintFoodList();
cout<<endl<<"Delete 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;
HEAD->bfr = NULL;
delete temp;
}
else
{ for(int i=1; temp!=NULL; i++, temp=temp->nxt)
{ if(i==x)
{ temp->bfr->nxt = temp->nxt;
if(temp!=TAIL)
{ temp->nxt->bfr = temp->bfr;
}
else
{ TAIL = temp->bfr;
}
}
}
}
}
else
break;
}
cout<<endl<<"~end~"<<endl<<endl;
system("pause");
return 0;
}






