Professional Documents
Culture Documents
Data Structures Notes
Data Structures Notes
COMPUTER SCIENCE
DATA STRUCTURES
NOTES
The term data structure is defined as a way of organizing and storing data such
that we can access and modify it efficiently. We can categorize the data
structures as primitive and non-primitive.
In this chapter we will learn about Stacks and Queues and their implementation
in Python using lists.
INTRODUCTION TO STACKS
A stack is a data structure that provides temporary storage of data in such a way
that the element stored last will be retrieved first. This method is also
called LIFO – Last In First Out . In real life we can think of stack as a stack of
copies, stack of plates, stack of rotis etc.
The first plate put in the stack is the last one to be removed. Similarly, last plate
to put in the stack is the first one to be removed.
OPERATIONS ON STACK
Stack can be easily implemented in Python using methods of list. The basic
operations that are performed on stack are:
Creation Of Stack
stack=[]
: The push operation takes place with the help of append() function. The
append() functions adds the new element at the end i.e. at the top of the
list.
: The pop operation takes place with the help of pop() function. The pop()
function removes the last(top most) element from the stack implemented
as a list. Before removing an element from the stack it is necessary to
check whether the stack is empty or not. To check whether the stack is
empty or not we can use the statement
print(“Stack is Empty”)
Display(Traversal) Of Stack
Display/traversal implies visiting each and every element of the stack and
displaying them. The display operation starts from the top position of the
stack. Thus to perform the display operation we first calculate the length
of the stack and using a looping statement display each element of the
stack as follows:
length=len(stack)
for i in range(length-1,-1,-1):
print (stack[i])
Here len() function calculates the length of the stack which is stored in
the variable length. After this the for loop is used to visit each element
from length-1 to 0 and display it using print statement.
Remember: To visit or access elements of stack, index values are used. The
first element of the stack is at index 0 i.e. stack[0]. the second will be at index 1
i.e. stack[1] and so on.
ans="y"
while (ans=="y"):
if (choice==1):
elif (choice==2):
else:
elif (choice==3):
print (stack[i])
else:
print("Invalid Input")
OUTPUT
1. PUSH
2. POP
3. Display
1. PUSH
2. POP
3. Display
1. PUSH
2. POP
3. Display
1. PUSH
2. POP
3. Display
45
23
12
1. PUSH
2. POP
3. Display
Deleted element is : 45
1. PUSH
2. POP
3. Display
Deleted element is : 23
1. PUSH
2. POP
3. Display
12
APPLICATIONS OF STACK
INTRODUCTION TO QUEUES
In real life we can think of queue as a queue of vehicles waiting at the petrol
pump, people waiting at the bus stop for the bus etc.
The first person to enter the queue is the first one to leave the queue. Similarly,
last person to join the queue is the last person to leave the queue.
OPERATIONS ON QUEUE
Queue can be easily implemented in Python using methods of list. The basic
operations that are performed on queue are:
Creation Of queue
queue=[]
this statement creates an empty queue.
: The insertion operation takes place with the help of append() function.
The append() functions adds the new element at the end of the list.
Initially the queue is empty. When the first element, 10 is added to the
queue it is both the first and last element. Thus, both front and rear point
to this element. When the next element is added is added to the queue
only the rear changes and front remains the same. This process continues
for all further additions in the queue.
: The deletion operation takes place with the help of pop() function. The
pop() function removes the last element. To delete the first element from
the queue implemented as a list we use pop(0). This will delete the first
element from the list and shifts all elements one position towards the left
to fill the gap created due to deletion of element from the queue. Before
removing an element from the queue it is necessary to check whether the
queue is empty or not. To check whether the queue is empty or not we
can use the statement:
print(“Queue is Empty”)
Display(Traversal) Of Queue
length=len(queue)
for i in range(0,length,1):
print (queue[i])
Here len() function calculates the length of the queue which is stored in
the variable length. After this the for loop is used to visit each element
from 0 to length-1 and display it using print statement.
ans="y"
while (ans=="y"):
if (choice==1):
elif (choice==2):
else:
elif (choice==3):
length=len(queue) # calculating length of the queue
print (queue[i])
else:
print("Invalid Input")
OUTPUT
1. Insertion
2. Deletion
3. Display
1. Insertion
2. Deletion
3. Display
1. Insertion
2. Deletion
3. Display
1. Insertion
2. Deletion
3. Display
10
20
30
1. Insertion
2. Deletion
3. Display
Deleted element is : 10
1. Insertion
2. Deletion
3. Display
20
30
APPLICATIONS OF QUEUE