The Programming Project: stack
Showing posts with label stack. Show all posts
Showing posts with label stack. Show all posts

Sunday, April 20, 2014

Pascal trialngle



CODE

import java.io.*;
import java.util.*;
public class Pascal {
    public static void main(String[] args) {
        int rows;
        Stack<Integer> s1 = new Stack<Integer>();
        Stack<Integer> s2 = new Stack<Integer>();
        s1.push(1);
        s1.push(1);
        Scanner in = new Scanner(System.in);
        System.out.println("Enter the number of rows:");
        rows = in.nextInt();
        System.out.println("Pascal Traingle:");
        for(int i = 1; i <= rows; i++) {
            if(i==1 || i==2) {
                if(i==1)
                    System.out.println("1");
                else {
                    System.out.println();
                    System.out.println("1  1");
                    }
                }
            else {
            System.out.println();
            int p = s1.pop();
            System.out.print(p+" ");
            s2.push(p);
            while(s1.empty() == false) {
                    int q = s1.pop();
                    int next = p+q;
                    System.out.print(next+" ");
                    s2.push(next);
                    p = q;
                    }
            System.out.println(" 1 ");
            s2.push(1);
            while(s2.empty() == false) {
                    s1.push(s2.pop());
                    }
                }
            } // end of i loop
        }
    }

Saturday, October 12, 2013

GAME

The following program implements all features of Data Structures STACK, QUEUE and LIST using generic functions (templates in C++). User have choice for selection the data type float,integer or character to use the data structures. Also there is a game which takes 3 positive integers as input a,b and c. a represent the number of digits, b represent the maximum sum of digits and c represent the maximum total sum of all the numbers formed during the game. The score of the game is the highest number formed. The user after inputting the parameters will sequentially enter the digits, and the game will automatically stops when the sum of the numbers formed exceeds c. For example is user enter 3,13,200 as parameters and the digits entered sequentially is 1,1,9,5,9,6,5,4, 8,9.... the numbers that will be stored in the stack will be 119,5,9,11,12,9.... using the restrictions.  The stack is then sorted using another stack and the top element is your score. The Game is generalized for decimal numbers also i.e., input can be 1,1,.,9,5,.9,...... so on.
Here is an sample output:





































Here is the link for the code http://sdrv.ms/16Wrw8x

 C++ Code

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
using namespace std;
#define TRUE 1
#define FALSE 0
void f1(void);
void f2(void);
void f3(void);
void f4(void);
template <class L> class List
    {
    private:
    L d;
    L *data;
    int i;
    public:
    List(){}
    List (int n)
        {
        data=new L[n];
        i=0;
        }
    void store()
        {
        cin>>d;
        data[i]=d;
        i++;
        }
    void display(int n)
        {
        for(i=0;i<n;i++)
            cout<<"\t"<<data[i];
        }
    };
template <class L>
class Stack: virtual public List<L>
    {
    protected:
    L *data;
    int top;
    public:
    Stack(){}
    Stack (int n)
    {
    data=new L[n];
    top=-1;
    }
    //static int t;
    void push(L,int);
    void pop(int *pund,L *x);
    };
//template<class L>int Stack::t=-1;
template<class L>
void Stack<L>::push(L x,int STACKSIZE)
    {
     if(top==STACKSIZE-1)
        {
        cout<<"\n Stack is full, element not inserted:";
        return;
        }
     else
     data[++top]=x;
     }
template<class L>
void Stack<L>::pop(int *pund, L *x)
    {
    if(top==-1)
        {
            *pund=TRUE;
            return;
        }
    else
        {
        *x=data[top--];
        *pund=FALSE;
        return;
        }
    }
template<class L>
class Queue: virtual public List<L>
    {
    protected:
    L *data;
    int front;
    int rear;
    public:
    Queue(){}
    Queue(int MAXQUEUE)
        {
        data=new L[MAXQUEUE];
        front=rear=MAXQUEUE-1;
        }
    void insert(int MAXQUEUE,L x);
    void remove(L *x,int *pund,int MAXQUEUE);
    };
