Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

CLASS-XII

COMPUTER SCIENCE

DATA STRUCTURES

NOTES

INTRODUCTION TO DATA STRUCTURES

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

 A stack is a linear data structure.


 It is controlled by two operations: PUSH and POP .
 Both the operations take place from one end of the stack, usually called
top.
 Push operation adds an element to the top of the stack.
 Pop operation removes an element from the top of the stack.
 These two operations implement the LIFO method.

IMPLEMENTATION OF STACK AS A LIST

Stack can be easily implemented in Python using methods of list. The basic
operations that are performed on stack are:
 Creation Of Stack

: Stack can be created using list with the help of statement

stack=[]

this statement creates an empty stack.

 Push(Insertion) In The 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.

 Pop(Deletion) From The Stack

: 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

if( stack= = [])

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.

PROGRAM TO IMPLEMENT STACK AS A LIST

stack=[] # Creation of empty stack

ans="y"

while (ans=="y"):

print ("1. PUSH")

print ("2. POP ")

print ("3. Display")

choice=eval(input("Enter your choice: "))

if (choice==1):

a=input("Enter any number :")

stack.append(a) # pushing an element into the stack

elif (choice==2):

if (stack==[]): # checking for empty stack

print ("Stack Empty" )

else:

print ("Deleted element is : ",stack.pop()) # deleting an element from the


stack

elif (choice==3):

length=len(stack) # calculating length of the stack

for i in range(length-1,-1,-1): # traversing each element of the stack

print (stack[i])

else:
print("Invalid Input")

ans= input("Do you want to continue? y or n")

OUTPUT

1. PUSH
2. POP
3. Display

Enter your choice: 1

Enter any number :12

Do you want to continue? y or n y

1. PUSH
2. POP
3. Display

Enter your choice: 1

Enter any number :23

Do you want to continue? y or n y

1. PUSH
2. POP
3. Display

Enter your choice: 1

Enter any number :45

Do you want to continue? y or n y

1. PUSH
2. POP
3. Display

Enter your choice: 3

45

23
12

Do you want to continue? y or n y

1. PUSH
2. POP
3. Display

Enter your choice: 2

Deleted element is : 45

Do you want to continue? y or n y

1. PUSH
2. POP
3. Display

Enter your choice: 2

Deleted element is : 23

Do you want to continue? y or n y

1. PUSH
2. POP
3. Display

Enter your choice: 3

12

Do you want to continue? y or n n

APPLICATIONS OF STACK

A stack is an appropriate data structure on which information is stored and


then later retrieved in reverse order. The applications of stack are as
follows:

 When a program executes, stack is used to store the return address


at time of function call. After the execution of the function is over,
return address is popped from stack and control is returned back to
the calling function.
 Converting an infix expression to postfix operation and to evaluate
the postfix expression.
 Reversing an array, converting decimal number into binary number
etc.

INTRODUCTION TO QUEUES

A queue is a data structure that provides temporary storage of data in such a


way that the element stored first will be retrieved first. This method is also
called FIFO – First In First Out.

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

 A queue is a linear data structure.


 It is controlled by two operations: insertion and deletion.
 Insertion operation takes place from the rear end of the queue and
deletion operation takes place from the front end of the queue.
 Insertion operation adds an element to the queue.
 Deletion operation removes an element from the queue.
 There are two ends FRONT and REAR of a queue. FRONT points to the
beginning of the filled queue and takes care of deletion operation. REAR
points to the end of the queue and takes care of insertion operation.
 These two operations implement the FIFO method.

IMPLEMENTATION OF QUEUE AS A LIST

Queue can be easily implemented in Python using methods of list. The basic
operations that are performed on queue are:

 Creation Of queue

: Queue can be created using list with the help of statement:

queue=[]
this statement creates an empty queue.

 Insertion In The 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.

 Deletion From 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:

if( queue= = [])

print(“Queue is Empty”)

 Display(Traversal) Of Queue

: Display/traversal implies visiting each and every element of the queue


and displaying them. The display operation starts from the top position of
the queue. Thus to perform the display operation we first calculate the
length of the queue and using a looping statement display each element of
the queue as follows:

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.

Remember: To visit or access elements of queue, index values are used.


The first element of the queue is at index 0 i.e. queue[0]. the second will
be at index 1 i.e. queue[1] and so on.

PROGRAM TO IMPLEMENT QUEUE AS A LIST

queue=[] # Creation of empty queue

ans="y"

while (ans=="y"):

print( "1. Insertion")

print ("2. Deletion")

print ("3. Display")

choice=eval(input("Enter your choice: "))

if (choice==1):

a=input("Enter any number :")

queue.append(a) # inserting an element into the queue

elif (choice==2):

if (queue==[]): # checking for empty queue

print ("Queue Empty" )

else:

print ("Deleted element is : ",queue.pop(0)) # deleting an element from the


queue

elif (choice==3):
length=len(queue) # calculating length of the queue

for i in range(0, length,1): # traversing each element of the queue

print (queue[i])

else:

print("Invalid Input")

ans= input("Do you want to continue? y or n")

OUTPUT

1. Insertion
2. Deletion
3. Display

Enter your choice: 1

Enter any number :10

Do you want to continue? y or n y

1. Insertion
2. Deletion
3. Display

Enter your choice: 1

Enter any number :20

Do you want to continue? y or n y

1. Insertion
2. Deletion
3. Display

Enter your choice: 1

Enter any number :30

Do you want to continue? y or n y

1. Insertion
2. Deletion
3. Display

Enter your choice: 3

10

20

30

Do you want to continue? y or n y

1. Insertion
2. Deletion
3. Display

Enter your choice: 2

Deleted element is : 10

Do you want to continue? y or n y

1. Insertion
2. Deletion
3. Display

Enter your choice: 3

20

30

Do you want to continue? y or n n

APPLICATIONS OF QUEUE

A queue is an appropriate data structure on which information is stored and then


retrieved in FIFO order. The applications of queue are as follows:

 Queues find their use in CPU scheduling, printer spooling, message


queuing, computer networks etc.
 In time sharing system queues help in scheduling of jobs.

You might also like