Created
May 20, 2017 18:24
-
-
Save muhammedeminoglu/4bdcd2a16baf0e3f9b4916fd24768f18 to your computer and use it in GitHub Desktop.
Stack operations, Push, pop, peek with array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <stdbool.h> | |
| #define STACKLIMIT 5 | |
| // ************ Muhammed Eminoglu ************ | |
| // *********www.algoritmauzmani.com*********** | |
| // *********www.muhammedeminoglu.com********** | |
| int stack[STACKLIMIT - 1]; | |
| int top = -1; | |
| bool checkFull() | |
| { | |
| if(top >= STACKLIMIT - 1) | |
| { | |
| return true; | |
| } | |
| else | |
| { | |
| return false; | |
| } | |
| } | |
| bool checkEmpty() | |
| { | |
| if(top < 0) | |
| { | |
| return true; | |
| } | |
| else | |
| { | |
| return false; | |
| } | |
| } | |
| void push(int item) | |
| { | |
| bool control = checkFull(); | |
| if(control == false) | |
| { | |
| top = top + 1; | |
| stack[top] = item; | |
| } | |
| else | |
| { | |
| printf("\nStackoverflow!!! ... "); | |
| } | |
| } | |
| void pop() | |
| { | |
| bool control = checkEmpty(); | |
| if(control == false) | |
| { | |
| top = top - 1; | |
| } | |
| else | |
| { | |
| printf("\n Your Stack is already empty you cant pop anything... "); | |
| } | |
| } | |
| int peek() | |
| { | |
| bool control = checkEmpty(); | |
| if(control == true) | |
| { | |
| printf("\n There is no item here ..."); | |
| return 0; | |
| } | |
| return stack[top]; | |
| } | |
| void printStack() | |
| { | |
| int i; | |
| printf("\n ************************ \n"); | |
| for( i = 0; i < top + 1; i++) | |
| { | |
| printf("%d ", stack[i]); | |
| } | |
| } | |
| int main() | |
| { | |
| int choise, item; | |
| int a; | |
| while(1 == 1) | |
| { | |
| printf("\n 1- Push an item"); | |
| printf("\n 2- Pop an item"); | |
| printf("\n 3- Peek (it shows top element of your stack ... "); | |
| scanf("%d", &choise); | |
| switch(choise) | |
| { | |
| case 1: | |
| printf("\n Which number do you want to add? ... "); | |
| scanf("%d", &item); | |
| push(item); | |
| printStack(); | |
| break; | |
| case 2: | |
| pop(); | |
| printStack(); | |
| break; | |
| case 3: | |
| a = peek(); | |
| printf("\n Your Stack's Top element is => %d", a); | |
| break; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment