Thanks to visit codestin.com
Credit goes to www.scribd.com

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

2.stack Implementation Using Array-1

The document outlines an experiment for SE [E&TC] students at Sinhgad Institute of Technology, focusing on implementing a stack using arrays to simulate a parcel handling system at a post office. It includes objectives, primitive operations, a C program for stack operations (push, pop, display), and theoretical concepts related to stacks. Additionally, it provides questions for assessment and suggestions for further experiments.

Uploaded by

kaustubhudavant
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views5 pages

2.stack Implementation Using Array-1

The document outlines an experiment for SE [E&TC] students at Sinhgad Institute of Technology, focusing on implementing a stack using arrays to simulate a parcel handling system at a post office. It includes objectives, primitive operations, a C program for stack operations (push, pop, display), and theoretical concepts related to stacks. Additionally, it provides questions for assessment and suggestions for further experiments.

Uploaded by

kaustubhudavant
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

SE [E&TC] Sinhgad Institute of Technology, Lonavala DSA

Name of the Student : ____________________________________ Roll No : _____

Class : S.E. [E&TC] Division : _______ Course : DSA


Experiment No. 2
** Implement stack using array **
Marks: /10

Date of Performance: / /20 Sign with Date

AIM : Simulation of a Parcel Handling System at a Post Office using Stack (LIFO)
implemented with arrays.

 Objectives:
To write a C program to simulate the handling of parcels at a post office using the Stack
data structure.
1. Implement push (add parcel),
2. Implement pop (remove parcel),
3. Implement display (show parcels).
This follows the Last In First Out (LIFO) principle.
4. Outcomes:
1. This assignment will help the students to realize how the different operation on
stack like push, pop, display can be implemented.

5. PEOs ,POs, PSOs and COs satisfied


PEOs : 1 POs : 1,3 PSOs : 1 COs :2

 Software used :CODE BLOCK

 Theory :
Stack
A stack is an ordered collection of items into which items may be inserted and deleted from
one end called the top of the stack. The stack operates in a LIFO (Last In First Out) manner
i.e. the element which is put in Last is the First to come out.

Primitive Operations on Stack

1 DEPARTMENT OF ELECTRONICS AND TELECOMMUNICATION ENGG


SE [E&TC] Sinhgad Institute of Technology, Lonavala DSA

i. Create : Creates a new stack. This operation creates a new stack, which is empty
ii. Push : Adds an element to the stack. The push operation inserts a new element at the
top of the stack. Top now points to this new element.
iii. Pop : Removing an element from the stack. The pop operation removes the topmost
element from the stack thereby reducing the size of the stack by 1. The element below
it now becomes the topmost element.
iv. Isempty : Checks whether a stack is empty. This operation returns TRUE if the stack
is empty and false otherwise. This required for the pop operation because we cannot
pop from an empty stack.
v. Isfull : Checks whether a stack is full. This operation returns TRUE if the stack is full
and false otherwise. This required for the push operation because we cannot push an
element into the stack when the stack is already full.

Data Structures Used


Arrays
#define MAX 20
struct stack
{
int top;
int items[MAX];
};

Algorithm:

Create a stack
1. Start
2. Declare variables for the stack (struct stack s )
3. Initialize the stack (s.top = -1)
4. Stop

Push into stack


1. Start
2. Accept the element to be push
3. Check whether the stack is full ? if yes Display Stack full and go to step 6
2 DEPARTMENT OF ELECTRONICS AND TELECOMMUNICATION ENGG
SE [E&TC] Sinhgad Institute of Technology, Lonavala DSA

4. Increment the value of top


5. Store the element at top position
6. Stop
Pop from stack
1. Start
2. Check whether the stack is empty ? if yes Display Stack empty and go to step 6
3. Get the element present on the top of stack
4. Display the element which is poped
5. Decrement the value of top
6. Stop
Check for stackempty
1. Start
2. Check whether the s.top == -1 ? if yes return TRUE else return FALSE
3. Stop
Check for stackfull
1. Start
2. Check whether the s.top == MAX-1 ? if yes return TRUE else return FALSE
3. Stop
C Program
#include <stdio.h>
#include <stdlib.h>
#define MAX 5 // maximum number of parcels

int stack[MAX], top = -1;

// Function to push a parcel


void push(int parcel) {
if (top == MAX - 1) {
printf("Stack Overflow! Cannot add parcel %d.\n", parcel);
} else {
stack[++top] = parcel;
printf("Parcel %d pushed to stack.\n", parcel);
}
}

// Function to pop a parcel


void pop() {
if (top == -1) {
printf("Stack Underflow! No parcel to remove.\n");
} else {
printf("Parcel %d popped from stack.\n", stack[top--]);
3 DEPARTMENT OF ELECTRONICS AND TELECOMMUNICATION ENGG
SE [E&TC] Sinhgad Institute of Technology, Lonavala DSA

}
}

// Function to display parcels


void display() {
if (top == -1) {
printf("Stack is empty.\n");
} else {
printf("Stack (Top -> Bottom): ");
for (int i = top; i >= 0; i--) {
printf("%d ", stack[i]);
}
printf("\n");
}
}

// Main function
int main() {
int choice, parcel;

while (1) {
printf("\n--- Parcel Handling using Stack (LIFO) ---\n");
printf("1. Push Parcel\n");
printf("2. Pop Parcel\n");
printf("3. Display Parcels\n");
printf("4. Exit\n");
printf("Enter choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter parcel number: ");
scanf("%d", &parcel);
push(parcel);
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("Invalid choice!\n");
}
}
return 0;
}

4 DEPARTMENT OF ELECTRONICS AND TELECOMMUNICATION ENGG


SE [E&TC] Sinhgad Institute of Technology, Lonavala DSA

 Conclusion/s :

A. Write short answer of following questions :


1. Define Stack? List out primitive operations on Stack
2. What are the applications of Stack?
3. Give an algorithm to evaluate postfix expressions
B. Viva Questions:
4. What is a Stack?
5. What is Stack Overflow?
6. What is Stack Underflow?
7. How do you represent the top of the stack in an array implementation?
8. Time complexity of Push and Pop operations?

A. Design Experiments/ Lab innovations ( over and above )


1. Convert an infix expression to postfix write a C function for the same,take any
expression.
2. Write a program that reads a line from the terminal and determines whether or not it
is a palindrome.(A palindrome is a string that is the same spelled forward or backword
For eg: MADAM).

5 DEPARTMENT OF ELECTRONICS AND TELECOMMUNICATION ENGG

You might also like