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.
#include <stdio.h>#define MAXSIZE 5struct stack{int stk[MAXSIZE];
int top;
};
typedef struct stack STACK;
STACK s;void push(void);
int pop(void);
void display(void);
void main ()
{int choice;
int option = 1;
s.top = -1;
printf ("STACK OPERATION\n");
while (option)
{printf ("------------------------------------------\n");
printf (" 1 --> PUSH \n");
printf (" 2 --> POP \n");
printf (" 3 --> DISPLAY \n");
printf (" 4 --> EXIT \n");
printf ("------------------------------------------\n");
printf ("Enter your choice\n");
scanf ("%d", &choice);
switch (choice)
{case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
return;
}fflush (stdin);
printf ("Do you want to continue(Type 0 or 1)?\n");
scanf ("%d", &option);
}}/* Function to add an element to the stack */void push ()
{int num;
if (s.top == (MAXSIZE - 1))
{printf ("Stack is Full\n");
return;
}else{printf ("Enter the element to be pushed\n");
scanf ("%d", &num);
s.top = s.top + 1;
s.stk[s.top] = num;
}return;
}/* Function to delete an element from the stack */int pop ()
{int num;
if (s.top == - 1)
{printf ("Stack is Empty\n");
return (s.top);
}else{num = s.stk[s.top];
printf ("poped element is = %dn", s.stk[s.top]);
s.top = s.top - 1;
}return(num);
}/* Function to display the status of the stack */void display ()
{int i;
if (s.top == -1)
{printf ("Stack is empty\n");
return;
}else{printf ("\n The status of the stack is \n");
for (i = s.top; i >= 0; i--)
{printf ("%d\n", s.stk[i]);
}}printf ("\n");
}
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