Search This Blog

Showing posts with label queue. Show all posts
Showing posts with label queue. Show all posts

Tuesday, 2 February 2016

Stack - Queue Implementation using class C++

//implement stack-queue such that
//insert() - push item in stack
//pop() - pop elements from stack and insert into queue
//display() - display the queue

#include<iostream>
#include<stdio.h>

using namespace std;

class SQ
{
    int rear,front,top,stack[5],queue[5];
    public:
    int full();
    int empty();
    void set();
    void insert(int);
    void pop();
    void display();
};
int SQ::full()
{
    if(rear==4 || top==4)
    return 1;
    else
    return 0;
}
int SQ::empty()
{
    if(top==-1)
    return 1;
    else
    return 0;
}
void SQ::set()
{
    rear=-1;
    front=0;
    top=-1;
}
void SQ::insert(int i)
{
    stack[++top]=i;
}
void SQ::display()
{
       /*    cout<<"\nStack is:"<<endl;

    for(int i=top;i>=0;i--)
    {
        cout<<"\n";
        cout<<stack[i];                                          // if you want to see the stack
    }
    cout<<"\n";
    */
    if(rear==-1)
    {
        cout<<"\nPop Element from stack to insert in queue"<<endl;
    }
    else{
    cout<<"\nQueue is:"<<endl;
    for(int j=front;j<=rear;j++)
    {

        cout<<queue[j]<<" ";
    }
    cout<<"\n";
    }

}
void SQ::pop()
{
    /*if(top==-1)
    {
        cout<<"\nDone"<<endl;
    } */
    if(rear==-1){
    queue[++rear]=stack[top];
    top--;
    }else
    {
        queue[++rear]=stack[top];
        top--;
    }

}
int main()
{
    SQ sq;
    sq.set();
    int ch,val;
    do
    {
        cout<<"\nEnter your choice:\n1.insert into stack\n2.pop from stack and push into queue\n3.display\n4.enter 0 to exit"<<endl;
        cin>>ch;

        switch(ch)
        {
            case 1:if(sq.full())
            {
                cout<<"\nFull!"<<endl;
            }
            else
            {cout<<"\nEnter ele:";
            cin>>val;
            sq.insert(val);
            }
            break;

            case 2:if(sq.empty())
            {
                cout<<"\nEmpty"<<endl;
            }else
            {
                sq.pop();
            }
            break;

            case 3:sq.display();
            break;


            default:cout<<"\nEnter Proper choice"<<endl;
        }
    }while(ch!=0);
    return 0;
}

Queue implementation using class C++

// Do not forget to take a look at that both stack-queue class implementation ! label is stackqueue

 #include<iostream>

using namespace std;

class Queue
{
    int rear,front,Q[5];
    public:
        void set()
        {
            rear=front=-1;
        }
        int full()
        {
            if(rear>=4)
                return 1;
            else
                return 0;
        }
        int empty()
        {
            if(front==-1 || front>rear )
                return 1;
            else
                return 0;
        }
      
        void insert(int i)
        {
            if(front==-1)
            {
                front++;
            }
            Q[++rear]=i;
        }
      
        void display()
        {
            int i;
            for(i=front;i<=rear;i++)
            {
                cout<<" "<<Q[i];
            }
        }
      
        void del()
        {
            front++;
        }
};

int main()
{
    Queue q;
    int i,ch;
    q.set();
    do{
  
        cout<<"\n1.insert\n2.delete\n3.display\n4.0 for exit";
        cin>>ch;
      
        switch(ch)
        {
            case 1:if(q.full()){
                cout<<"\nQueue is full";
                }
                else{
                    cout<<"\nEnter element:";
                    cin>>i;
                    q.insert(i);
                }
                break;
              
            case 2:if(q.empty())
            {
                cout<<"\nQueue is empty";
            }else
            {
                q.del();
            }
            break;
          
            case 3:if(q.empty())
            {
                cout<<"\nQueue is empty";
            }else
            {
                q.display();
            }
            break;
          
            default : cout<<"\nProper choice plz";
        }
      
  
    }while(ch!=0);  
  
    return 0;  
}

Wednesday, 13 January 2016

Queue using Linked List

/*Queue using Linked List*/

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

    typedef struct queue
    {

    int data;
    struct queue *next;

    }node;

    node *rear=NULL;
    node *front=NULL;
    node *getnode()
    {

    int val;
    node *temp;
    printf("Enter val:");
    scanf("%d",&val);
    temp=(node *)malloc(sizeof(node));
    temp->data=val;
    temp->next=NULL;
    return temp;

    }
    void insert()
    {

    node *neww;
    neww=getnode();
    if(front==NULL)
    {
         front=rear=neww;
    }
    else
    {
         rear->next=neww;
         rear=neww;
    }

    }
    void del()
    {

    if(front==NULL)
    {
         printf("\nQueue is Empty!\n");
    }
    else
    {
         node *t;
         t=front;
         printf("\n%d is deleted\n",front->data);
         front=front->next;
         t->next=NULL;
         free(t);
    }

    }
    void display()
    {

    if(front==NULL)
    {
          printf("\nEmpty!\n");
    }
    else
    {
         node *t;
         t=front;
         printf("\n");
         while(t)
         {
              printf("%d ",t->data);
              t=t->next;
         }
         printf("\n");
    }

    }
    void main()
    {

    int ch;
    clrscr();
    while(1)
    {
    printf("\n1.insert\n2.delete\n3.display\n4.exit\nYour choice:");
    scanf("%d",&ch);

    switch(ch)
    {
    case 1:insert();
    break;

    case 2:del();
    break;

    case 3:display();
    break;

    case 4:exit(0);
    break;

    default:printf("\nProper choice plz!\n");
    }

    }
    }

