0% found this document useful (0 votes)
29 views6 pages

Understanding Stack Data Structure

The document provides an overview of the stack data structure, emphasizing its Last In, First Out (LIFO) principle and detailing various operations such as push, pop, peek, and size. It includes user-defined Python functions for manipulating stacks containing different types of records, including large numbers, colors, keyboards, and country populations. Additionally, it describes functions for handling vowels in a string and managing stack operations effectively.

Uploaded by

addxca
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)
29 views6 pages

Understanding Stack Data Structure

The document provides an overview of the stack data structure, emphasizing its Last In, First Out (LIFO) principle and detailing various operations such as push, pop, peek, and size. It includes user-defined Python functions for manipulating stacks containing different types of records, including large numbers, colors, keyboards, and country populations. Additionally, it describes functions for handling vowels in a string and managing stack operations effectively.

Uploaded by

addxca
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

Stack:

A stack is a data structure.


A data structure is a way of organizing data to enable efficient access and modifications.
It defines a set of operations that can be performed on the data.

Stack 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 i.e. top element
will be removed first always. And when some new item is added it will always be added at the top.

Operations on a Stack:
1. Push: It adds an item to the top of the stack.
2. Pop: It removes the item from the top of the stack.
3. Peek (or Top): display the item on the top of the stack without removing it.
4. isEmpty: Checks if the stack is empty. Returns True if its empty otherwise False.
5. size: Returns the number of elements in the stack.
6. Removes all the elements in stack one by one and display msg when stack is empty.
7. Peek all the elements without displaying them. Show message if stack is empty.
Ques : 1 Write the following user defined functions in Python and perform the specified
operations on a stack named BigNums.

(i) PushBig() : It checks every number from the list Nums and pushes all such numbers
which have 5 or more digits into the stack, BigNums.

(ii) PopBig() : It pops the numbers from the stack, BigNums and displays them. The
function should also display "Stack Empty" when there are no more numbers left in the
stack.

Nums = [213,10025,167,254923,14,1297653,31498,386,92765]
Ques 2: A stack, named ClrStack, contains records of some colors.
Each record is represented as a tuple containing four elements – ColorName, RED, GREEN, BLUE.
ColorName is a string, and RED, GREEN, BLUE are integers.
For example, a record in the stack may be ('Yellow', 237, 250, 68)

Write the following user-defined functions in Python to perform the specified operations on ClrStack:

(i) push_Clr(ClrStack, new_Clr) : This function takes the stack ClrStack and a new record new_Clr
as arguments and pushes this new record onto the stack.

(ii) pop_Clr(ClrStack) : This function pops the topmost record from the stack and returns it. If the stack
is already empty, the function should display the message "Underflow".

(iii) isEmpty(ClrStack) : This function checks whether the stack is empty. If the stack is empty, the
function should return True, otherwise the function should return False.
Ques:A stack named KeyStack contains records of some computer keyboards.

Each record is represented as a list containing Make, Keys, Connectivity. The Make and
Connectivity are strings, and Keys is an integer. For example, a record in the stack may be
('Hitech', 105, 'USB').

Write the following user-defined functions in Python to perform the specified operations on
KeyStack :

(I) push_key(KeyStack, new_key): This function takes the stack KeyStack and a new
record new_key as arguments and pushes this new record onto the stack.
(II) pop_key(KeyStack): This function pops the topmost record from the stack and
returns it. If the stack is already empty, the function should display the message
“Underflow”.
(III) isEmpty(KeyStack): This function checks whether the stack is empty. If the stack is
empty, the function should return True, otherwise the function should return
False.
Ques: Write a python function push(stack, data), that accepts an empty list, which is the
stack and data, which is the dictionary .

It pushes the names of those countries onto the stack whose population is greater than
25 crores. This dictionary contains the names of some cities and their population in
crore. Also write the function pop(stack) that removes the top element of the stack on its
each call.

e.g.
Ques Write the following user-defined functions in Python :

1. the push_vowels(S,St): Here S is a string and St is a list representing a stack. The function
should push all the vowels of the string S onto the stack St.
For example, if string S is "Easy Concepts", then the function push_vowels() should push the
elements 'E','a','o','e' onto the stack.
2. pop_one(St) : The function should pop an element from the stack St, and return this element. If
the stack is empty, then the function should display the message ‘Stack Underflow’, and return
None.
3. display_all(St): The function should display all the elements of the stack St, without deleting
them. If the stack is empty, the function should display the message ‘Empty Stack’ .

You might also like