Skip to content

Instantly share code, notes, and snippets.

@muhammedeminoglu
Created May 20, 2017 18:24
Show Gist options
  • Save muhammedeminoglu/4bdcd2a16baf0e3f9b4916fd24768f18 to your computer and use it in GitHub Desktop.
Save muhammedeminoglu/4bdcd2a16baf0e3f9b4916fd24768f18 to your computer and use it in GitHub Desktop.
Stack operations, Push, pop, peek with array
#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