KEMBAR78
Stacks Notes With Programs | PDF | Software Development | Computer Science
0% found this document useful (0 votes)
81 views2 pages

Stacks Notes With Programs

This document discusses stacks as a data structure and provides examples of push and pop operations on a stack. It defines a stack as a data structure that follows the LIFO (last-in, first-out) principle for inserting and deleting data. Example applications of stacks include reversal of sequences, backtracking puzzles, and undo mechanisms. It also defines stack overflow and underflow errors. Three questions are provided as examples of using push and pop operations on stacks to add and remove names and even numbers.

Uploaded by

Kartik Dubey
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)
81 views2 pages

Stacks Notes With Programs

This document discusses stacks as a data structure and provides examples of push and pop operations on a stack. It defines a stack as a data structure that follows the LIFO (last-in, first-out) principle for inserting and deleting data. Example applications of stacks include reversal of sequences, backtracking puzzles, and undo mechanisms. It also defines stack overflow and underflow errors. Three questions are provided as examples of using push and pop operations on stacks to add and remove names and even numbers.

Uploaded by

Kartik Dubey
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
You are on page 1/ 2

Narendra Srivastava, Doon International School

STACKS (DATA STRUCTURES)


A data structure is a logical way of organizing data in memory that considers not only the
items stored but also the relationship between 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.
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.
Following basic operations of stack are:
a. PUSH insertion of element in the stack
b. POP deletion of an element from the stack
c. Traversal displaying all the elements of the stack
Applications of stacks:
a. Reversal of sequence
b. Backtracking- It is used in a large number of puzzles like Sudoku etc. and in
optimization, problems such as the knapsack problem.
c. UNDO mechanism in next editors.
OVERFLOW in Stacks: A stack overflow is a type of buffer overflow error that occurs when
a computer program tries to use more memory space in the call stack
than has been allocated to that stack.
UNDERFLOW in Stacks: An error condition that occurs when an item is called for from the
stack, but the stack is empty.
Q1. Write a function Push(Names), and POP(Names), to add a new name and delete a
name from a Stack Names, considering them to act as push and pop operations of
the Stack data structure.
def Push(Names):
N=input(“Enter a name”)
Names.append(N)
def POP(Names):
if Names==[]:
print(“Underflow”)
else:
X=Names.pop()
print(X, “Deleted from Stack”)

Q2. Julie has created a dictionary containing names and marks as key value pairs of 6
students. Write a program, with separate user defined functions to perform the
following operations:
● Push the keys (name of the student) of the dictionary into a stack, where the
corresponding value (marks) is greater than 75.
● Pop and display the content of the stack. For example: If the sample content of the

Data File Handling Text File Part A, Theory


Narendra Srivastava, Doon International School

dictionary is as follows:
R={"OM":76, "JAI":45, "BOB":89, "ALI":65, "ANU":90, "TOM":82} The output
from the program should be: TOM ANU BOB OM
Ans:
R={"OM":76, "JAI":45, "BOB":89, "ALI":65, "ANU":90, "TOM":82}
def PUSH(S,N):
S.append(N) d
def POP(S):
if S!=[]: r
return S.pop()
else:
return None
ST=[]
for k in R:
if R[k]>=75:
PUSH(ST,k)
while True:
if ST!=[]:
print(POP(ST),end=" ")
else:
break

Q3. Alam has a list containing 10 integers. You need to help him create a program with
separate user defined functions to perform the following operations based on this list.
● Traverse the content of the list and push the even numbers into a stack.
● Pop and display the content of the stack. For Example: If the sample Content of the
list is as follows: N=[12, 13, 34, 56, 21, 79, 98, 22, 35, 38] Sample Output of the code
should be: 38 22 98 56 34 12
Ans:
N=[12, 13, 34, 56, 21, 79, 98, 22, 35, 38]
def PUSH(S,N):
S.append(N)
def POP(S):
if S!=[]:
return S.pop()
else: return None
ST=[]
for k in N:
if k%2==0:
PUSH(ST,k)
while True:
if ST!=[]:
print(POP(ST),end=" ")
else:
break

Data File Handling Text File Part A, Theory

You might also like