STACK

Array Implementation of STACK :-

Quest :- Declare a stack using ar5ray that contains int type numbers and define Pop & Push function using C++ syntax.

 

Ans :-

const int size = 50;

class Stack

{

          int data[size];

          int top;

 public:

          stack( ) { top = -1;  }

          void push( );

          void pop( );

          void display ( );

          {

                    for (int i=top; i>=0; i--)

                             cout<< data[i]<<”\n”;

          }

};

void stack :: push()

{

          if(top == size)

          {

                     cout<< “Overflow”;

                    return;

          }

          int x;

          cout<< “Enter the data :”;

          cin>>x;

          top++;

          data[top] = x;

}

 

void stack :: pop( )

{

          if (top == -1)

          {

                    cout<< “Underflow”;

                    return;

          }

          int x;

          x = data[top];

          top--;

          cout<<x<< “Removed”;

}

 

Quest :-

class stack {

          int data[10];

          int top;

public :

          stack( ) { top = -1; }

          void push( );           // to push an element into the stack

          void pop ( );                  // to pop an element from the stack

          void delete (int item); // to delete all elements which are equal to Item.

};

Complete the class with all function definitions. Use another stack to transfer data temporarily.

 

Ans : - Up to push and pop same as above.

 

void stack :: delete (int item)

{

          stack t;

          if (top = = -1)

          {

                    cout<< “Underflow “;

                    return;

          }

          while (top >= 0)

          {

                    if (data[top] != item)

                    {

                             t.top++;

                             t.data[t.top] = data[top];

                    }

                    top--;

          }

          while (t.top>>0)

          {

                    top ++;

                    data[top] = t.data[t.top --];

                    t.top--;

          }

}

Linked List implementation of STACK :-

 

Quest :- Write a function in C++ to delete a node containing Books information from a dynamically allocated stack of Books implemented with the help of following structure.

 

struct book

{

          int bno;

          char bname[20];

          book *next;

};

class stack {

          book *top;

public:

          stack( ) { top = = NULL; }

          void push( )

          void pop ( )

};

 

Ans :-

void stack :: push( )

{

          book *newptr;

          newptr = new book;

          cout<< “Insert Book Data \n”;

          cout<< “Enter Book No :”

          cin>>newptr -> bno;

          cout<<”\n Enter Book name :”

          gets(newptr -> bname);

 

          if (top == NULL )  //if stack is empty

                    top = newptr;

          else

          {

                    newptr->next = top;

                    top = newptr;

          }

}

 

void stack :: pop( )

{

          book *ptr = top;

          if (top == NULL)

                    cout<< “Stack empty, Underflow .”;

          else

          {

                    cout<< “Element being deleted is :\n”;

                    cout<< top -> bno;

                    cout<< top -> bname;

                    top = top ->next;

                    delete ptr;

          }

}

 

Quest :-  Write a function in C++ to perform a Push operation on a dynamically allocated stack containing real number .

Ans :-

struct node

{

          int info;

          node *next;

};

class stack

{

          node *top;

public:

          stack( ) { top = NULL; }

          void push( )

};

 void stack :: push( )

{

// body

}

 

Quest :- Write a function in C++ to perform a Push operation in a dynamically allocated stack considering the following:

struct node

{

          int x,y;

          node * link;

};

class stack

{

          node *top;

public:

          stack( ) { top = NULL };

          void push( );

          void pop( );

          ~stack( );

};

 

Quest :- Define functions stackpush( ) to insert nodes and stackpop( ) to delete nodes for a linked list implemented stack having the following structure for each node :

 

struct node

{

          char name[20];

          int age;

          node *link;

};

class stack

{

          node *top;

public:

          stack( ) { top = NULL; }

          void stackpush( );

          void stackpop( );

};

 

 

Queue  

 

Queue as Linked List :-

 

1.     Quest :- Write a function in C++ to perform Delete operation in a dyanamically allocated queue considering the following description.

struct node {

          float u,v;

          node *link;

};  //end of node structure.

 

class queue {

          node *rear, *front;

public:

          queue( ) {rear = NULL; front = NULL; }

          void insert( );

          void delete( );

          void display( ) {

                    node *dispptr;

                    dispptr = front;

                    while (dispptr != NULL) {

                              cout<<dispptr->u;

                              cout<<dispptr->v;

                              dispptr = dispptr->next;

                    } //end of while loop.

          } //end of display function.

          ~queue( );

 

}; //end of class queue.

 

