What is Stack in Data Structures ?
For Example ------ A bundle of Books in Library
Bundles of Plates etc.
Operations Performs on Stack
There are two operations perform on Stack -----
1.) Push
2.) Pop
1,) Push--When we insert elements in stack then it is called Push operation.
Algorithm for Push Operation------
1.) Check if the stack is full or not.we declare a variable max and initialize with array of size and we check condition if top of the stack == to max-1 then we print a message to user StackOverflow.
2.) If the stack is not full, then increment the top=-1 to top++ and add the element in to the stack.
Algorithm for POP operation---------
1.) Check if the stack is empty or not by using condition if(top==-1) then print stack underflow.2.) If the stack is empty, then print error of underflow and exit the program.If the stack is not empty, then print the element at the top and decrement the top=-1 to top-- .
Implementation Of Stack Using C-Language
Explanation of program in easy steps ------
- First we declare variable top and initialize with -1 because array starts with 0 and after increment -1 converts in to 0 that's -1+1=0.
- we create an array name int array[max] . we declare after header file #define max 5 using symbolic constant that's means we write max instead of 5.
- we take int variable choice for switch case and int value for insert in stack and int i for loop.
- we create 3 methods for stack void push() , void pop() , void display() .
- In push method we check condition if stack array size == to top then stack will be Overflow otherwise we insert element and top==-1 will be increment and array[top] means array[0] = value ;
- In pop method we check condition if stack top == -1 then stack will be Underflow no elements to be pop otherwise we delete element and top==-1 will be decrement back to -1 from 0 and array[top] means array[0] = value will be delete .
- In display method we again check if top==-1 then we print stack is empty no elements are present in stack. else we take a for loop initialize with 0 and goes to top of the stack means if we push 2 value in the stack then after increment stack will be 0 , 1 and i=0 and i=1 and for loop prints array[0]=value , array[1]=value.Last Explanation is we declare and define all functions and function related variable outside the main method because in c language we don't declare functions inside the main method so we declare it Globally.
Here is the full source code in C-Programming in easy steps------
Download this full code press here
Here is the output of this code -------
Enter your specific operation----
1.) PUSH--
2.) POP--
3. DISPLAY--
1
Enter value to be insert in Stack--
95
--Value are successfully inserted--
--Do you want to continue press 1 for yes other key for no--
1
1.) PUSH--
2.) POP--
3. DISPLAY--
1
Enter value to be insert in Stack--
78
--Value are successfully inserted--
--Do you want to continue press 1 for yes other key for no--
1
1.) PUSH--
2.) POP--
3. DISPLAY--
3
Elements are stack are----
95
78
--Do you want to continue press 1 for yes other key for no--
1
1.) PUSH--
2.) POP--
3. DISPLAY--
2
Elements from stack are Pop--78
--Do you want to continue press 1 for yes other key for no--
1
1.) PUSH--
2.) POP--
3. DISPLAY--
3
Elements are stack are----
95
--Do you want to continue press 1 for yes other key for no--
2
Compilation terminate because of wrong input.
--------------------------------
Process exited after 20.21 seconds with return value 0
Press any key to continue . . .
#StayHome
Also read-Java Program For Musical Instruments
0 Comments
Write your comments or feedback here.........