Stack Using Array
Stack Using Array
#include<stdio.h> #include<conio.h> #define MAX 10 struct stack { int top; int element[MAX]; }; struct stack *s; void push(int value); int pop(); int peek(); int isfull(); int isempty(); void main() { int info,a,x,y; char ch; clrscr(); s->top=-1; do { printf("Enter your choice\n1:Push\n2:Pop\n"); printf("3:Access the top element\n"); scanf("%d",&a); if(a==1) { printf("Enter the element\n"); scanf("%d",&info); push(info); } else if(a==2) { x=pop(); if(x==-2) { printf("Stack Empty\n"); } else { printf("Element deleted: %d\n",x); } } else if(a==3) { y=peek(); printf("Top element: %d\n",y); } printf("Do you want to continue? y/n\n"); scanf(" %c",&ch);
void push(int value) { int x; x=isfull(); if(x==1) { return; } else { s->top++; s->element[s->top]=value; } } { int value,x; x=isempty(); if(x==1) { return -2; } else { value=s->element[s->top]; s->top--; return value; }
int peek() { return (s->element[s->top]); } int isfull() { if(s->top==MAX) { printf("Stack Overflow\n"); return 1; } else return 0; } int isempty() { if(s->top==-1) { return 1; }
else return 0;
Output