EXPERIMENT NO.
-1
Date:11/07/24
Aim: Implement Stack ADT using array.
Requirement: Turbo C
Theory:
Array:- Arrays are defined as the collection of similar types of data items
stored at contiguous memory locations. It is one of the simplest data
structures where each data element can be randomly accessed by using
its index number.
Stacks:- A stack is a linear data structure that follows the principle of Last In First
Out (LIFO). This means the last element inserted inside the stack is removed first.
Stack Operations:
• Push: Insert an element onto the stack.
• Pop: Remove the top element from the stack.
• Peek: View the top element without removing it.
• isEmpty: Check if the stack is empty.
• isFull: Check if the stack is full.
Push():-The push() is an operation that inserts elements into the stack.
Algorithm-
1. Checks if the stack is full.
2. If the stack is full, produces an error and exit.
3. If the stack is not full, increments top to point
next empty space.
4. Adds data element to the stack location, where
top is pointing.
5. Returns success.
Pop():-When we delete an element from the stack, the operation is known as a
pop.
Alogrithm-
1. Checks if the stack is empty.
2. If the stack is empty, produces an error and exit.
3. If the stack is not empty, accesses the data element
at which top is pointing.
4. Decreases the value of top by 1.
5. Returns success.
Peek():The peek operation retrieves the top element of the stack without removing it
.
Algorithm-
1: Start
.
2: If top == -1, print "Stack is empty" and return -1
.
3: Else, return the element at stack[top].
4: End.
isEmpty(): operation checks whether the stack is empty.
Algorithm-
1 Start.
2: If top == -1, return TRUE (stack is empty)
.
3: Else, return FALSE (stack is not empty).
4: End.
isFull(): operation checks whether the stack has reached its maximum capacity.
Algorithm-
1: Start.
2: If top == MAX - 1, return TRUE (stack is full).
3: Else, return FALSE (stack is not full).
4: End.
PART B
Roll No.: 76 Name: Sandesh Bhagat
Class : SE-C (COMPS) Batch : C4
Date of Experiment: Date of Submission :
Grade :
SOURCE CODE:
INPUT & OUTPUT:
Observations and learning:
In this practical, we learned different stack operations that can be performed on a stack using an array.
Conclusion
we successfully implemented a stack using an array in C. We performed fundamental stack operations such as
push, pop, peek, and checked conditions like isEmpty and isFull. This helped in understanding the Last In First
Out (LIFO) principle .