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

Introduction to Stack-1

Uploaded by

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

Introduction to Stack-1

Uploaded by

sonichirag1301
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

What is a Stack?

A stack is a linear data structure that follows the LIFO (Last In, First Out) principle.
This means the last element added to the stack will be the first one to be removed.

Characteristics of a Stack

1.​ LIFO Principle:


○​ Last element added is the first to be removed.
2.​ Operations:
○​ Push: Add an element to the top of the stack.
○​ Pop: Remove the top element of the stack.
○​ Peek/Top: View the top element without removing it.
○​ isEmpty: Check if the stack is empty.

Visualization of a Stack

Imagine a stack of plates:

●​ You can only add or remove plates from the top.


●​ Adding a plate = Push.
●​ Removing a plate = Pop.

Initial Stack: []
Push 5: [5]
Push 10: [5, 10]
Push 20: [5, 10, 20]
Pop: [5, 10] (20 removed)
Peek: 10 (top element)

Algorithm for PUSH Operation in a Stack

The PUSH operation is used to add an element to the top of the stack.

Steps for the PUSH Algorithm


1.​ Check for Overflow:
○​ If the stack is full, the PUSH operation cannot be performed.
○​ Handle the overflow condition by showing an error or stopping further
operations.
2.​ Increment the Top Pointer:
○​ Move the top pointer (or index) to the next empty position in the stack.
[top=top+1]
3.​ Insert the New Element:
○​ Add the new element at the position indicated by the top pointer.
4.​ End:
○​ The operation is complete.

Algorithm for POP Operation in a Stack

The POP operation is used to remove the top element from the stack and return it.

Steps for the POP Algorithm

1.​ Check for Underflow:


○​ If the stack is empty (i.e., top == -1), the POP operation cannot be
performed.
○​ Handle the underflow condition by showing an error or stopping further
operations.
2.​ Access the Top Element:
○​ Retrieve the element currently at the top of the stack.
3.​ Decrement the Top Pointer:
○​ Move the top pointer one step down to indicate that the top element has
been removed. [top=top-1]
4.​ Return the Removed Element:
○​ The operation is complete, and the removed element is returned.
Sample Example of Push and Pop Operations on a Stack

Problem Statement:

Perform the following operations on a stack:

1.​ Push the elements: 5,10,15,205, 10, 15, 205,10,15,20.


2.​ Pop the top element.
3.​ Push 252525 and 303030.
4.​ Pop two elements.
5.​ Display the stack after each operation.

Example Execution

Initial Stack (Empty):

Stack: []
In summary,

Pushed: 5 -> Stack: [5]

Pushed: 10 -> Stack: [5, 10]

Pushed: 15 -> Stack: [5, 10, 15]

Pushed: 20 -> Stack: [5, 10, 15, 20]

Popped: 20 -> Stack: [5, 10, 15]

Pushed: 25 -> Stack: [5, 10, 15, 25]

Pushed: 30 -> Stack: [5, 10, 15, 25, 30]

Popped: 30 -> Stack: [5, 10, 15, 25]

Popped: 25 -> Stack: [5, 10, 15]

Final Stack: [5, 10, 15]

You might also like