Custom Search
Showing posts with label push. Show all posts
Showing posts with label push. Show all posts

Saturday, 16 June 2012

Stack Using Linked List in C++


#include
#include
struct node
{
int data;
node *next;
};
class stack
{
node *start,*last,*head;
public:
void push();
void pop();
void display();
stack()
{
start=last=NULL;
}
};
void stack::push()
{

head=new node;
cout<<"Enter the data to be inserted :";
cin>>head->data;
if(start==NULL)
{
head->next=NULL;
start=last=head;
}
else
{
head->next=start;
start=head;
}
}
void stack::pop()
{
node *temp;
if(start==NULL)
{
cout<<"\nStack is empty";
}
else
{
temp=start;
cout<<"\nDeleted item is : "<data;
if(start==last)
{
start=last=NULL;
}
else
{
start=start->next;
delete temp;
}
}
}
void stack::display()
{
node *temp;
temp=start;
if(temp==NULL)
{
cout<<"\nStack is empty :";
}
else
{
cout<<"\nThe elements are : ";
while(temp!=NULL)
{
cout<<"   "<data<<"   ";
temp=temp->next;
}
}
}
void main()
{
stack s;
int opt;
char ch;
clrscr();
do
{
cout<<"\nMENU";
cout<<"\n1.PUSH()\n2.POP()\n3.DISPLAY()\nEnter your choice:";
cin>>opt;
switch(opt)
{
case 1:s.push();break;
case 2:s.pop();break;
case 3:s.display();break;
default:cout<<"Invalid choice\n";
}
cout<<"\nDo you want to continue?(Y/N)  :";
cin>>ch;
}while(ch=='y'||ch=='Y');
getch();
}
Read more

Thursday, 14 June 2012

Stack implementation Using C++


Author: Sanif S S 
Site: theeduzone.blogspot.com 
Email: sanifss@gmail.com


#include<stdio.h>
#include<conio.h>

#define max 3
class stack
 {
   int  item[max];
   int top;
  public:
     void push();
     void pop();
     void display();
     stack()
     {top=-1;}

   };
void stack::push()
  {
    int value;
     if(top==max-1)
       {
       cout<<"Stack is full insertion not possible\n";
}
      else
{
cout<<"\nEnter the value to be inserted\n";
cin>>value;
top=top+1;
item[top]=value;
}
   }
void stack::pop()
   {
    int value;
    if(top==-1)
     {
       cout<<"Stack empty\n ";
       }
    else
    {
      value=item[top];
      cout<<"deleted item is"<
      top=top-1;
      }
    }
 void stack::display()
   {
   if(top==-1)
     {
      cout<<"Stack is empty\n" ;
      }
   else
    {
     cout<<"Elements in stack are\n";
      for(int i=top;i>=0;i--)
{
cout<
}
       getch();
    }
     }
void main()
{
 int choice;
 char ch;
 stack s;
 clrscr();
 do
   {
     cout<<"MENU\n";
     cout<<"1.Insertion\n" ;
     cout<<"2.Deletion\n";
     cout<<"3.Display\n";
     cout<<"Enter your choice\n";
     cin>>choice;
     switch(choice)
      {
      case 1 :s.push();break;
      case 2 :s.pop();break;
      case 3 :s.display();break;
      default:cout<<"Invalid choice\n";

      }
     cout<<"\nDo you want to continue(yes=y,no=n)";
     cin>> ch;
    }while(ch=='Y'||ch=='y');
     getch();

   }

Read more

LinkWithin

Related Posts Plugin for WordPress, Blogger...