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

04 Data Structures (Stack)

The document discusses data structures like stacks and queues. It contains multiple choice questions about operations related to stacks like push, pop etc. It also discusses applications of stacks and different stack related terms.

Uploaded by

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

04 Data Structures (Stack)

The document discusses data structures like stacks and queues. It contains multiple choice questions about operations related to stacks like push, pop etc. It also discusses applications of stacks and different stack related terms.

Uploaded by

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

Data Structures – II: Stack and Queues

Multiple Choice Questions:


1. The process of inserting an element in stack is called:
(a) Create (b) Push
(c) Evaluation (d) Pop
2. The process of removing an element from stack is called:
(a) Create (b) Push
(c) Evaluation (d) Pop
3. In a stack, if a user tries to remove an element from an empty stack, the
situation is called:
(a) Underflow (b) Empty collection
(c) Overflow (d) Garbage collection
4. Pushing an element into a stack already having five elements and a
stack of size 5, then the stack becomes:
(a) User flow (b) Crash
(c) Underflow (d) Overflow
5. Entries in a stack are "ordered". What is the meaning of this statement?
(a) A collection of stacks can be sorted.
(b) Stack entries may be compared with the <' operation.
(c) The entries are stored in a linked list.
(d) There is a Sequential entry that is one by one.
6. Which of the following applications may use a stack?
(a) A parentheses balancing program
(b) Tracking of local variables at run time
(c) Compiler Syntax Analyzer.
(d) All of the above
7. Consider the usual algorithm for determining whether a sequence of
parentheses is balanced. The maximum number of parentheses that
appear on the stack AT ANY ONE TIME when the algorithm analyzes:
(()(())(()))is:
(a) 1 (b) 2
(c) 3 (d) 4 or more
8. Data structure stack is also known as _____ list.
(a) First In First Out (b) First In Last Out
(c) Last In First Out (d) All of these
9. In a stack, all insertions take place at _____ end(s).
(a) top (b) front
(c) rear (d) any
10. In a stack, deletions take place at _____ end.
(a) front (b) top
(c) rear (d) any
11. The terms Push and Pop are related to
(a) Queue (b) Stack
(c) Both (d) None

12. Choose correct output for the following sequence of operations (★


signifies top).
push(5), push(8), pop, push(2), push(5), pop, push(1)
(a) 85251 (b) 85521
(c) 2551 (d) 521
13. _____ form of access is used to add/remove nodes from a stack.
(a) LIFO (b) FIFO
(c) Both (a) and (b) (d) None of these
14. Stack follows the strategy of _____.
(a) LIFO (b) FIFO
(c) LRU (d) RANDOM
15. Which of the following is an application of stack?
(a) Finding factorial (b) Reversing of a string
(c) Infix to postfix (d) All of the above
16. When a stack, implemented as an array/list of fixed size, is full and no
new element can be accommodated, it is called an _____.
(a) OVERFLOW (b) UNDERFLOW
(c) EXTRAFLOW (d) NOFLOW
17. When a stack is empty and an element's deletion is tried from the
stack, it is called _____.
(a) OVERFLOW (b) UNDERFLOW
(c) EXTRAFLOW (d) NOFLOW
18. Pushing an element into stack already having five elements and stack
has fixed size of 5, then _____ occurs.
(a) Overflow (b) Crash
(c) Underflow (d) User flow
Type A questions
1. What are data structures? Name some common data structures.
Ans. A data structure is a logical way for organising data in memory that
considers not only the terms stored but also the relationship among the
items so as to give efficient and optimal performance. In other words, it is a
way of organising data such that it can be used efficiently.

2. Is data structure related to a data type? Explain.


Ans. A data type is a set of values along with well-defined operations
starting its input-output behaviour e.g. you cannot put decimal point in an
integer or two strings cannot be multiplied etc.
On the other hand, a data structure is a physical implementation that clearly
defines way of storing, assessing, manipulating data stored in a data
structure. The data stored in a data structure has a specific data type e.g. in a
stack, all insertion and deletion take place at only one place only.

3. What do you understand about linear and non-linear data structures?


