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

Stack

The document outlines a lab exercise for implementing a menu-driven program in C to perform basic stack operations. It describes the stack data structure, its LIFO nature, and details operations such as push, pop, display, check if empty, and check if full. The source code provided implements these functionalities using a maximum stack size of 5 and includes a simple user interface for interaction.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Stack

The document outlines a lab exercise for implementing a menu-driven program in C to perform basic stack operations. It describes the stack data structure, its LIFO nature, and details operations such as push, pop, display, check if empty, and check if full. The source code provided implements these functionalities using a maximum stack size of 5 and includes a simple user interface for interaction.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Name: Shristi Chapagain Date: 2079/05/02

Lab 2: Write a Program to implement menu driven basic operations of stack.

Stack
A stack is a collection of items of the same type. Stack follows the Last In First Out (LIFO)
fashion where the last element entered is the first one to be popped out. In stacks, the
insertion and deletion of elements happen only at one endpoint of it.
Operations performed on Stacks
The table shows the basic operations served by the Stack:

Push This function adds an element to the top of the Stack.


Pop This function removes the topmost element from the stack.
IsEmpty Checks whether the stack is empty.
IsFull Checks whether the stack is full.
Top Displays the topmost element of the stack.
Display Displays the element in stack

Programming Language: C
IDE: VS Code
.

Source Code:

#include <stdio.h>
#include <process.h>
#include <stdlib.h>

#define MAX 5 // Maximum number of elements that can be


stored
//declaring stack and functions
int top = -1, stack[MAX];
void push();
void pop();
void display();
//Initializing main function
void main()
{
int ch;
while (1) // infinite loop, will end when choice will be 4
{
printf("\n*** Stack Menu *");
printf("\n\n1.Push\n2.Pop\n3.Display\n4.Exit");
printf("\n\nEnter your choice(1-4):");
scanf("%d", &ch);
switch (ch)
{
.

case 1: //case for push


push();
break;
case 2: //case for pop
pop();
break;
case 3: //case for display
display();
break;
case 4: //case for exit
exit(0);
default:
printf("\nWrong Choice!!");
}
}
}
// user defined functions :
void push() // user defined functions for push
{
int val;
if (top == MAX - 1)
{
printf("\nStack is full!!");
}
else
{
.

printf("\nEnter element to push:");


scanf("%d", &val);
top = top + 1;
stack[top] = val;
}
}

void pop() // user defined functions for pop


{
if (top == -1)
{
printf("\nStack is empty!!");
}
else
{
printf("\nDeleted element is %d", stack[top]);
top = top - 1;
}
}

void display() // user defined functions for display


{
int i;
if (top == -1)
{
.

printf("\nStack is empty!!");
}
else
{
printf("\nStack is...\n");
for (i = top; i >= 0; --i)
printf("%d\n", stack[i]);
}
}

Output:
.

You might also like