Selasa, 03 Maret 2020

Data structure 2

SUMMARY:

Stack is a Data structure which is used to store data  in a practicular order, stack is a linear data structure which can be implemented  by either using an array or a linked list. two operation that can be performed on a stack are: push operation which inserts an element into a stack. Pop operation which removes the last element that was added intp the stack. it follows Last in first out (LIFO) Order

this is the program example that helps me to understand pop and push operation
I got this from https://www.sanfoundry.com/c-program-stack-implementation/
I'm just gonna copy paste the program from the website.

  1. #include <stdio.h>
  2. #define MAXSIZE 5
  3.  
  4. struct stack
  5. {
  6.     int stk[MAXSIZE];
  7.     int top;
  8. };
  9. typedef struct stack STACK;
  10. STACK s;
  11.  
  12. void push(void);
  13. int  pop(void);
  14. void display(void);
  15.  
  16. void main ()
  17. {
  18.     int choice;
  19.     int option = 1;
  20.     s.top = -1;
  21.  
  22.     printf ("STACK OPERATION\n");
  23.     while (option)
  24.     {
  25.         printf ("------------------------------------------\n");
  26.         printf ("      1    -->    PUSH               \n");
  27.         printf ("      2    -->    POP               \n");
  28.         printf ("      3    -->    DISPLAY               \n");
  29.         printf ("      4    -->    EXIT           \n");
  30.         printf ("------------------------------------------\n");
  31.  
  32.         printf ("Enter your choice\n");
  33.         scanf    ("%d", &choice);
  34.         switch (choice)
  35.         {
  36.         case 1:
  37.             push();
  38.             break;
  39.         case 2:
  40.             pop();
  41.             break;
  42.         case 3:
  43.             display();
  44.             break;
  45.         case 4:
  46.             return;
  47.         }
  48.         fflush (stdin);
  49.         printf ("Do you want to continue(Type 0 or 1)?\n");
  50.         scanf    ("%d", &option);
  51.     }
  52. }
  53. /*  Function to add an element to the stack */
  54. void push ()
  55. {
  56.     int num;
  57.     if (s.top == (MAXSIZE - 1))
  58.     {
  59.         printf ("Stack is Full\n");
  60.         return;
  61.     }
  62.     else
  63.     {
  64.         printf ("Enter the element to be pushed\n");
  65.         scanf ("%d", &num);
  66.         s.top = s.top + 1;
  67.         s.stk[s.top] = num;
  68.     }
  69.     return;
  70. }
  71. /*  Function to delete an element from the stack */
  72. int pop ()
  73. {
  74.     int num;
  75.     if (s.top == - 1)
  76.     {
  77.         printf ("Stack is Empty\n");
  78.         return (s.top);
  79.     }
  80.     else
  81.     {
  82.         num = s.stk[s.top];
  83.         printf ("poped element is = %dn", s.stk[s.top]);
  84.         s.top = s.top - 1;
  85.     }
  86.     return(num);
  87. }
  88. /*  Function to display the status of the stack */
  89. void display ()
  90. {
  91.     int i;
  92.     if (s.top == -1)
  93.     {
  94.         printf ("Stack is empty\n");
  95.         return;
  96.     }
  97.     else
  98.     {
  99.         printf ("\n The status of the stack is \n");
  100.         for (i = s.top; i >= 0; i--)
  101.         {
  102.             printf ("%d\n", s.stk[i]);
  103.         }
  104.     }
  105.     printf ("\n");
  106. }

and the Run Time Cases will be like this

STACK OPERATION
------------------------------------------
      1    -->    PUSH
      2    -->    POP
      3    -->    DISPLAY
      4    -->    EXIT
------------------------------------------
Enter your choice
1
Enter the element to be pushed
34
Do you want to continue(Type 0 or 1)?
0
$ a.out
STACK OPERATION
------------------------------------------
      1    -->    PUSH
      2    -->    POP
      3    -->    DISPLAY
      4    -->    EXIT
------------------------------------------
Enter your choice
1
Enter the element to be pushed
34
Do you want to continue(Type 0 or 1)?
1
------------------------------------------
      1    -->    PUSH
      2    -->    POP
      3    -->    DISPLAY
      4    -->    EXIT
------------------------------------------
Enter your choice
2
poped element is = 34
Do you want to continue(Type 0 or 1)?
1
------------------------------------------
      1    -->    PUSH
      2    -->    POP
      3    -->    DISPLAY
      4    -->    EXIT
------------------------------------------
Enter your choice
3
Stack is empty
Do you want to continue(Type 0 or 1)?
1
------------------------------------------
      1    -->    PUSH
      2    -->    POP
      3    -->    DISPLAY
      4    -->    EXIT
------------------------------------------
Enter your choice
1
Enter the element to be pushed
50
Do you want to continue(Type 0 or 1)?
1
------------------------------------------
      1    -->    PUSH
      2    -->    POP
      3    -->    DISPLAY
      4    -->    EXIT
------------------------------------------
Enter your choice
1
Enter the element to be pushed
60
Do you want to continue(Type 0 or 1)?
1
------------------------------------------
      1    -->    PUSH
      2    -->    POP
      3    -->    DISPLAY
      4    -->    EXIT
------------------------------------------
Enter your choice
3

The status of the stack is
60
50

Do you want to continue(Type 0 or 1)?
1
------------------------------------------
      1    -->    PUSH
      2    -->    POP
      3    -->    DISPLAY
      4    -->    EXIT
------------------------------------------
Enter your choice
4
========================================================================

There are 3 notation which are Infix, Postfix, and Prefix Notation.
Prefix : operator is written ahead of operator.
Infix   : it is characterized by the placement of operators between operands
Prefix : Operator is written after operands

example:

No
Infix Notation
Prefix noatation
Postfix notation
1
a+b
+ab
ab+
2
(a+b)*c
*+abc
ab+*c
3
a*(b+c)
*a+bc
abc+*
4
a/b+c/d
+/ab/cd
ab/cd/+

Thank you ^_^

Tidak ada komentar:

Posting Komentar