Queue Implemented With LinkList


#include "stdafx.h"
#include <iostream>
using namespace std;

class node
{
     
private:
      int val;
      node *next;
public:
      void setvalue(int x)
      {
            val=x;

      }
      int getvalue()
      {
            return val;
      }
      void setnext(node *a)
      {
            next=a;
      }
      node * getnext()
      {
            return next;

      }
};
class Queue
{
private:
     
      node* rear;
      node* front;
      int size;
public:
      Queue()
      {
            rear=NULL;
            size=0;
            front=NULL;

      }
      void push(int temp)
     
      {
           
      node *n=new node();
      n->setvalue(temp);
      n->setnext(NULL);
      if(size==0)
      {
           
            front=rear=n;
      }

     
     
      else
      {
           
            rear->setnext(n);
            rear=n;
      }
     

           
            size++;
           
      }
     
      int pop()
      {    
            node *p=NULL;
     
            int val;
            if(size==0)
            {
                  cout<<"deletion is not possible"<<endl;
                  return 0;
           
            }
            else if(size==1)
            {
            p=front;
            front=NULL;
            rear=NULL;

            }
            else
            {
            p=front;
            front=front->getnext();
            p->setnext(NULL);
           
            }
            val=p->getvalue();
            delete p;
            size--;
            cout<<"Element deleted with value "<<val<<endl;
            return val;
      }
     
      int asize()
      {
            return size;
      }
      void display()
      {
            node *p;
            p=front;
            if(front==NULL)
                  cout<<"No elements to display"<<endl;
            else
            while(p)
            {
            cout<<p->getvalue()<<endl;
            p=p->getnext();
            }
      }
           
     
};

int _tmain(int argc, _TCHAR* argv[])

{
      Queue q1;
      int x,ch,i;
      do
      {
            cout<<"\n  Menu"<<endl;
            cout<<"1.Add Element"<<endl;
            cout<<"2.Delete Element"<<endl;
            cout<<"3.Display Elements"<<endl;
            cout<<"4.Exit"<<endl;
           
            cout<<"\nEnter your choice ";
            cin>>ch;
            switch(ch)
            {
                  case 1:
                        {
                       
                        cout<<"\nEnter number of elements to push ";
                        cin>>i;
                        for(int j=0;j<i;j++)
                        {
                        cout<<"Enter element value ";
                        cin>>x;
                        q1.push(x);
                       
                        }
                        break;
                        }
                  case 2:
                        q1.pop();
                        break;
                  case 3:
                        q1.display();
                        break;

            }
      }
      while(ch<4);
}


Comments

Popular posts from this blog

Check if ViewBag is null or doesn't exist

Using Progress Bar In C#

Jquery serer side datatables in asp.net