Best Stack implementation code

Code for implementing Special Stack

For learning c code in more advanced form, students are advised to practice few codes related to pointer, dynamic memory allocation. For learning pointer, dynamic memory allocation you should go through this code. This is Code for implementing Special Stack

In C code practice, this Code for implementing Special Stack logic plays a very important role. So I have designed this code as easy as possible for the students. They will be able to understand each and every step very easily. 

Before going for this type of program a student should go through basics of pointer, dynamic memory allocation etc. So that they can easily go through each step with self explanation mode. 

For more code click here. For video tutorial on several topics click here

#include<stdio.h>
#include<stdlib.h>
#define max 50
typedef struct stack
{
int ar[max];
int top;
}st;
void push(st *,int);
int pop(st *);
int peek(st);
int isfull(st);
int isempty(st);
void disp(st);
void main()
{
int ch,g;
st s;
s.top=-1;
while(1)
{
printf(\"n1)Pushn2)Popn3)Peekn4)Dispn5)Exit\");
printf(\"n Enter your choice ; \");
scanf(\"%d\",&ch);
switch(ch)
{
case 1: if(!isfull(s))
 {
 printf(\"Enter the value : \");
 scanf(\"%d\",&g);
 push(&s,g);
 }
 else
 printf(\"No More Space\");
 break;
case 2: if(!isempty(s))
 printf(\"Popped value is %d\",pop(&s));
 else
 printf(\"Nothing to display \");
 break;
case 3: if(!isempty(s))
 {
 printf(\"Top value is %d\",peek(s));
 }
 else
 printf(\"Nothing to display\");
 break;
case 4: if(!isempty(s))
 {
 printf(\"Values in the stack are : \");
 disp(s);
 }
 else
 printf(\"Nothing to display\");
 break;
case 5: exit(0);
}
}
}
void push(st *s,int g)
{
s->ar[++s->top]=g;
}

int pop(st *s)
{
int p;
p=s->ar[s->top];
s->top--;
return p;
}
int peek(st s) { return s.ar[s.top]; } int isfull(st s) { if(s.top==max-1) return 1; else return 0; } int isempty(st s) { if(s.top==-1) return 1; else return 0; } void disp(st s) { int p=s.top; printf(\"nn\"); while(p>=0) { printf(\"|%d|n\",s.ar[p]); p--; } }


Run


Explanation Video

Leave a Comment

Your email address will not be published. Required fields are marked *