Write a C program using pointers to implement a stack with all the operations.
Program to implement stack with its operations using pointers
#include<stdio.h>#include<conio.h>#include<stdlib.h>#define MAX 50int size;/* Defining the stack structure */structstack{int AR[MAX];int top;};/* Initializing the stack(i.e., top=-1) */void init_stk(structstack*st){
st->top=-1;}/* Entering the elements into stack */void push (structstack*st,int num){if(st->top == size-1){printf("\nStack overflow(i.e., stack full).");return;}
st->top++;
st->AR[st->top]= num;}//Deleting an element from the stack.int pop(structstack*st){int num;if(st->top ==-1){printf("\nStack underflow(i.e., stack empty).");returnNULL;}
num=st->AR[st->top];
st->top--;return num;}void display(structstack*st){int i;for(i=st->top;i>=0;i--)printf("\n%d",st->AR[i]);}voidmain(){int ele,opt,val;structstack ptr;
clrscr();
init_stk(&ptr);printf("\nEnter Stack Size :");scanf("%d",&size);while(1){printf("\n\n\tSTACK PRIMITIVE OPERATIONS");printf("\n1.PUSH");printf("\n2.POP");printf("\n3.DISPLAY");printf("\n4.QUIT");printf("\n");printf("\nEnter your option : ");scanf("%d",&opt);switch(opt){case 1:printf("\nEnter the element into stack:");scanf("%d",&val);
push(&ptr,val);break;case 2:
ele=pop(&ptr);printf("\nThe element popped from stack is : %d",ele);break;case 3:printf("\nThe current stack elements are:");
display(&ptr);break;case 4:
getch();exit(0);default:printf("\nEnter correct option!Try again.");}}}
My name is Pritesh Taral. I am working in well known MNC. as Java Developer. I am part time blogger loves writing articles on C/C++.
I am active on facebook using community fan page .One can Visit me @ Facebook Facebook Fan Page