0% found this document useful (0 votes)
5 views3 pages

Stack Operations and Usage Guide

A stack is a linear data structure that operates on a Last In, First Out (LIFO) principle, where the last element added is the first to be removed. Key operations include push, pop, peek, is_empty, and size, which allow for adding, removing, and inspecting elements in the stack. This document serves as a pre-read for a session on stacks in a Data Structures and Algorithms course.

Uploaded by

Ashish Deshmukh
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)
5 views3 pages

Stack Operations and Usage Guide

A stack is a linear data structure that operates on a Last In, First Out (LIFO) principle, where the last element added is the first to be removed. Key operations include push, pop, peek, is_empty, and size, which allow for adding, removing, and inspecting elements in the stack. This document serves as a pre-read for a session on stacks in a Data Structures and Algorithms course.

Uploaded by

Ashish Deshmukh
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

Stacks Pre read

Stack

A stack is a linear data structure that follows the Last In, First Out (LIFO) principle. This

means that the last element added to the stack will be the first one to be removed.

1. Stack Operations

The core operations in a stack are:

● Push: Adds an element to the top of the stack.


● Pop: Removes the element from the top of the stack.
● Peek: Retrieves the top element without removing it.
● IsEmpty: Checks if the stack is empty.
● Size: Returns the number of elements in the stack.

Example Pseudocode for Stack Operations:

Initialize:

stack ← empty list


Function push(item):

Add item to the top of the stack

Function pop():

IF stack is not empty THEN

Remove and return the top element of the stack

ELSE

PRINT "Stack is empty"

Function peek():

IF stack is not empty THEN

RETURN the top element of the stack

ELSE

PRINT "Stack is empty"

Function is_empty():

Return TRUE if stack is empty, otherwise FALSE

Function size():

Return the number of elements in the stack

//Example Usage (Pseudocode)

stack ← new Stack()


[Link](10)

[Link](20)

[Link](30)

pop_result ← [Link]() # Output: 30 (Last added element)

peek_result ← [Link]() # Output: 20 (Top element after popping 30)

is_empty_result ← stack.is_empty() # Output: FALSE (stack still

contains elements)

Note: This post serves as pre-reads for the session titled “Stack” in our DSA and System

Design course.

You might also like