Showing posts with label CBCS. Show all posts
Showing posts with label CBCS. Show all posts

Friday 2 September 2016

Queue Question Set

Queue

Data Structures 15CS33 


1.      What is a linear queue? What are the applications of linear queue? Implement/Write a C program to simulate the 1) insert 2) delete  3)display operations.
(December 2007)
(8 marks)

2.      What is circular queue? What ate the advantages of Circular queue over simple queue.
Write implementation for circular queue using array. Also write following routine of circular queue. 1)      insert 2) delete 3) display

(June 2009)
(December 2010)
(June 2011)
(December 2013)
(December 2015)
(10 marks)

3.      Explain priority queue.
(December 2009)
(6 marks)

4.      Explain the working of simple queue
(June 2010)
(5 marks)

5.      For a given circular queue shown in Fig below write the values of front and rear in the table after each specified operation is performed. Queue full/empty conditions must be considered. 0-7 indicates the array indices.
(December 2011)(4 marks)


6.      Explain how would you implement a circular queue using dynamically allocated arrays.
(June 2013)


Stack Questions set

Stack
Data Structures 15CS33 


1.     Define stack and List and implement basic operations in stack using C (push, pop, isempty, isfull). Implement reversing a string using stack (array implementation) in C.
(December 2007)
(June 2009)
(June 2010)
(December 2010)
(December 2010)
(December 2011)
(December 2012)
(December 2013)
(December 2014)
(December 2015)
(12 marks)

2.      Write a C program to implement multiple stacks using single array
(December 2007)
(12 marks)

3.      Write short notes on Applications of stacks
(December 2007)
(5 marks)

4.      Write an algorithm to evaluate postfix expression. Trace the same algorithm with stack contents for the following expression A B C + * C B A - + *    with A=1, B=2, C=3.
(June 2009)
(December 2009)
(10 marks)

5.      Convert each of the following expression to its postfix and prefix forms
a)      ( A + B ) * C – D $ E * F
b)     A - B / C * D $ E
c)      ( A + B ) * ( C + D – E ) * F
d)     ( ( ( A + ( B - C ) * D ) ^ E ) + F )
e)      ( a + b ) * d + e / ( f + a * d ) + c
f)       ( ( a / ( b - c + d ) ) * ( e - a ) * c )
g)      a / b – c + d * e – a * c
(June 2009)
(December 2012)
(December 2013)
(12 marks)

6.      What is stack? Indicate how stack is represented in C.
(December 2009)
(5 marks)

7.      Show using the tabular column how the expression (a+b)*c is converted to a postfix expression according to the infix to postfix coversion algorithm
(June 2010)
(5 marks)


8.  Write the algorithm to evaluate a valid postfix expression and hence evaluate the postfix expression.
      6 2 3 + - 3 8 2 / + *
  
      1 2 3 + * 3 2 1 - +  *
  
       A B + C D E - * /                   for A=5 B=6 C=4 D=3 E=7
  
       6 2 3 + 3 8 2 / + * 2 $ 3 +

All the operands are single digit positive integers and operators are binary in nature.

(June 2010)
(December 2010)
(December 2011)
(June 2013)
(June 2015)
(December 2015)
(10 marks)

9.      Write a algorithm and function to convert a valid infix expression to postfix expression. Demonstrate the same function with example.(using stack)
a)      ( a * b ) + c / d
b)     ( ( ( a / b ) – c ) + ( d * e ) ) - (a * c)
c)      a * ( b + c ) * d
d)     A $ B * C – D + E / F / ( G + H )
e)      A – B / ( C * D $ E )

(June 2011)
(June 2012)
(June 2014)
(December 2015)
(12 marks)

10.  Write a C program to implement a two primitive operations on stack using dynamic memory allocation.
(June 2012)
(8 marks)

11.  What is system stack? How the control is transferred to or from the function with the help of activation records.
(December 2012)
(6 marks)

12.  Convert the infix expression to postfix expression and evaluate the same.
a / b – c + d * e – a * c   for a=6 b=3 c=1 d=2 e=4
 (June 2013)
(6 marks)

13.  How multiple stacks implemented using one dimensional array? Explain with suitable example.
(June 2014)
(4 marks)


Circular Queue Using an Array

Circular Queue Using an Array

