0% found this document useful (0 votes)
4 views5 pages

Stack Programs

The document outlines a C program that implements stack operations (Push, Pop, Display) using arrays. It details the program's aim, input/output requirements, and provides the code along with explanations of stack concepts such as overflow and underflow. Additionally, it lists various applications of stacks and includes sample viva questions related to stack operations.

Uploaded by

M Vinitha
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)
4 views5 pages

Stack Programs

The document outlines a C program that implements stack operations (Push, Pop, Display) using arrays. It details the program's aim, input/output requirements, and provides the code along with explanations of stack concepts such as overflow and underflow. Additionally, it lists various applications of stacks and includes sample viva questions related to stack operations.

Uploaded by

M Vinitha
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

Experiment 1: Stack Operations using Arrays

1. Aim

To write a C program to implement stack operations (Push, Pop, Display) using arrays.

2. Input

 User choice for stack operation (Push/Pop/Display/Exit)


 Element to be inserted into the stack

3. Output

 Stack after performing operations


 Messages for overflow, underflow, and stack contents

4. Tools Used

 Programming Language: C
 Editor
 Compiler: GCC
 Platform: Ubuntu

5. Program

#include <stdio.h>
#include <stdlib.h>
#define MAX 5

int stack[MAX];
int top = -1;

void push();
void pop();
void display();
void peek();

void main()
{
int choice;
while(1)
{
printf("\n--- STACK OPERATIONS ---\n");
printf("1. Push\n 2. Pop\n 3. Display\n4. Peek\[Link]\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1: push(); break;
case 2: pop(); break;
case 3: display(); break;
case 4: peek(); break;
case 5: printf("Exiting...\n"); exit(0);
default: printf("Invalid choice\n");
}
}
}
//Implementation of push
void push()
{
int item;
if(top == MAX-1)
printf("Stack Overflow\n");
else
{
printf("Enter element to push: ");
scanf("%d", &item);
stack[++top] = item;
}
}
// IMplementataion of pop
void pop()
{ if(top == -1)
printf("Stack Underflow\n");
else
printf("Popped element = %d\n", stack[top--]);
}
//Implementation of display
void display()
{ int i;
if(top == -1)
printf("Stack is empty\n");
else
{
printf("Stack elements are:\n");
for(i = top; i >= 0; i--)
printf("%d\n", stack[i]);
}
}
//Implementation of peek
void peek()
{ if(top == -1)
printf("Stack is empty\n");
else
printf("Top element = %d\n", stack[top]);
}
6. Actual Output
--- STACK OPERATIONS ---
1. Push
2. Pop
3. Display
4. Peek
5. Exit
Enter your choice: 1
Enter element to push: 10

Enter your choice: 1


Enter element to push: 20

Enter your choice: 4


Top element = 20

Enter your choice: 3


Stack elements are:
20
10

7. Result

Thus, the stack operations (push, pop, and display) were


successfully implemented using arrays and executed through a
menu-driven C program.

Sample Viva Questions:

 Define stack.
A stack is a linear data structure that follows the
LIFO (Last In First Out) principle.
 What is LIFO principle?
LIFO means the element inserted last is removed
first.
 List basic operations of stack.
Push, Pop, Peek (Top), and Display.
 What is stack overflow?
It is the condition when an element is pushed onto a
full stack.
 What is stack underflow?
It is the condition when pop is performed on an empty
stack.
 What is peek operation?
Peek returns the top element of the stack without
removing it.
 Write the condition for empty stack (array).
top = -1
 Write the condition for full stack (array).
top = MAX - 1
 What is the use of top variable in stack?
It stores the index/position of the top element in
the stack.
 Give two applications of stack.
Expression evaluation and function calls (recursion).
 Name two ways to implement stack.
Array implementation and linked list implementation.
 What is the time complexity of push operation?
O(1)
 What is the time complexity of pop operation?
O(1)
 What happens when pop is applied on empty stack?
Stack underflow occurs.
 What happens when push is applied on full stack?
Stack overflow occurs.
 Stack follows which order: FIFO or LIFO?
LIFO.
 Give one real-life example of stack.
Stack of plates.
 Which data structure is used in recursion?
Stack.
 Can we insert/delete from middle in stack?
No, insertion and deletion are allowed only at the
top.
 Write any two differences between stack and queue.
Stack follows LIFO, queue follows FIFO.
In stack insertion/deletion at one end, in queue at
both ends
Applications of Stack
[Link] Evaluation
Used to evaluate postfix and prefix expressions.
[Link] Conversion
Converts infix expressions to postfix or prefix.
[Link] Calls (Call Stack)
Stores function calls, local variables, and return
addresses during program execution.
[Link] Handling
Each recursive call is stored in the stack.
[Link] Checking
Checks balanced parentheses in expressions and
programs.
[Link]/Redo Operations
Used in editors to store previous states of actions.
[Link] Algorithms
Used in maze solving, puzzle solving, and DFS
traversal.
[Link] Parsing in Compilers
Helps in parsing expressions and checking syntax
correctness.
[Link] Data
Stack can reverse strings, numbers, or lists.
10. Browser Navigation
Back and forward buttons use stack to store visited
pages.

You might also like