template<class L>
void Queue<L>::remove(L *x,int *pund,int MAXQUEUE)
    {
        if(rear==front)
            {
            cout<<"\n Queue is empty:";
            *pund=TRUE;
            return;
            }
        if(front==MAXQUEUE-1)
            front=0;
        else
            front++;
        *x=data[front];
        return;
    }
template<class L>
void Queue<L>::insert(int MAXQUEUE,L x)
    {
    if(rear==MAXQUEUE-1)
        rear=0;
    else
        rear++;
    if(rear==front)
        {
        cout<<"\n Queue overflow, element not inserted:";
        return;
        }
    data[rear]=x;
      return;
    }
template<class L>
class GAME:public Stack<L>,public Queue<L>
    {
    protected:
    L *digits;
       int tdigits;
    L dsum;
    L tsum;
    public:
    GAME(int a,int b,int c)
        { tdigits=a; dsum=b; tsum=c; }
    GAME():Stack<L>(400),Queue<L>(400)
        { digits= new L[400]; }
    void enter_digits(GAME,GAME,bool);
    };
template<class L>
void GAME<L>::enter_digits(GAME s2,GAME s3,bool FLAG)
{
    int und,pund=FALSE,len,k;
    int i=0,j=0,FLAG1=TRUE,dg=0,DFLAG=0,DCOUNT,count=0;
    L y,x,a,numb=0,sumd=0,tmp=0,sumt=0,numb1=0;
    char t[10],dot[3];
    if(FLAG)
    {
    do
    {

        for(i=0;i<tdigits;i++)
            {
          
            cout<<"\n Enter a digit: ";
            cin>>t;
            len=strlen(t);
            if(!atoi(t) && atoi(t) != 0)
                {
                i--;
                cout<<"\n You must enter a digit:\n";
                continue;
                }
            if(len>1)
                {
                i--;
                cout<<"\n You must enter a digit:\n";
                continue;
                }
            a=atoi(t);
            s2.insert(400,a);
            }
        do  
            {
                 if(sumd>dsum)
                break;
            else
                {
                if(FLAG1==TRUE)
                            numb=tmp+numb*10;
                s2.remove(&x,&und,400);
                    numb=x+numb*10;
                sumd=sumd+x;
                if(sumd>dsum)
                    {
                    tmp=x;
                    FLAG1=TRUE;
                    sumd -=x;
                     break;
                    }
                tmp=0;
                }
            FLAG1=FALSE;
            j++;
            }while(j<tdigits);
         if(FLAG1==TRUE)
         {j=1;numb=numb/10;}
         else
         {j=0;}
         //cout<<"\n <Number>"<<numb;
         s2.pop(&pund,&y);
             if(pund==TRUE)
            s2.push(numb,400);
            else
               {
               if(numb>=y)
                   {
                   s2.push(y,400);
                   s2.push(numb,400);
                   }
               else
                   {
                   s3.push(y,400);
                       while(y>numb)
                       {
                       s2.pop(&pund,&y);
                       if(pund==TRUE || y<=numb)
                           {
                           if(pund==TRUE)
                           {break;}
                           else
                           {s2.push(y,400); break;}
                           }
                       s3.push(y,400);
                        }
                        s2.push(numb,400);
                       while(1)
                       {
                       s3.pop(&pund,&y);
                       if(pund==TRUE)
                           break;
                       s2.push(y,400);
                       }
                  }
        }
        sumt +=numb;
        sumd=tmp;
        numb=0;

    }while(sumt<=tsum);
    s2.pop(&pund,&y);
    cout<<"\n Game ends,your score is:"<<y;
    cout<<"\n The sequence of numbers formed on the stack in descending order:\n";
    while(1)
    {
    cout<<y;
    s2.pop(&pund,&y);
    if(pund==TRUE)
        break;
    cout<<"\n";
    }
    }//end of integer game
    else
    {
        strcpy(dot,"$");
     do
        {
        for(i=0;i<tdigits;i++)
        {
        cout<<"\n Enter the number:";
        cin>>t;
         //cout<<strlen(t);
            if(strlen(t)>=2)
            {
                  cout<<"\n Not a valid input:";
            i--;
            continue;
            }
        if(strcmp(dot,t)==0)
            {
            i--;
            cout<<"\n Two consecutive dots not allowed:";
            continue;
            }
        if(strcmp(t,".")==0)
            {
            strcpy(dot,t);
            a=-1.0;
            s2.insert(400,a);
            continue;
            }
        if(!atof(t) || !atoi(t))
            {
                  cout<<"\n Not a valid input:";
            i--;
            continue;
            }
            j=0;
        while(t[j]!='\0' && strlen(t)<2)
              {  
              a=atof(t);
              s2.insert(400,a);
                j++;
                        }
                strcpy(dot,"$");
          } //end of one set of input
          und=TRUE;
           while(und)
             {
             s2.remove(&x,&und,400);
             if(x==-1)
                 {
                 DCOUNT++;
                 DFLAG=TRUE;
                 j=1;
                 if(DCOUNT==2)
                     break;
                 }//end of if
             else
                 {
                 if(DFLAG==TRUE)
                     {
                     sumd +=x;
                     if(sumd>dsum)
                         {
                         tmp=x;
                         DFLAG=FALSE;
                         break;
                         }
                     count++;
                     numb1=numb1+x*pow(10,-j);
                     j++;
                     if(count==tdigits)
                     break;
                     }//end of if
                 else
                     {
                     sumd +=x;
                     if(sumd>dsum)
                         {
                         tmp=x;
                         break;
                         }
                     count++;
                     numb=x+numb*10;
                     }//end of inner else
                 if(count==tdigits)
                 break;
                 }//end of else
             cout<<"\n Sumd="<<sumd;
             cout<<"\n Count="<<count;
             cout<<"\n J="<<j;
             cout<<"\n DFLAG="<<DFLAG;
             } //end of while(und)  
    count=0;
    numb=numb+numb1;
    cout<<"\n <Number>\n"<<numb;
    sumt +=numb;
    numb=0.0;
    numb1=0.0;
    if(DFLAG==TRUE && tmp!=0)
        {
        count++;
        numb1=numb1+tmp*pow(10,-j);
        sumd=tmp;
        }
    if(DFLAG==FALSE && tmp!=0)
        {
        count++;
        numb=tmp+numb*10;
        sumd=tmp;
        }
    }while(sumt<70.0);    
    }//end of float game
}
int main()
{
    int menu;
    cout<<"\n Press '1' for LIST:";
    cout<<"\n Press '2' for STACK:";
    cout<<"\n Press '3' for QUEUE:";
    cout<<"\n Press '4' to play the GAME:";
    cout<<"\n Press '5' to exit:";
    do{
    cout<<"\n Enter your choice:";
    cin>>menu;
    switch(menu)
        {
        case 1:
        f1();
        break;
        case 2:
        f2();
        break;
        case 3:
        f3();
        break;
        case 4:
        f4();
               break;
        default:
        break;
        }
    }while(menu!=5);
return 0;
}
void f4(void)
{
    int a,b,c,ch;
    bool FLAG;
    cout<<"\n Enter the values of a,b and c to begin:";
    cin>>a>>b>>c;
    GAME <int>s1(a,b,c); GAME<int>s2;GAME <int>s3;
    GAME <float>t1(a,b,c); GAME <float>t2; GAME <float>t3;
    cout<<"\n Enter 1 to play with positive digits:";
    cout<<"\n Enter 2 to play with floating point:";
    cout<<"\n Enter your choice:";
    cin>>ch;
    if(ch==1)
        {
        FLAG=1;
        cout<<"\n Start entering digits:\n";
        s1.enter_digits(s2,s3,FLAG);
        }
    if(ch==2)
        {
        FLAG=0;
        cout<<"\n Start entering digits:\n";
        t1.enter_digits(t2,t3,FLAG);
        }
  
   
}
void f3(void)
{
    int n,choice,x,und,ch;
    Queue <int>Q1(n);
    Queue <float>Q2(n);
    float x1;
    cout<<"\n Enter the size of the QUEUE:";
    cin>>n;
    cout<<"\n Enter 1 to add integer values to QUEUE:";
    cout<<"\n Enter 2 to add floating point values to QUEUE:";
    cout<<"\n Enter your choice:";
    cin>>ch;
    if(ch==1)
    {
    cout<<"\n Press 1 to insert an element in the Queue:";
    cout<<"\n Press 2 to remove an element from the Queue:";
    cout<<"\n Press 3 to exit:";
    do
    {
    cout<<"\n Enter your choice for menu:";
    cin>>choice;
    switch(choice)
        {
        case 1:
        cout<<"\n Enter the element:";
        cin>>x;
        Q1.insert(n,x);
        break;
        case 2:
        Q1.remove(&x,&und,n);
        if(und==TRUE)
            {
          //    cout<<"\n Queue Underflow:";
            break;
            }
        else
        cout<<"\n Element removed is:  "<<x;
        break;
        default:
        break;
        }
    } while(choice!=3);
    }
    if(ch==2)
    {
    cout<<"\n Press 1 to insert an element in the Queue:";
    cout<<"\n Press 2 to remove an element from the Queue:";
    cout<<"\n Press 3 to exit:";
    do
    {
    cout<<"\n Enter your choice for menu:";
    cin>>choice;
    switch(choice)
        {
        case 1:
        cout<<"\n Enter the element:";
        cin>>x;
        Q2.insert(n,x1);
        break;
        case 2:
        Q2.remove(&x1,&und,n);
        if(und==TRUE)
            {
          //    cout<<"\n Queue Underflow:";
            break;
            }
        else
        cout<<"\n Element removed is:  "<<x1;
        break;
        default:
        break;
        }
    } while(choice!=3);
    }
  
}
void f2(void)
{
    int n,choice,x,und,ch;
    Stack <int>S1(n);
    Stack <float>S2(n);
    float x1;
    cout<<"\n Enter the size of the STACK:"<<endl;
    cin>>n;
    cout<<"\n Enter 1 to add integer values to List:";
    cout<<"\n Enter 2 to add floating point values to List:";
    cout<<"\n Enter your choice:";
    cin>>ch;
    if(ch==1)
    {
    cout<<"\n Press 1 to push an element in the stack:";
    cout<<"\n Press 2 to pop an element:";
    cout<<"\n Press 3 to exit:";
    do
    {
    cout<<"\n Enter your choice for menu:";
    cin>>choice;
    switch(choice)
        {
        case 1:
        cout<<"\n Enter the element:";
        cin>>x;
        S1.push(x,n);
        break;
        case 2:
        S1.pop(&und,&x);
        if(und==TRUE)
            {
            cout<<"\n Stack Underflow:";
            break;
            }
        else
        cout<<"\n Element popped out is:  "<<x;
        break;
        default:
        break;
        }
    } while(choice!=3);
    }
    if(ch==2)
    {
    cout<<"\n Press 1 to push an element in the stack:";
    cout<<"\n Press 2 to pop an element:";
    cout<<"\n Press 3 to exit:";
    do
    {
    cout<<"\n Enter your choice for menu:";
    cin>>choice;
    switch(choice)
        {
        case 1:
        cout<<"\n Enter the element:";
        cin>>x1;
        S2.push(x1,n);
        break;
        case 2:
        S2.pop(&und,&x1);
        if(und==TRUE)
            {
            cout<<"\n Stack Underflow:";
            break;
            }
        else
        cout<<"\n Element popped out is:  "<<x1;
        break;
        default:
        break;
        }
    } while(choice!=3);
    }
}
void f1(void)
{
    int n,nf=0,choice;;
    char c;
    List <int> Ls(n);
    List <float>Ls1(n);
    List <char>Ls2(n);
    cout<<"\n Enter the number of elements to be stored in the list:"<<endl;
    cin>>n;
    cout<<"\n Enter 1 to add integer values to List:";
    cout<<"\n Enter 2 to add floating point values to List:";
    cout<<"\n Enter 3 to add character values to List:";
    cout<<"\n Enter your choice:";
    cin>>choice;
    if(choice==1)
        {
      
        do
    {
    cout<<"\n Enter the element in the list:";
    Ls.store();
    nf++;
    if(nf==n)
        {
        cout<<"\n No more elements can be added:";
        break;
        }
    cout<<"\n Do you wish to enter another element (y/n):";
    cin>>c;
    }while (c!='n');
    cout<<"\n Press 'v' to view the list or any other key to exit:";
    cin>>c;
     if(c=='v')
    Ls.display(nf);
        }
    if(choice==2)
        {
      
        do
    {
    cout<<"\n Enter the element in the list:";
    Ls1.store();
    nf++;
    if(nf==n)
        {
        cout<<"\n No more elements can be added:";
        break;
        }
    cout<<"\n Do you wish to enter another element (y/n):";
    cin>>c;
    }while (c!='n');
    cout<<"\n Press 'v' to view the list or any other key to exit:";
    cin>>c;
     if(c=='v')
    Ls1.display(nf);
        }
    if(choice==3)
    {
        List <char>Ls[n];
      
    do
    {
    cout<<"\n Enter the element in the list:";
    Ls2.store();
    nf++;
    if(nf==n)
        {
        cout<<"\n No more elements can be added:";
        break;
        }
    cout<<"\n Do you wish to enter another element (y/n):";
    cin>>c;
    }while (c!='n');
    cout<<"\n Press 'v' to view the list or any other key to exit:";
    cin>>c;
     if(c=='v')
    Ls2.display(nf);
    }
}