Priority Queue

/* Priority Queue */

    #include<stdio.h>
    #include<conio.h>
    #define size 10
    struct queue
    {

    int Q[size];
    int front,rear;

    }q;
    int full()
    {

    if(q.rear==size-1)
    {
         return 1;
    }
    else
    {
         return 0;
    }

    }
    int empty()
    {

    if(q.front>q.rear)
    {
         return 1;
    }
    else
    {
         return 0;
    }

    }
    void insert(int x)
    {

    int j;
    j=q.rear;
    ++q.rear;
    while(j>=0 && q.Q[j]>=x)
    {
         q.Q[j+1]=q.Q[j];
         j--;
    }
    q.Q[j+1]=x;

    }
    void del()
    {

         int x;
         x=q.Q[q.front];
         q.front++;
    }

    void display()
    {

    int i;
    for(i=q.front;i<=q.rear;i++)
    {
         printf("%d ",q.Q[i]);
    }

    }
    void main()
    {

    int ch,val;
    q.rear=-1;
    q.front=0;
    clrscr();
    while(1)
    {
         printf("\n1.insert\n2.delete\n3.display\n4.exit:");
         scanf("%d",&ch);

         switch(ch)
         {
              case 1:if(full())
              {
                    printf("\nFULL!");
              }
              else
              {
                    printf("\nEnter data");
                    scanf("%d",&val);
                    insert(val);
              }
              break;

              case 2:if(empty())
              {
                     printf("\Aalready empty\n");
              }
              else
              del();
              break;

              case 3:
              if(empty())
              {
                    printf("\nalready empty\n");
              }
              else
              display();
              break;

              case 4:exit(0);
              break;

             default:printf("\nproper choice plz!\n");
        }
    }

    }

Circular Queue

/*Circular Queue -*/

    #include<stdio.h>
    #include<conio.h>
    # define size 5

    struct queue
    {

         int rear,front,Q[size];

    }q;

    void insert(int item)
    {

        
    if ((q.front == 0 && q.rear == size-1) || (q.front == q.rear+1))
    {
         printf("\nQueue Overflow\n");
         return;
    }

    

        
    if (q.front == -1)   
    {
         q.front = 0;
         q.rear = 0;
    }
    else
    {
         if (q.rear == size - 1)
              q.rear = 0;
         else
              q.rear = q.rear + 1;
    } 
    q.Q[q.rear] = item ;

    }

    void del()
    {

    if (q.front == -1)
    {
         printf("Queue Underflow\n");
         return ;
    }

    printf("\nDeleted element is %d\n",q.Q[q.front]);

    if (q.front == q.rear)
    {
         q.front = -1;
         q.rear = -1;
    }

    else
    {

         if (q.front == size - 1)
              q.front = 0;
         else
              q.front = q.front + 1;
    }

    }

    void display()
    {

        
    int i = q.front, j=q.rear;

    if (q.front == -1)    
    {
         printf("Queue is empty");
         return;
    }

    printf("\nQueue is :\n");

    if (i <= j)
    {
         while (i <= j)
         {
              printf("%d ",q.Q[i]);
              i++;
         }
    }
    else
    {
         while (i <= size - 1)
         {
              printf("%d ",q.Q[i]);
              i++;
         }

         i = 0;
         while (i <= j)
         {
              printf("%d ",q.Q[i]);
              i++;
         }
    }

    }
    void main()
    {

    
    int choice,data;
    q.front=-1;
    q.rear=-1;

    while(1)
    {
         printf("\n1.Insert\n2.Delete\n3.display\n4.exit\n");
         scanf("%d",&choice);

         switch(choice)
         {
              case 1:printf("\nEnter data\n");
              scanf("%d",&data);
              insert(data);
              break;

              case 2:del();
              break;

              case 3:display();
              break;

              case 4:exit(0);
              break;

              default:printf("\nEnter proper choice\n");
    }

    }
    getch();
    }

Simple Queue using array

/* Simple Queue using array */

    #include<stdio.h>
    #include<conio.h>
    #define size 5

    struct Queue{

    int rear,front,Q[size];

    }q;

    int qfull()
    {

    if(q.rear>=size-1)
    {
    return 1;
    }
    else
    {
    return 0;
    }

    }

    int qempty()
    {

    if(q.front==-1 || q.front>q.rear)
    {
    return 1;
    }
    else
    {
    return 0;
    }

    }

    void insert(int x)
    {

    if(q.front==-1)
    {
    q.front++;
    }
    q.rear++;
    q.Q[q.rear]=x;

    }

    int del()
    {

    int i;
    i=q.Q[q.front];
    q.front++;
    return i;

    }

    void display()
    {

    int i;
    printf("\n");
    for(i=q.front;i<=q.rear;i++)
    {
    printf("%d ",q.Q[i]);
    }
    printf("\n");

    }

    void main()
    {

    int choice,value,d;
    q.front=-1;
    q.rear=-1;

    while(1)
    {
    printf("\n1.Insert\n2.delete\n3.display\n4.exit\nEnter your choice:\n");
    scanf("%d",&choice);

    switch(choice)
    {

    case 1:if(qfull())
    {
    printf("\nQueue is full\n");
    }
    else
    {
    printf("\nEnter data\n");
    scanf("%d",&value);
    insert(value);
    }
    break;

    case 2:if(qempty())
    {
    printf("\n\Queue is empty can't delete\n");
    }
    else
    {
    d=del();
    printf("\n\deleted element is %d\n",d);
    }
    break;

    case 3:if(qempty())
    {
    printf("\nAdd at least one item\n");
    }
    else
    {
    display();
    }
    break;

    case 4:exit(0);
    break;

    default:printf("\nEnter proper choice\n");
    }

    }

    }