void queue::insert( ) {

          node *newptr;

          newptr = new node;

          cout<< “Enter Number1 :”;

          cin>>newptr->u;

          cout<< “Enter Number2 :”;

          cin>>newptr ->v;

          newptr->link = NULL;

          if(rear = NULL)

                    front = rear = newptr;

          else          {

                    rear->link = newptr;

                    rear = newptr;

          } //end of if-else statement.

} //end of function.

 

void queue :: delete( ) {

          if (front == NULL)

                    cout<< “Underflow”;

          else {

                    node *ptr;

                    ptr = front;

                    front = front -> link;

                    delete ptr;

          }  //end of if else statement.

} //end of function.

 

2.     Quest :- Write a function in C++ to delete a node containing customers information, from a dynamically allocated queue of customers implemented with the help of following structures.

struct customer

{

          int cno;

          char cname[20];

          customer *link;

};

 

Ans :- Same as quest 1 delete operation.

 

3. Quest :-          Define member functions queins( ) to insert nodes and quedel( ) to delete nodes of the linked list implemented class queue, where each node has following structure.

 

struct node {

          char name[20];

          int age;

          node *link;

};

class queue {

          node *rear, *front;

public:

          queue( ) { rear = NULL; front = NULL; }

          void queins( );

          void quedel( );

};

 

Ans :- same as quest 1 except enter value for name and age.

 

4.       Quest :- Give necessary declarations for queue containing float type numbers. Write a user defined function in C++ to insert a float type number in queue. Use linked representation of queue.

 

Ans :- Same as question no.1 except change data member of node and write a float variable as data member.

 

 

Array implementation of  Queue :-

1.     Quest :- Declare a queue using array that contains int type numbers and define insertion and deletion function using C++ syntax.

Ans :-

const int size = 50;

class queue {

     int data[size];

     int front, rear;

public:

     queue( ){ front = rear = -1; }

     void insert( );

     void delete( );

     void display( ) {

          if(front = = -1)

                   return;

          for (int i = front; i<=rear; i++)

                   cout<<data[i]<< “\t”;

          cout<<endl;

     }

};

 

void queue :: insert( )

{

     if(rear = = size-1) {

          cout<< “Queue Full, Overflow”;

          return;

     } //end of if

     else if (rear = = -1) {

          front = rear =0;

          cout<< “Enter the data :”;

          cin>>data[rear];

          } //end of else if

          else {

                   rear++;

                   cout<< “ Enter the Data :”;

                   cin>>data[rear];

          } //end of else

} //end of insert function.

    

void queue :: delete( )

{

     if(front = = -1) {

          cout<< “Queue empty”<<endl;

          return;

     }

     else if (front = = rear)

                   front = rear = -1;

          else

                   front++;

} //end of delete function.

    

 

Array implementation of Circular Queue :-

Quest :-

class queue {

     int data[10];

     int front, rear;

public:

     queue( ) { front = -1; rear = -1; }

     void add( );  //to add an element into the queue.

     void remove( );  //to remove an element from the queue.

     void delete(int ITEM);  //to delete all elements which are equal to item.

};

Complete the class with all functions definitions for circular array queue. Use another queue to transfer data temporarily.

 

Ans :-

void queue :: add( ) {

     if((front = = 0 && rear = = 9) || (rear = = front -1))

     {

          cout<< “Overflow”;

          return;

     }

     int x;

     cout<< “Enter the data :”;

     cin>>x;

     if(rear = = -1)

          { front = 0; rear = 0;  }

     else if (rear = = 9)

                   rear = 0;

          else

                   rear++;

     data[rear] = x;

}

 

void queue :: remove ( ) {

     if (front = = -1) {

          cout<< “Underflow”;

          return;

}

int x;

x = data[front];

if(front = = rear) {

          front = -1;  rear = -1;

}

else if(front = = 9)

          front = 0;

        else

          front++;

cout<<x<< “Removed”;

}

 

void queue :: delete(int ITEM)

{

          if (front = = -1) {

                    cout<< “Underflow”;

                    return;

          }

          queue t;

          t.front = 0;

          while (1)

          {

                    if (data[front] != ITEM)

                    {

                             t.rear++;

                             t.data[t.rear] = data[front];

                    }

                    if ( front = = rear)

                             break;

                    front++;

                    if ( front == 10)

                             front = 0;

          }

          //copy temp data to current data

          front = 0; rear = -1;

          while (t.front <= t.rear)

          {

                    rear++;

                    data[rear] = t.data[t.front];

                    t.front ++;

          }

}