Friday 22 March 2013

Stack


Stack : Stack is a linear data structure in which insertion and deletion of elements takes place only one end known as TOP.


The stack is sometimes also called as LIFO List (Last-In-First-Out), because the last element pushed
into the stack is the first one to be popped out.

Example Code is given here :-


#include<iostream.h>
#include<conio.h>
struct node
{
  int data;
  node *next;
};
class stack
{
      node *top;
public :
      stack()
      { top=NULL;}
       void push();
       void pop();
       void display();
      ~stack();
};
void stack::push()
{
      node *temp;
      temp=new node;
      cout<<"Enter data :";
      cin>>temp->data;
      temp->next=top;
      top=temp;
}
void stack::pop()
{
      if(top!=NULL)
      {
            node *temp=top;
            top=top->next;
            cout<<temp->data<<"deleted";
            delete temp;
      }
      else
            cout<<"Stack empty";
}
void stack::display()
{
      node *temp=top;
      while(temp!=NULL)
      {
        cout<<temp->data<<" ";
        temp=temp->next;
      }
}
stack::~stack()
{
      while(top!=NULL)
      {
            node *temp=top;
            top=top->next;
            delete temp;
      }
}
void main()
{
      stack st;
      char ch;
      do
      {
            cout<<"stack options\nP for push \nO for Pop \nD for Display \nQ for quit";
            cin>>ch;
            switch(ch)
            {
                  case 'P': st.push();break;
                  case 'O': st.pop();break;
                  case 'D': st.display();break;
            }
      }while(ch!='Q');
}

0 comments:

Queue Implementation

Queue :- Queue is a linear data structure in which  insertion of new element take place at one end known as Rear or Back end and deletion of existing element takes place at the other end known as Front end.


Queues are sometimes also called  FIFO List (First-In-First-Out).


Queue Implementation 


#include<iostream.h>
#include<conio.h>
struct node
{
      int data;
      node *next;
};
class queue
{
      node *rear,*front;
public:
      queue()
      { rear=NULL;front=NULL;}
      void qinsert();
      void qdelete();
      void qdisplay();
      ~queue();
};
void queue::qinsert()
{
      node *temp;
      temp=new node;
      cout<<"Data :";
      cin>>temp->data;
      temp->next=NULL;
      if(rear==NULL)
      {
            rear=temp;
            front=temp;
      }
      else
      {
            rear->next=temp;
            rear=temp;
      }
}
void queue::qdelete()
{
      if(front!=NULL)
      {
            node *temp=front;
            cout<<front->data<<"deleted \n";
            front=front->next;
            delete temp;
            if(front==NULL)
                  rear=NULL;
      }
      else
            cout<<"Queue Empty..";
}
void queue::qdisplay()
{
      node *temp=front;
      while(temp!=NULL)
      {
            cout<<temp->data<<endl;
            temp=temp->next;
      }
}
queue::~queue()
{
      while(front!=NULL)
      {
            node *temp=front;
            front=front->next;
            delete temp;
      }
}
void main()
{
      queue obj; char ch;
      do
      {
            cout<< "i. insert\nd. Delete\ns. Display\n q. quit ";
            cin>>ch;
            switch(ch)
            {
                  case 'i' : obj.qinsert();break;
                  case 'd' : obj.qdelete();break;
                  case 's' : obj.qdisplay();
            }
      }while(ch!='q');
}


0 comments: