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

Data Structures

A data structure is a way of organizing and storing data in a computer so that it can be accessed efficiently. The document discusses two common data structures - stacks and queues. A stack follows the LIFO (last-in, first-out) principle, while a queue follows the FIFO (first-in, first-out) principle. Both can be implemented using lists in Python. The key operations for stacks are push to add an element and pop to remove an element. For queues, the key operations are enqueue to add an element and dequeue to remove an element. The document provides examples of implementing stacks and queues using lists and functions for common operations like is_empty and traversal. It also discusses differences between stacks and

Uploaded by

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

Data Structures

A data structure is a way of organizing and storing data in a computer so that it can be accessed efficiently. The document discusses two common data structures - stacks and queues. A stack follows the LIFO (last-in, first-out) principle, while a queue follows the FIFO (first-in, first-out) principle. Both can be implemented using lists in Python. The key operations for stacks are push to add an element and pop to remove an element. For queues, the key operations are enqueue to add an element and dequeue to remove an element. The document provides examples of implementing stacks and queues using lists and functions for common operations like is_empty and traversal. It also discusses differences between stacks and

Uploaded by

ap39fp3t
Copyright
© © All Rights Reserved
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 7

Data structure notes class

12
Chapter 6: Data Structures in Python For Video Lecture Click here:

What is Data Structure:

Data structure is a way of storing, organizing and fetching data in computer.

What is Stack:

A stack is a linear data structure in python in which addition and deletion of elements can be done at one
end only that is called TOP. A stack is known as LIFO (Last – In, First – Out) data structure in python.
Insertion in stack is also known as a PUSH operation. Deletion from stack is also known as POP operation
in stack.

Working with Stack using List / Implementation of Stack using List:

Working with stack is similar to working with list. Basic operations that we should know are :
1. How to create an empty stack?
2. How to add elements to a stack?
3. How to delete / remove elements from the stack
4. How to traverse or displaying elements of stack?
5. How to check for empty stack?

Create an empty stack:


st = [ ] or st = list( )

Adding an element to a Stack : This is known as PUSH


st.append(5)

Deleting / removing elements from the stack : This is known as POP


st.pop( )

Traverse or displaying elements of stack:

L = len(st)
for i in range(L-1, -1, -1) : #to display in reverse order
print(st[i])

Check for empty stack (is_empty):


if (st == [ ]):
print("stack is empty")

Overflow and Underflow:

Overflow: It refers to a situation, when one tries to push an element in stack/queue, that is full. In python
list OVERFLOW condition does not arise until all memory is exhausted because list can grow as per data.

Underflow: It refers to situation when one tries to pop/delete an item from an empty stack or queue.
Example:
List=[]
List.pop()

What is Queue:

Queue is a data structures that is based on First In First Out (FIFO) strategy. Elements are added at one end
called Rear and removed from other end called Front. In a queue, one end is always used to insert data
(enqueue) and the other is used to delete data (dequeue), because queue is open at both its ends.

Operations on Queue:

Enqueue: In this a new element is added at the end of list called Rear.

Dequeue: In this an element is to be deleted from one end called Front.

Is empty: It is used to check whether the queue has any element or not. It is used to avoid underflow
condition while performing Dequeue operation.

Is full: It is used to check whether any more elements can be added to the queue or not, to avoid the
overflow condition while performing Enqueue operation.
Peek: It is used to view element at the front and back of the queue, without removing it from the queue.
We can do this by: list[0] or list[-1]

Working with Queue using List / Implementation of Queue using List:

Basic operations that we should know are :


1. How to create an empty Queue?
2. How to add elements to a Queue?
3. How to delete / remove elements from the Queue?
4. How to traverse or displaying elements of Queue?
5. How to check for empty Queue?

Create an empty Queue:


st = [ ] or st = list( )

Adding an element to a Queue : This is known as Enqueue


st.append(5)

Deleting / removing elements from the Queue : This is known as Dequeue


st.pop(0)

Traverse or displaying elements of Queue:

L = len(st)
for i in range(0, L,)
: print(st[i])

Check for empty Queue (is_empty):


if (st == [ ]):
print("Queue is empty")

Difference between Stack and Queue:

Stack Queue
Stack is a data structures that is based on Last In Queue is a data structures that is based on First In
First Out (LIFO) strategy. First Out (FIFO) strategy.
Insertion and deletion of elements can be done Elements are added at one end called Rear
at one end only that id called TOP. and removed from other end called Front.

Insertion in stack is also known as a PUSH Insertion in Queue is also known as a Enqueue
operation. Deletion from stack is also known as operation. Deletion from Queue is also known
POP operation in stack. as Dequeue operation in stack.
Important Questions for Exams For Video Lecture Click Here:

Q1. Write a function push(),pop() and display() to add a new student name, remove a student name and
to display student names in stack format, according to Data Structure in Python.
st=[ ]
def push():
sn=input("Enter name of
student:") st.append(sn)
print(st)

def pop():
if st==[]:
print("Stack is
empty") else:
print("Deleted student name :",st.pop())

def display():
if st==[]:
print("Stack is
empty") else:
for i in st[::-1]:
print(i)

Q2. Write a function Push() which to add in a stack named "MyStack". After calling push() three times, a
message should be displayed "Stack is Full"

st=[ ]
StackSize=3
def
push():
sn=input("Enter name of
student:") if len(st)<StackSize:
st.append(sn)
else:
print("Stack is
full!") print(st)

Q3: 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.

R={"OM":76, "JAI":45, "BOB":89,"ALI":65, "ANU":90, "TOM":82}


st=[]
def push():
for i in R:
if R[i]>75:
st.append(i)
print(st)

def pop():
while True:
if st!=[]:
print(st.pop(),end="
") else:
break

Q4: 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.

N=[12, 13, 34, 56, 21, 79, 98, 22, 35, 38]
st=[]
def push():
for i in N:
if i%2==0:
st.append(i)

def pop():
while True:
if st!=[]:
print(st.pop(),end="
") else:
break

Q 5: Write a function push(l) to perform push opration in stack. Where a list is given:
L=[10,12,15,16,17,18,20,21,25]
Push only those element who is divisible by 5.

def push(l):
L2.append(l)

L2=[]
L1=[10,12,15,16,17,18,20,21,25]
for i in L1:
if i%5==0:
push(i)
print("Original List:\n",L1)
print("List after using push:\
n",L2)

Q 6: Write a function Addvowel(l) to perform push opration in stack. Where a list is given:

L=[‘Ansh’,’Vipin’,’Ishan’,’Devesh’,’Om’,’Upashna’]
Push only those element who is started from vowels

def
Addvowel(l)
:
L2.append(l)

L2=[]
L1=['Ansh','Vipin','Ishan','Devesh','Om','Upashna']
for i in L1:
if i[0] in
'aeiouAEIOU':
Addvowel(i)

print("Original List:\n",L1)
print("List after using push:\
n",L2)

You might also like