Page 163 - DCAP201_FUNDAMENTALS_OF_DATA_STRUCTURES
P. 163

Fundamentals of Data Structures




                    Notes            //Deleting an element from the stack.
                                     int pop(struct stack *st)
                                     {
                                         int num;
                                         if(st->top == -1)
                                             {
                                             printf("nStack underflow(i.e., stack empty).");
                                             return NULL;
                                             }
                                         num = st->arr[st->top];
                                         st->top--;
                                         return num;
                                     }
                                     void display(struct stack *st)
                                     {
                                         int i;
                                         for(i=st->top;i>=0;i--)
                                             printf("n%d",st->arr[i]);
                                     }
                                     int main()
                                     {
                                         int element,opt,val;
                                         struct stack ptr;
                                         init_stk(&ptr);
                                         printf("nEnter Stack Size :");
                                         scanf("%d",&size);
                                         while(1)
                                         {
                                             printf("nntSTACK PRIMITIVE OPERATIONS");
                                             printf("n1.PUSH");
                                             printf("n2.POP");
                                             printf("n3.DISPLAY");
                                             printf("n4.QUIT");
                                             printf("n");
                                             printf("nEnter your option : ");
                                             scanf("%d",&opt);
                                             switch(opt)
                                             {
                                                 case 1:
                                                     printf("nEnter the element into stack:");
                                                     scanf("%d",&val);

                                                                                                         Contd...



          156                               LOVELY PROFESSIONAL UNIVERSITY
   158   159   160   161   162   163   164   165   166   167   168