Data structures using C, Stack is a data structure in which the objects are arranged in a non linear order. In stack, elements are added or deleted from only one end, i.e. top of the stack. In this program, we have implement the stack operations using linked list. Read more about C Programming Language .
Example of C Program to implement STACK operations using Linked Lists
#include<stdio.h>
struct stack
{
int info;
struct stack *next;
};
typedef struct stack node;
class stlink
{
node *start;
public:
stlink()
{
start=NULL;
}
void display(void);
void push(int);
int pop(void);
};
void stlink::push(int term)
{
node *p,*s;
s=start;
if(s==NULL||s!=NULL)
{
p=(node *)malloc(sizeof(node));
p->info=term;
p->next=s;
s=p;
}
start=s;
return;
}
void stlink::display(void)
{
node *temp;
if(start==NULL)
{
cout << endl<<"UNDERFLOEW";
}
temp=start;
while(temp!=NULL)
{
cout << endltemp=temp->next;
}
return;
}
int stlink::pop(void)
{
int term;
if(start==NULL)
{
cout<<"UNDERFLOW";
return(-1);
}
else
{
node *p;
term=start->info;
p=start;
free(start);
start=p->next;
return(term);
}
}
int main()
{
stlink s1;
int ch,temp;
do
{
clrscr();
cout<<"1->Pushn";
cout<<"2->Displayn";
cout<<"3->Popn";
cout<<"4->Exitn";
cout<<"Enter your choice:";
cin>>ch;
switch(ch)
{
case 1:
cout<<"Enter the term to push:";
cin>>temp;
s1.push(temp);
break;
case 2 :
cout << endl<<"Stack";
s1.display();
getch();
break;
case 3 :
temp=s1.pop();
if(temp!=-1)
cout<<"Popped term is " << temp;
getch();
break;
case 4 :
cout<<"Exiting";
getch();
break;
default:
cout<<"Invalid choice";
getch();
break;
}
}while(ch!=4);
return(0);
}