Ans. Linear Data Structure:- A data structure is said to be linear if its
elements form a sequence or a linear list e.g. Array or linear lists, stacks and
queues.
Non-linear Data Structure:- A data structure is said to be a non-linear if
traversal of nodes is non- linear in nature e.g. graphs, trees.

4. Name some linear data structures, is linked list is a linear data


structure?
Ans. Name of some linear data structure are stack, queue, linked list.
Yes, linked list is a linear data structure.

5. What are data structures? Name some common data structures?


Ans. data-structure is a logical way for organizing data in memory that
considers not only the items stored but also the relationship among the
items so as to give efficient and optimal performance. In other words, it is a
way of organizing data such that it can be used efficiently. Some common
data structures are stack, queue, tree, linked list.

6. What is a stack?
Ans. Stack is a basic data structure where insertion and deletion of data
takes place at one end called the top of the stack i.e., it follows the Last in
First Out(LIFO) principle.

7. Write some application of Stack in real life.


Ans. Some of the applications of stack in real-life are:
(a) Pile of clothes in an almirah
(b) Multiple chairs in a vertical pile
(c) Bangles worn on wrist
(d) Pile of boxes of eatables in pantry or on a kitchen shelf

8. Define the following terms:


(i) Peek value
(ii) Overflow
(iii) Underflow
Ans.
(i) Peek value: This operation allows you to look at the top element of the
stack without actually removing it. It's useful for inspecting the element at
the top of the stack without altering the stack's contents.
(ii) Overflow: When you try to add an element to a stack that has already
reached its maximum capacity, the stack is said to be in an overflow state.
This typically happens when the stack's size is fixed, and attempting to push
more elements onto it results in an error condition.
(iii) Underflow: On the other hand, when you try to remove or peek at an
element from an empty stack, it's referred to as underflow. This indicates
that there are no elements present in the stack to perform the requested
operation, which can also result in an error condition.

9. What are some operation of stack in programming?


Ans. Some operation of stack in programming are:
(a) We can reverse a string using Stack.
(b) Undo/Redo option is programmed using the Stack.
(c) While browsing the web, the history of browsed pages is maintained as
stack.
(d) While writing any arithmetic expression in a program, we may use
parentheses to order the evaluation of operators. To handle matching of
parentheses, stack is used.
Board Pattern Questions
1. Julie has created a dictionary containing names and marks as key value
pair of 6 students. Write a program, with separate user defined functions
to perform the following operations.
(a) Push(R, stk): Push the keys (name of the student) of the dictionary into a
stack. Where the corresponding value (marks) is greater than 75.
(b) Pop(stk): pop and display the content of the stack. Also print “Stack
Empty” when stack is empty.
For example if the sample content of the dictionary is as follows
R = {“OM”:76, ”JAI”: 45, “BOB”: 89, “ANU”:90, “TOM”:82}
The output from the program should be
TOM ANU BOB OM
Ans:

2. A list contains the following record of a customer:


[customer_name, Phone_number, City]
Write the following user defined functions to perform given operations
on the stack named ‘status’:
(i) Push_element(Cust, status) – To push an object containing name and
phone number of customers who live in Goa to the stack.
(ii) Pop_element(status) – To pop the objects from the stack and display
them. Also display “Stack Empty” When there is no elements in the stack.
For example : if the list of customer details are:
[[“Gurdas”, “9999999999”, ”Goa”], [“Julee”,”8888888888”, ”Mumbai”],
[“Murugun”, ”7777777777”, ”Cochin”],[“Ashmit”, “1010101010”, “Goa”]]
The Stack should contain
[“Ashmit”, “1010101010”]
[“Gurdas”, “9999999999”]
The Output should be:
[“Ashmit”, “1010101010”]
[“Gurdas”,”9999999999”]
Stack Empty
Ans.

3. Write the functions in Python.