#include <stdio.h>
#include<stdlib.h>
#define MAX 3

char cq[MAX];
int front = -1,rear = -1;
void add(char);
void del();
void display();

int main()
{
            int ch;
            char item;
            while(1)
            {
                        printf("\n\n~~Main Menu~~");
                        printf("\n==> 1. Insertion and Overflow Demo");
                        printf("\n==> 2. Deletion and Underflow Demo");
                        printf("\n==> 3. Display");
                        printf("\n==> 4. Exit");
                        printf("\nEnter Your Choice: ");
                        scanf("%d", &ch);
                        switch(ch)
                        {
                                    case 1: printf("\n\nEnter the element to be inserted: ");
                                                scanf("%d", &item);
                                                add(item);
                                                break;
                                    case 2: del();
                                                break;
                                   case 3: display();
                                                break;
                                    case 4: exit(0);
                                    default: printf("\n\nPlease enter a valid choice");
                        }
            }
}
void add(char item)
{
            if(front == (rear+1)%MAX)
                        printf("\n\n~~Circular Queue Overflow~~");
            else
            {
                        if(front == -1)
                                    front = rear = 0;
                        else
                                    rear = (rear+1)%MAX;
                        cq[rear] = item;
            }
}

void del()
{
            char item;
            if(front == -1)
            {
                        printf("\n\n~~Circular Queue Underflow~~");
            }
            else
            {
                        item = cq[front];
                        if(front == rear) //only one element
                                    front = rear = -1;
                        else
                                    front = (front+1)%MAX;
                        printf("\n\nDeleted element from the queue is: %d ", item );
            }
}
void display ()
{
            int i ;
            if(front == -1)
            {
                        printf("\n\nCircular Queue Empty");
                        return;
            }
            else
            {
                        printf("\nCircular Queue contents are:\n");
                        printf("\nFront[%d]-> ", front);
                        for(i=front; i!=rear ; i=(i+1)%MAX)
                        {
                                    printf("  %d", cq[i]);
                        }
                        printf("  %d",cq[i]);
                        printf(" <-[%d]Rear", rear);
    }
}

Output:
~~Main Menu~~
==> 1. Insertion and Overflow Demo
==> 2. Deletion and Underflow Demo
==> 3. Display
==> 4. Exit
Enter Your Choice: 1
Enter the element to be inserted: 11

~~Main Menu~~
==> 1. Insertion and Overflow Demo
==> 2. Deletion and Underflow Demo
==> 3. Display
==> 4. Exit
Enter Your Choice: 1
Enter the element to be inserted: 22

~~Main Menu~~
==> 1. Insertion and Overflow Demo
==> 2. Deletion and Underflow Demo
==> 3. Display
==> 4. Exit
Enter Your Choice: 1
Enter the element to be inserted: 33

~~Main Menu~~
==> 1. Insertion and Overflow Demo
==> 2. Deletion and Underflow Demo
==> 3. Display
==> 4. Exit
Enter Your Choice: 1
Enter the element to be inserted: 44
~~Circular Queue Overflow~~

~~Main Menu~~
==> 1. Insertion and Overflow Demo
==> 2. Deletion and Underflow Demo
==> 3. Display
==> 4. Exit
Enter Your Choice: 3
Circular Queue contents are:
Front[0]->   11  22  33 <-[2]Rear

~~Main Menu~~
==> 1. Insertion and Overflow Demo
==> 2. Deletion and Underflow Demo
==> 3. Display
==> 4. Exit
Enter Your Choice: 2
Deleted element from the queue is: 11

~~Main Menu~~
==> 1. Insertion and Overflow Demo
==> 2. Deletion and Underflow Demo
==> 3. Display
==> 4. Exit
Enter Your Choice: 2
Deleted element from the queue is: 22

~~Main Menu~~
==> 1. Insertion and Overflow Demo
==> 2. Deletion and Underflow Demo
==> 3. Display
==> 4. Exit
Enter Your Choice: 3
Circular Queue contents are:
Front[2]->   33 <-[2]Rear

~~Main Menu~~
==> 1. Insertion and Overflow Demo
==> 2. Deletion and Underflow Demo
==> 3. Display
==> 4. Exit
Enter Your Choice: 1
Enter the element to be inserted: 66

