Social Icons

banner image

stack programs

Program to Implement a Stack using Linked List

This C Program implement a stack using linked list. Stack is a type of queue that in practice is implemented as an area of memory that holds all local variables and parameters used by any function, and remembers the order in which functions are called so that function returns occur correctly. Each time a function is called, its local variables and parameters are “pushed onto” the stack. When the function returns,these locals and parameters are “popped.” Because of this, the size of a program’s stack fluctuates constantly as the program is running, but it has some maximum size. stack is as a last in, first out (LIFO) abstract data type and linear data structure. Linked list is a data structure consisting of a group of nodes which together represent a sequence. Here we need to apply the application of linkedlist to perform basic operations of stack.
#include<iostream>
#include<conio.h>
using namespace std;
struct node
{
int info;
node *link;
}*top=NULL,*nptr,*ptr;
void inset()
{
nptr=new node;
cin>>nptr->info;
if(top==NULL)
{
top=nptr;
nptr->link=NULL;
}
else
{
nptr->link=top;
top=nptr;
}
}
    
void traverse()
{
cout<<"The values are:-\n";
ptr=top;
while(ptr!=NULL)
{
cout<<ptr->info<<endl;
ptr=ptr->link;
}
}
void del()
{
top=top->link;
}
int main()
{
int n,i,b;
    int a;
cout<<"Enter the no of element you want to enter:- ";
cin>>n;
for(i=1;i<=n;i++)
{
void insert();
}
traverse();
cout<<"PRESS 1 to delete the element else PRESS 2:- ";
cin>>a;
switch(a)
{
case 1: cout<<"Are you sure want to  delete PRESS 1 (OK) or 2 (CANCEL):- ";
         cin>>b;
 switch(b)
 {
  case 1: cout<<"Element DELETED !!!\n";
         del();
         traverse();
         break;
  case 2: cout<<"Canceled!!!";
       break;        
 }
 break;
case 2: cout<<"Thanks";
       break;  
}
}



stack programs stack programs Reviewed by Shobhit Goel on November 07, 2015 Rating: 5

No comments:

Airtel Hackaton 2017

Powered by Blogger.