(a) Push(SItem) – where SItem is a dictionary containing the details of
stationary
items = {Sname: price}
The function should push the names of those items in the stack ‘stk’ who
have price greater than 75. Also display the count of the elements pushed
into the stack.
(b) Pop(stk) – this function pop all the items in the stack and print them
and print “Stack Empty” when stack is empty.
For example : If the dictionary contains the following data:
SItem = {“Pen”: 106, “Pencil”: 59, “Notebook”: 80, “Eraser”: 25}
The stack should contain
Notebook
Pen
4. A list has containing 5 integers. To create a program with separate user
defined functions to perform following tasks.
(i) Add_Odd( L1, stk) : Insert odd numbers.
(ii) Pop_All(stk) : Remove and display the content of the stack and print
“Stack Empty” when stack is empty.
For example,
Sample input:
L1 = [15, 26, 72, 53, 44]
Sample Output:
53 15

5. Jitendra was told by his teacher to pick certain elements from a


dictionary: “Garment” and write these methods.
a. Push(Garment, stk): TO push into a stack selectively the garment items
whose price is above 5000.
b. Pop(stk): To pop the element out of the stack. It should also print “Stack
Empty” when it is empty.
The structure of the government is given below:
Garment = {“1” : [“Shirt”,6500], “2” : [“Trouser”, 2000], “3” : [“Coat”,
15000]}

6. Nabojeet has created a dictionary: “Electronics”, storing data of


electronic products. Write two user-defined function.
(a) PushItems(Electronics, Stack): This function checks the details in the
dictionary and pushes only those items into a stack whose price is above
10000.
(b) PopItems(Stack): This function pops and print the items in the stack,
also print “Stack Empty” when stack is empty.
Dictionary Structure:
Electronics = {“D1” : [“LCD” , 15000], “D2” : [“CD Player”, 4000],-----------,}

7. Sunita has created a nested list : “KidsData”, storing data of kids. Write
these functions:
(a) Push_Kids(KidsData, Stack): The function checks the details in the list
and pushes Kid Id and Name to the Stack, only if the kids are of “Nursery”
(b) Pop_Kids(Stack): The function removes the elements and print them
from the stack. Also display “Stack Empty” when stack is empty.
List Structure:
KidsData = [[KidID, Kidname, Age, Grade]]

8. A list, NList contains following record as list elements:


[City, Country, distance from Delhi]
Each of these records are nested together to form a nested list. Write the
following user defined functions in Python to perform the specified
operations on the stack named travel.
(i) Push_element(NList): It takes the nested list as an argument and pushes
a list object containing name of the city and country, which are not in India
and distance is less than 3500 km from Delhi.
(ii) Pop_element(): It pops the objects from the stack and displays them.
Also, the function should display “Stack Empty” when there are no
elements in the stack. For example: If the nested list contains the following
data:
NList=[["New York", "U.S.A.", 11734],
["Naypyidaw", "Myanmar", 3219],
["Dubai", "UAE", 2194], ["London", "England", 6693],
["Gangtok", "India", 1580],
["Columbo", "Sri Lanka", 3405]]
The stack should contain:
['Naypyidaw', 'Myanmar'],
['Dubai', 'UAE'],
['Columbo', 'Sri Lanka']
The output should be:
['Columbo', 'Sri Lanka']
['Dubai', 'UAE']
['Naypyidaw', 'Myanmar']
Stack Empty
9. Abhiraj has a dictionary named Employee, Which contains the details
of employees. Structure of the Dictionary is as follows:
Employee = {EmpId : [EmpName, EmpDept, Salary ], ------------------}
Write these user defined functions:
(a) Push_Employee(Employee, Stack): This function takes the dictionary
Employee and pushes the EmpID and Salary into the stack whoes dept is
HR.
(b) Pop_Employee(Stack): This functions pops out the elements and print
them. Also, print Stack Empty when it is empty.

10. Given a stack named "movies", write a Python program with two user-
defined functions:
(a) Add_Movie(movie_details, movies): This function takes a nested list of
movie details and adds title and genre to the stack only if the rating is above
7.
(b) Remove_Movie(movies) : This function removes and displays the
elements from the stack. Also, print “Stack Empty” When Stack is empty.
The structur of movie_details is:
Movie_details = [[title, genre, rating], ----------------]

You might also like