~~Main Menu~~
==> 1. Insertion and Overflow Demo
==> 2. Deletion and Underflow Demo
==> 3. Display
==> 4. Exit
Enter Your Choice: 3
Circular Queue contents are:
Front[2]->   33  66 <-[0]Rear

Linear Queue using an array

Linear Queue using an Array

#include<stdio.h>
#include<stdlib.h>
#define MAX 5

int front = -1;
int rear = -1;
int q[MAX];

void insert();
void delete();
void display();

void main()
{
            int ch;
            while(1)
            {
                        printf("\n\nMenu\n1.Insert\n2.Delete\n3.Display\n4.exit");
                        printf("\nEnter your choice:\t");
                        scanf("%d",&ch);
                        switch(ch)
                        {
                                    case 1:                         insert();
                                                            display();
`                                                           break;
                                    case 2:                         delete();
                                                            display();
                                                            break;
                                    case 3:                         display();
                                                            break;
                                    case 4:                         exit(0);
                                    default:            printf("\nInvalid Choice\n");
                        }
            }
}

void insert()
{
            int item;
            if(rear == MAX-1)
            {
                        printf("\n~~~Queue Overflow~~~");
                        return;
            }

            printf("\nEnter the item to to be inserted:");
            scanf("%d", &item);

            rear = rear+1;
            q[rear] = item;
}

void delete()
{
            int item;
            if(front == rear)
            {
                        printf("\n~~~Queue Underflow~~~");
                        return;
            }
            front = front+1;
            item = q[front];
            printf("\nThe item that got deleted is: %d", item);
}

void display()
{
            int i;
            printf("\nfront = %d, rear = %d",front, rear);
            if(front == rear)
            {
                        printf("\nQueue is empty\n");
                        return;
            }
            else
            {

                        printf("\nElements of the queue are: ");
                        for(i=front+1; i<=rear;i++)
                                                            printf("%d\t",q[i]);
            }
}


Output:
Menu
1.Insert
2.Delete
3.Display
4.exit
Enter your choice:      1
Enter the item to to be inserted: 11
front = -1, rear = 0
Elements of the queue are: 11

Menu
1.Insert
2.Delete
3.Display
4.exit
Enter your choice:      1
Enter the item to to be inserted:       22
front = -1, rear = 1
Elements of the queue are: 11   22

Menu
1.Insert
2.Delete
3.Display
4.exit
Enter your choice:      1
Enter the item to to be inserted:         33
front = -1, rear = 2
Elements of the queue are: 11   22   33

Menu
1.Insert
2.Delete
3.Display
4.exit
Enter your choice:      1
Enter the item to to be inserted:       44
front = -1, rear = 3
Elements of the queue are: 11   22    33   44


Menu
1.Insert
2.Delete
3.Display
4.exit
Enter your choice:      1
Enter the item to to be inserted: 55
front = -1, rear = 4
Elements of the queue are: 11   22  33  44  55

Menu
1.Insert
2.Delete
3.Display
4.exit
Enter your choice:      1
~~~Queue Overflow~~~
front = -1, rear = 4
Elements of the queue are: 11  22  33  44  55

Menu
1.Insert
2.Delete
3.Display
4.exit
Enter your choice:      2
The item that got deleted is:            11
front = 0, rear = 4
Elements of the queue are: 22   33  44  55

Menu
1.Insert
2.Delete
3.Display
4.exit
Enter your choice:      2
The item that got deleted is:             22
front = 1, rear = 4
Elements of the queue are: 33   44   55

Menu
1.Insert
2.Delete
3.Display
4.exit

Enter your choice:      2
The item that got deleted is:             33
front = 2, rear = 4
Elements of the queue are: 44   55

Menu
1.Insert
2.Delete
3.Display
4.exit
Enter your choice:      2
The item that got deleted is:             44
front = 3, rear = 4
Elements of the queue are: 55

Menu
1.Insert
2.Delete
3.Display
4.exit
Enter your choice:      2
The item that got deleted is: 55
front = 4, rear = 4
Queue is empty


Menu
1.Insert
2.Delete
3.Display
4.exit
Enter your choice:      2
~~~Queue Underflow~~~
front = 4, rear = 4
Queue is empty

Menu
1.Insert
2.Delete
3.Display
4.exit
Enter your choice:      4