Sunday, 9 November 2014

Stack & Queue implimentation (DEV-C++)

#include<iostream>

#include<conio.h>
#include<windows.h>
//for system("cls");

using namespace std;
struct link{
    int data;
    link *next;
};
class S_Q{               
//Stack & Queue class
    private:
        link *first, *rear;
    public:
    S_Q()
        {
            first=NULL;
            rear=NULL;
        }
    void enqueue_push(int v) 
//Push & Enqueue Function will be same
    {
            if(first==NULL)
        {   
            first=new link;
            first->data=v;
            first->next=NULL;
            rear=first;
        }
        else
        {
            link *temp;
            temp=new link;
            temp->data=v;
            temp->next=NULL;
            rear->next=temp;
            rear=temp;
        }
    }
    void dequeue()
        {
            if(first==NULL)
            cout<<"\tQueue is an EMPTY..!";
            else
            {
            link *temp;
            temp=first;
            first=first->next;
            temp->next=NULL;
            delete temp;
            }
        }
    void pop()
        {
            if(first==NULL)
            cout<<"\tStack is an EMPTY..!";
            else
            {
            link *pre,*temp;
            pre=first;
            temp=pre;
            while(pre->next!=NULL)
            {
                temp=pre;
                pre=pre->next;
            }
            temp->next=NULL;
            delete pre;
            cout<<"\n\t--Poped Sucessfully--";
            }
        }
    void show()
    {
        if(first==NULL)
        cout<<"\tStack/Queue is an EMPTY..!";
        link *temp;
        temp=first;
        while(temp!=NULL)
        {
            cout<<"\n\t"<<temp->data<<"\n\t______\n"<<endl;
            temp=temp->next;
        }
    }
   
};
   
int main()
{
    S_Q sq;
    int a;
    start:
        system("cls");
    cout<<"\n\t\t\t***MAIN MENU***\n\n";
    cout<<"\t\t1- for Stack Implimentation\n";
    cout<<"\t\t2- for Queue Implimentation\n";
    cout<<"\t\t0- EXIT\n\n";
    cout<<"\tEnter the number = ";
    cin>>a;
    switch(a)
    {
        case 1:
            stack:
            system("cls");
            int b;
            cout<<"\n\t\t\t***Stack Implimentation***\n\n";
            cout<<"\t\t1- for Push(); \n";
            cout<<"\t\t2- for Pop();\n";
            cout<<"\t\t3- for Show\n";
            cout<<"\t\t0- Back to Main Menu\n\n";
            cout<<"\tEnter the number = ";
            cin>>b;
            switch(b)
            {
                case 1:
                   
                    int c;
                    cout<<"\tEnter the PUSH value = ";
                    cin>>c;
                    sq.enqueue_push(c);
                    cout<<"\n\t--Pushed Sucessfully--";
                    getch();
                    goto stack;
                    break;
                case 2:
                    sq.pop();
                   
                    getch();
                    goto stack;
                    break;
                case 3:
                    sq.show();
                    getch();
                    goto stack;
                case 0:
                    goto start;
                default:
                    cout<<"--Enter invalid number--";
                    goto stack;
            }break;
           
        case 2:
            queue:
            system("cls");
            int d;
           
            cout<<"\n\t\t\t***Queue Implimentation***\n\n";
            cout<<"\t\t1- for Enqueue(); \n";
            cout<<"\t\t2- for Dequeue();\n";
            cout<<"\t\t3- for Show\n";
            cout<<"\t\t0- Back to Main Menu\n\n";
            cout<<"\tEnter the number = ";
            cin>>d;           
            switch(d)
            {
                case 1:
                   
                    int c;
                    cout<<"\tEnter the En-Queue value = ";
                    cin>>c;
                    sq.enqueue_push(c);
                    cout<<"\n\t--Enqueue Sucessfully--";
                    getch();
                    goto queue;
                    break;
                case 2:
                    sq.dequeue();
                    cout<<"\n\t--Dequeue Sucessfully--";
                    getch();
                    goto queue;
                    break;
                case 3:
                    sq.show();
                    getch();
                    goto queue;
                case 0:
                    goto start;
                default:
                    cout<<"--Enter invalid number--";
                    goto queue;
            }break;   
               
               
               
                   
        case 0:
            goto out;               
           
    default:
        cout<<"\n\t--Enter invalid number--";
        goto start;
   
    }
   
   
    out:
    return 0;
}
 

No comments:

Post a Comment

Mini Library

  CLICK HERE  for Opening Mini Library Stuff Books are available in URDU & ENGLISH