Assignment 10: Stack and Queue operations                                              Date: 2nd Apr, 07



Tutorial 10A:

Write a menu driven program in C language to implement a Stack and its operations using arrays.

Solution:

              #include<stdio.h>

            #define MAXLEN 10

            int stack[MAXLEN];

            int  main(){

            int element,ch,top=0,flag=1;

            while(flag)

                   {

                        printf("\n1.PUSH\n");

                        printf("\n2.POP\n");

                        printf("\n3.DISPLAY\n");

                        printf("\n4.EXIT\n");

                        printf("\nEnter your Choice :");

                        scanf("%d",&ch);

                        switch(ch)

                                {

                                     case 1:

                                                    printf("\nEnter the element to push :");

                                                  scanf("%d",&element);

 if(top<MAXLEN)

      {

                top++;

              stack[top]=element;

              printf("\n Element pushed successfully\n");

       }

 else

        printf("\nStack overflow\n");

        break;

                                     case 2:

 if(top>0)

       {

          top--;

          printf("\n Element popped successfully\n");

        }

 else

          printf("\nStack underflow\n");

          break;

                                        case 3:

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

       printf("\n %d",stack[i]);

   break;

                                     case 4:

    flag=0;

    break;

                                     default:

     break;

                                     }// end of switch

                        }// end of while

            }// end of main



Assign. 10A:

Write a menu driven C program to implement a stack and its operations using single linked list. Dynamic memory management is mandatory. Use the following structure definition for the elements of the stack.

struct node{

int data;

struct node *next;

};

Example:

Push scenario:

                      (top)

            stack :

push 1, stack : 1->null

push 4, stack : 4->1->null

push 9, stack : 9->4->1->null

push 5, stack : 5->9->4->1->null

 

Pop scenario:

                    (top) 

pop , stack :  9->4->1->null

……..



Tutorial 10B:

    Write a C program to implement queue and its operations.

Solution:

              #include<stdio.h>

            #define MAXLEN 10

            int queue[MAXLEN];

            int  main(){

            int element,ch,front=0,rear=0,flag=1;

            while(flag)

                   {

                        printf("\n1.INSERT\n");

                        printf("\n2.DELETE\n");

                        printf("\n3.DISPLAY\n");

                        printf("\n4.EXIT\n");

                        printf("\nEnter your Choice :");

                        scanf("%d",&ch);

                        switch(ch)

                                {

                                     case 1:

                                                    printf("\nEnter the element to insert :");

                                                  scanf("%d",&element);

 if(rear<MAXLEN)

      {

               queue[rear]=element;

             rear++;

              printf("\n Element inserted successfully\n");

       }

 else

        printf("\nqueue overflow\n");

        break;

                                     case 2:

 if(front<rear)

       {

          front++;

          printf("\n Element deleted successfully\n");

        }

 else

          printf("\nqueue underflow\n");

          break;

                                        case 3:

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

       printf(" %d",queue[i]);

   break;

                                     case 4:

    flag=0;

    break;

                                     default:

     break;

                                     }// end of switch

                        }// end of while

            }// end of main



Assign 10B:

      Change Tutorial 10B to manage the space efficiently using the concept of circular queue.

Assign 10C:

      Write a C program to implement a Deque and its operations using arrays.

Hint : In a deque (short for double-ended queue) elements can be added to or removed from the front or rear.

Example:

After successive insertions of 1,3,5,2,4 at rear 7,6 at front and deletion of 4,2 at rear of the deque the elements should be displayed as 6,7,1,3,5.



Assign. 10D:

    Write a C program to reverse the elements of stack, which is implemented by a linked list. Dynamic memory management is mandatory.

Example:

Input:    1-->3-->5-->2-->6-->0-->8-->7-->6-->4-->3

           (top)

Output: 3-->4-->6-->7-->8-->0-->6-->2-->5-->3-->1