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

NOTES DataStructure Stacks 2022 23

The document discusses stacks as a data structure. It defines a stack as a collection of data items that can be accessed from only one end, called the top. Items can only be inserted or removed from the top of the stack, following the last-in, first-out (LIFO) principle. The main operations on a stack are push, which inserts an item at the top, and pop, which removes an item from the top. The document also provides examples of stack operations using a Python list to implement a stack, and outputs showing items being pushed and popped from the stack.

Uploaded by

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

NOTES DataStructure Stacks 2022 23

The document discusses stacks as a data structure. It defines a stack as a collection of data items that can be accessed from only one end, called the top. Items can only be inserted or removed from the top of the stack, following the last-in, first-out (LIFO) principle. The main operations on a stack are push, which inserts an item at the top, and pop, which removes an item from the top. The document also provides examples of stack operations using a Python list to implement a stack, and outputs showing items being pushed and popped from the stack.

Uploaded by

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

Unit I: Computational Thinking and Programming – 2

 Data Structure: Data-structures: Lists as covered in Class XI, Stacks – Push, Pop
using a list.
WHAT IS DATA STRUCTURE

The way in which data is stored in memory of computer for efficient search and
retrieval. Different data structures are suited for different problems.

STACK Data Structure

Allow to insert and delete items from one endonly called TOP.
Stack works on LIFO principle. It means items added
last is removed first.
Real life Example: Stack of Plates,
Books,Bangles
Computer based examples: Undo Operation,
Function Call etc.

A stack is a collection of data items that can beaccessed at only one


end, called top.
Items can be inserted and deleted in a stack onlyat the top.
The last item inserted in a stack is the first one to be deleted.
Therefore, a stack is called a Last-In-First-Out(LIFO) data
structure.
2 mains operations on Stack is PUSH & POP
PUSH means inserting new item at top and POPmeans
deleting item from top.
THER STACK TERM

 Peek : getting the most recent value of stack i.evalue


at TOP

 OverFlow : a situation when we are Pushing itemin Stack


that is full.

 Underflow : a situation when we are Popping


item from empty stack

def push(L):
x= int(input("Enter data to be inserted to stack "))
L.append(x)

def pop(L):
if len(L)==0:
print("Stack is empty- cannot delete")
else:
x= L.pop()
print(x, " Has been deleted")

def display(L):
for i in range(len(L)-1,-1,-1):
print(L[i])

def menu():
print(" Choose 1 for Push, 2 for Pop 3 for Display elements 4
to Exit ")

mylist=[]
while True:
menu()
ch= int(input("Enter your choice 1/2/3/4 "))
if ch==1:
push(mylist)
print("Now Stack is : ")
display(mylist)
elif ch==2:
pop(mylist)
print("Now Stack is : ")
display(mylist)
elif ch==3:
display(mylist)
elif ch==4:
break
else:
print("Wrong input”)

Output:

Choose 1 for Push, 2 for Pop 3 for Display elements 4 to Exit


Enter your choice 1/2/3/4 1
Enter data to be inserted to stack 1
Now Stack is :
1
Choose 1 for Push, 2 for Pop 3 for Display elements 4 to Exit
Enter your choice 1/2/3/4 1
Enter data to be inserted to stack 2
Now Stack is :
2
1
Choose 1 for Push, 2 for Pop 3 for Display elements 4 to Exit
Enter your choice 1/2/3/4 1
Enter data to be inserted to stack 3
Now Stack is :
3
2
1
Choose 1 for Push, 2 for Pop 3 for Display elements 4 to Exit
Enter your choice 1/2/3/4 2
3 Has been deleted
Now List is :
2
1
Choose 1 for Push, 2 for Pop 3 for Display elements 4 to Exit
Enter your choice 1/2/3/4 3
2
1
Choose 1 for Push, 2 for Pop 3 for Display elements 4 to Exit
Enter your choice 1/2/3/4

Important Questions

Q1. i) Give any two characteristics of stacks.


Ans :
Characteristics of Stacks:
 It is a LIFO data structure
The insertion and deletion happens at oneend i.e. from the top of the stack

ii) How can implement Stacks in Python?


Ans: We can implement Stack using a List.
iii) What is a Data Structure? What is meant by LIFO?
iv) Can we delete an item from the middle of Stack?
Ans : No. Only at top
v) Give one example when Stacks are used.
Ans: Converting infix to postfix notations of Expression.

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 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)

def POP(S):
if S!=[]:
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

Output :
TOM ANU BOB OM

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

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

Output:
38 22 98 56 34 12
Q4) “Lists are dynamic in nature” What do you mean by this statement?
Ans. This means that list can be grown or shrink in size means elements can be
deleted or added in list.
Q5) What is the order of inserting elements in the following stack?
Ans. Order is: A, B, C, D
Q6) Insertion into stack is called ______ (push/pop)
Q7) Write a function Push() which takes number as argument and add in a
stack "MyValue"
Ans.
MyValue=[]
def Push(value):
MyValue.append(value)

Q8) Write a function Push that takes "name" as argument and add in a stack
named "MyStack"
Ans
Mynames=[]
def Push(Value):
Mynames.append(Value)
Push("Amit")

Q9. Write a function pop() which remove name from stack named "MyStack".
Ans.
def Pop(MyStack):
if len(MyStack) > 0:
MyStack.pop()
else:
print("Stack is empty.")

Q10. Write push(rollno) and pop() method in python:


push(rollno) --add roll number in Stack
pop() --- remove roll number from Stack.

Ans.
MyStack=[]
def Push(rollno):
MyStack.append(rollno)
def Pop(MyStack):
if len(MyStack) > 0:
MyStack.pop()
else:
print("Stack is empty.")

You might also like