Created
September 21, 2016 11:14
-
-
Save devils-ey3/3d780045d794d61be199269cf100911b to your computer and use it in GitHub Desktop.
Stack by c without pointer
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> | |
#define max 10 | |
int i=0; | |
int array[max]; | |
void printInstruction() | |
{ | |
printf("Press 1 for push\nPress 2 for pop\nPress 3 for display full list\nPress 4 for display top\nPress ctrl+z for exit\n"); | |
} | |
void push(int element) | |
{ | |
if (i==max){ | |
printf("There are no space in the array\n"); | |
return; | |
} | |
array[i]=element; | |
i++; | |
} | |
int pop() | |
{ | |
if (i==0) | |
{ | |
printf("The stack is empty\n"); | |
return; | |
} | |
int j; | |
j=array[--i]; | |
return j; | |
} | |
void display_list() | |
{ | |
if (i==0) | |
{ | |
printf("NOthing to display \n"); | |
return ; | |
} | |
int loop; | |
printf("The list is : "); | |
for (loop=--i;loop>=0;loop--) | |
printf("%d ",array[loop]); | |
printf("\n"); | |
i++; | |
} | |
int display_last_entry() | |
{ | |
int j=array[--i]; | |
i++; | |
return j; | |
} | |
int main() | |
{ | |
int j; | |
printf("You can enter 10 data in stack \n\n"); | |
printInstruction(); | |
int choise; | |
int element; | |
while(scanf("%d",&choise)==1) | |
{ | |
system("cls"); | |
if (choise==1) | |
{ | |
printf("Enter the number for push : "); | |
scanf("%d",&j); | |
system("cls"); | |
push(j); | |
} | |
else if (choise==2) | |
{ | |
system("cls"); | |
printf("%d is poped\n",pop()); | |
} | |
else if (choise==3) | |
{ | |
system("cls"); | |
display_list(); | |
} | |
else if (choise==4) | |
{ | |
system("cls"); | |
printf("The last element is %d\n",display_last_entry()); | |
} | |
printInstruction(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment