پیاده سازی پشته با لیست پیوندی :
#include <iostream>
#include <conio>
struct stack
{
int value;
struct stack *top;
};
void insert(struct stack *,int);
int remove(stack *,bool*);
void print(stack *);
struct stack sample;
int main()
{
char ch;
bool flag=0;
int item,d;
while(1)
{
clrscr();
gotoxy(45,23);
cout<<"*** Writting By Karma ***";
gotoxy(1,1);
cout<<"\n\nWhat do you want to do?\n\n";
cout<<"1-insert\n2-remove\n3-Print\n4-Exit\n\n";
cout<<"What is your selection:";
cin>>ch;
switch(ch)
{
case'1':
clrscr();
cout<<"\n\nPlease neter a number to add:";
cin>>item;
insert(&sample,item);
break;
case'2':
d=remove(&sample,&flag);
if(flag==1)
{
clrscr();
cout<<"\n\nYour deleted number is:";
cout<<d;
getch();
}
break;
case'3':
print(&sample);
getch();
break;
case'4':
return 0;
}
}
getch();
return 0;
}
//***********************************************
void insert(struct stack *p,int item)
{
struct stack*temp;
temp=new struct stack;
temp->value=item;
if(p==0)
p=temp;
else
{
temp->top=p->top;
p->top=temp;
}
}
//***********************************************
int remove(stack *p,bool*flag)
{
int item;
struct stack*temp;
temp=p->top;
if(p==0||p->top==0)
{
clrscr();
cout<<"\nVoid deletion";
getch();
*flag=0;
}
else
{
temp=p->top;
item=temp->value;
p->top=temp->top;
*flag=1;
}
delete temp;
return item;
}
//***********************************************
void print(struct stack*p)
{
int i=0;
struct stack*temp;
temp=p;
while(temp!=0)
{
if(i!=0)
cout<<" <- "<<temp->value;
temp=temp->top;
i++;
}
}