Tuesday, August 27, 2013

Parentheses Checking

Given an expression string exp, write a program to examine whether the pairs and the orders of “{“,”}”,”(“,”)”,”[","]” are correct in exp. For example, the program should print true for exp = “[a+{b-(c-(d-e)*d)+w)}+q]” and false for exp = “[(])”



 import java.util.*;

public class Parentheses
   {
   public static void main(String[] args)
{
int n,i,pos,posm,t;
boolean flag=true;
char[] symb={'(','{','[',']','}',')'};
char tmp;
Scanner in = new Scanner(System.in);
String expression;
System.out.println(" Enter the expression:");
expression=in.nextLine();
System.out.println(" Entered expression is:"+expression);
n=expression.length();
Stack s = new Stack(n);
System.out.println("");
for(i=0;i<n;i++)
{
pos=0;
posm=0;
if(expression.charAt(i)==symb[0] || expression.charAt(i)==symb[1] || expression.charAt(i)==symb[2] )
{
s.push(expression.charAt(i));

}
if(expression.charAt(i)==symb[3] || expression.charAt(i)==symb[4] || expression.charAt(i)==symb[5] )
{
if(s.isempty()==true)
{
flag=false;
break;
}
else
{
tmp=s.pop();
for(t=0;t<3;t++)
if(symb[t]==tmp)
pos=t;
for(t=3;t<6;t++)
if(symb[t]==expression.charAt(i))
posm=t;
if(pos+posm!=5)
{
flag=false;
break;
}
}
}
 

}
if(s.isempty()==false)
flag=false;
if(flag==true)
System.out.println(" Valid Expression:");
else
System.out.println(" Invalid Expression:");
in.close();
}

  }
