0% found this document useful (0 votes)
127 views

Implementation of Stack Operation

This C program implements a stack using an array. It defines functions to push, pop, and peek items from the stack. The main function displays a menu to choose these operations and calls the corresponding functions. The push function adds an item to the stack if not full. Pop removes and returns the top item if the stack is not empty. Peek returns the top item without removing it, checking for empty stack.

Uploaded by

Goldy Batra
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
127 views

Implementation of Stack Operation

This C program implements a stack using an array. It defines functions to push, pop, and peek items from the stack. The main function displays a menu to choose these operations and calls the corresponding functions. The push function adds an item to the stack if not full. Pop removes and returns the top item if the stack is not empty. Peek returns the top item without removing it, checking for empty stack.

Uploaded by

Goldy Batra
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

/* implementation of stack operation */

#include<stdio.h>
#include<conio.h>
void push(int b[ ]);
void pop(int b[ ]);
void peek(int b[ ]);
#define MAXSIZE 10
int top=-1;
void main()
{
int a[MAXSIZE],ch;
do
{
printf("enter your choice 1 push,2 pop, 3 peek, 4 exit");
scanf("%d",&ch);
switch(ch)
{
case 1:
push(a);
break;
case 2:
pop(a);
break;
case 3:
peek(a);
break;
}
} while(ch!=4);
getch();
}
void push(int b[ ])
{
int item;
if(top==MAXSIZE-1)
{
printf("overflow");
}

else
{
printf("enter new item");
scanf("%d",&item);
top=top+1;
b[top]=item;
}
}
void pop(int b[])
{
int item;
if(top==-1)
{
printf("underflow");
}
else
{
item=b[top];
top=top-1;
printf("item removed=%d",item);
}
}
void peek(int b[])
{
int item;
if(top==-1)
{
printf("underflow");
}
else
{
item=b[top];
printf("item at top=%d",item);
}
}

You might also like