class Stack
{
public Stack(int a)
{
top=-1;
n=a;
stack = new char[n];
}
public void push(char x)
{
stack[++top]=x;
}
public char pop()
{
return (stack[top--]);
}
public char stacktop()
{
return (stack[top]);
}
public boolean isempty()
{
return ( top==-1 ? true: false);
}
private int top;
private int n;
private char[] stack;

}



Friday, March 29, 2013

C Program #19


/* Dynamic Implementation of queue and few basic operations!*/

#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 100
#define TRUE 1
#define FALSE 0
struct queue{
                                                int items;
                                                struct queue *next;
                                                };
typedef struct queue node;
struct stack{
                        int top;
                        int items[MAXSIZE];
                        };
struct stack stk;
struct queue p,q,temp;
int pop(struct stack *);
int remove(node **);
int Isempty(node *);
void push(struct stack *,int);
int isempty_st(struct stack *);
void insert(node *,int);
void reverse_queue(node **);
void display_front_rear(node **);
void display_rear_front(node **);
void append(node *,node *);
void replace(node**,int,int,node **);
void display_front_rear_unchanged(node **,node **);
void display_rear_front_unchanged(node **,node **);
int Isequal(node **,node**,node**,node**);
int main()
{
            int choice,x,ins,e;
            stk.top=-1;
            node *rootp,*rootq,*tqueue,*rqueue;
            rootp=(node *)malloc(sizeof(node));
            rootp->next=NULL;
            rootq=(node *)malloc(sizeof(node));
            rootq->next=NULL;
            tqueue=(node *)malloc(sizeof(node));
            tqueue->next=NULL;
            rqueue=(node *)malloc(sizeof(node));
            rqueue->next=NULL;
            do
                        {
                        printf("\n Press 1 to add elements in Queue:");
                        printf("\n Press 2 to remove elements from Queue:");
                        printf("\n Press 3 to append queue Q at the end of queue P:");
                        printf("\n Press 4 to print elements of queue P from front to end with P becoming empty:");
                        printf("\n Press 5 to print elements of queue P from rear to front with P becoming empty:");
                        printf("\n Press 6 to print elements of queue P from front to end with P remaining same:");
                        printf("\n Press 7 to print elements of queue P from rear to front with P remaining same:");
                        printf("\n Press 8 to reverse the queue P:");
                        printf("\n Press 9 to replace a specific element of a queue:");
                        printf("\n Press 10 to check whether the queues P and Q are equal or not:");
                        printf("\n Type 11 to exit:");
                        scanf("%d",&choice);
                        switch(choice)
                                    {
                                    case 1:
                                                            printf("\n Enter 1 to insert in queue P, 2 to insert in queue Q:");
                                                            scanf("%d",&x);
                                                                        switch(x)
                                                                                    {
                                                                                    case 1:
                                                                                    printf("\n Enter the element to be inserted:");
                                                                                    scanf("%d",&ins);
                                                                                    insert(rootp,ins);
                                                                                    break;
                                                                                    case 2:
                                                                                    printf("\n Enter the element to be inserted:");
                                                                                    scanf("%d",&ins);
                                                                                    insert(rootq,ins);
                                                                                    break;
                                                                                    }
                          break;
                          case 2:
                                                            printf("\n Enter 1 to remove in queue P, 2 to remove in queue Q:");
                                                            scanf("%d",&x);
                                                                        switch(x)
                                                                                    {
                                                                                    case 1:
                                                                                    e=remove(&rootp);
                                                                                    //if(e=-9999)
                                                                                    //                      {}
                                                                           //       else
                                                                                    printf("\n Element removed is:%d\n",e);
                                                                                    break;
                                                                                    case 2:
                                                                                    e=remove(&rootq);
                                                                                    //if(e=-9999)
                                                                                    //          {}
                                                                                    //else
                                                                                    printf("\n Element removed is: %d\n",e);
                                                                                    break;
                                                                                    }
                                    break;
                                    case 3:
                                    append(rootp,rootq);
                                    break;
                                    case 4:
                                    display_front_rear(&rootp);
                                    break;
                                    case 5:
                                    display_rear_front(&rootp);
                                    break;
                                    case 6:
                                    display_front_rear_unchanged(&rootp,&tqueue);
                                    break;
                                    case 7:
         case 8:
                                    reverse_queue(&rootp);
                                    break;
                                    display_rear_front_unchanged(&rootp,&tqueue);
                                    break;
                                    case 9:
                                    printf("\n Enter the element to be replaced:");
                                    scanf("%d",&e);
                                    printf("\n Enter the element to be placed:");
                                    scanf("%d",&x);
                                    replace(&rootp,e,x,&tqueue);
                                    break;
                                    case 10:
                                    if(Isequal(&rootp,&rootq,&tqueue,&rqueue))
                                                printf("\n Queues are equal:\n");
                                    else
                                                printf("\n Queues are not equal:\n");
                                    break;
                                    }
                        }while(choice>=1 && choice<=10);
return 0;
}
int Isequal(node **qp,node **qq,node **t1,node **t2)
            {
            int FLAG=TRUE,s1=0,s2=0,x,y;
            if((Isempty(*qp) && !Isempty(*qq)) ||(!Isempty(*qp) && Isempty(*qq)))
                        return (FALSE);
            else if(Isempty(*qp) && Isempty(*qq))
                        return (TRUE);
            else
                                    {
                                    while(!Isempty(*qp))
                                                {insert(*t1,remove(qp)); s1++;}
                                    while(!Isempty(*qq))
                                                {insert(*t2,remove(qq)); s2++;}
                                    if(s1!=s2)
                                                return (FALSE);
                                    else
                                                {
                                                while(!Isempty(*t1))
                                                                        {
                                                                        x=remove(t1); y=remove(t2);
                                                                        if(x!=y)
                                                                        FLAG=FALSE;
                                                                        insert(*qp,x); insert(*qq,y);
                                                                        }
                                                }
                                     if(FLAG==TRUE)
                                                return (TRUE);
                                     else
                                                return (FALSE);
                                    }

   }
void replace(node**qp,int e,int x,node **qq)
            {
            int y;
            node *tmp;
            tmp=*qp;
            printf("\n Elements of the queue:");
            while(!Isempty(*qp))
                        insert(*qq,remove(qp));
            while(!Isempty(*qq))
                        {
                        y=remove(qq);
                        if(y==e)
                                    {
                                    printf("%d ",x);
                                    insert(tmp,x);
               }
                        else
                                    {
                                    insert(tmp,y);
                                    printf("%d ",y);
                                    }
                        }
            printf("\n");
            }
void display_rear_front_unchanged(node **qp,node **qq)
            {
            int x;
            printf("\n Elements of the queue:");
            while(!Isempty(*qp))
                         {
                         x=remove(qp);
                         push(&stk,x);
                         insert(*qq,x);
                         }
            while(!isempty_st(&stk))
                        {
                        printf("%d ",pop(&stk));
                        insert(*qq,remove(qq));
                        }
            printf("\n");
            }
void display_front_rear_unchanged(node **qp,node **qq)
            {
            int x;
            node *tmp;
            tmp=*qp;
            printf("\n Elements of the queue:");
            while(!Isempty(*qp))
                        insert(*qq,remove(qp));
            while(!Isempty(*qq))
                        {
                        x=remove(qq);
                        printf("%d ",x);
                        insert(tmp,x);
                        }
            printf("\n");
            }

void reverse_queue(node **use)
{
            node *curr,*prev;
   prev=(node *)malloc(sizeof(node));
            prev->next=NULL;
            curr=*use;
            while((*use)->next!=NULL)
                        {
                                                curr=*use;
                                                *use=(*use)->next;
                                                curr->next=prev;
                                                prev=curr;
                        }
                        *use=curr;
}
void display_rear_front(node **use)
            {
            printf("\n Elements of the queue:");
            while(!Isempty(*use))
                        push(&stk,remove(use));
            while(!isempty_st(&stk))
                        printf("%d ",pop(&stk));
            printf("\n");
            }
void display_front_rear(node **use)
            {
            printf("\n Elements of the queue:");
            while(!Isempty(*use))
                        printf("%d ",remove(use));
            printf("\n");
            }
void append(node *p,node *q)
            {
            while(p->next->next!=NULL)
                        p=p->next;
            p->next=q;
            }
int Isempty(node *use)
            {
            return ((use->next==NULL)? TRUE : FALSE);
            }
int remove(node **use)
            {
            if(Isempty(*use))
                        {printf("\n Queue is empty:\n");
                        return (-9999);}
            else{
            node *temp,*tp;
            temp=*use;
            tp=temp;
            temp=temp->next;
            *use=temp;
            return(tp->items);
            free(tp);}
            }
void insert(node *use,int x)
            {
            node *p;
            while(use->next!=NULL)
            use=use->next;
            use->items=x;
            p=(node *)malloc(sizeof(node));
            use->next=p;
            p->next=NULL;
            }
int isempty_st(struct stack *stk)
            {
             return((stk->top==-1)?TRUE:FALSE);
            }
int pop(struct stack *stk)
            {
            return(stk->items[(stk->top)--]);
            }
void push(struct stack *stk,int x)
            {
            stk->items[++stk->top]=x;
            }