20CS2013 L-Data Structures and Algorithms Lab Ex. No. 1: Stack Implementation Using Array
20CS2013 L-Data Structures and Algorithms Lab Ex. No. 1: Stack Implementation Using Array
Aim:
To implement a menu driven code for various operations on stack - push, pop and peek
using array.
Algorithm:
Step 1: Start the program.
Step 2: Create a class stack.
Step 3: Create __ init__ function and parameter is s.
Step 4: Declare s.stack = list(), s.maxSize = 8 and s.top = 0.
Step 5: Create push function and if top >= maxsize then return “ Stack Full!”.
Step 6: Otherwise compute s.stack.append(data), s.top += 1 and return True.
Step 7: Create a pop function and if top <= 0 then return "Stack Empty!".
Step 8: Otherwise compute item = s.stack.pop(), s.top -= 1 and return item.
Step 9: Create peek function and if stack then return stack[-1].
Step 10: Create while loop and it’s true then display '**** Stack ADT Implementation
****', '1) push', '2) pop’, ‘3) peek’ and '4) quit'.
Step 11: Display 'Enter the option : ' and Read the integer input i from user.
Step 12: If i is equal to 1 then read the input data from the user and display Status of Push
and Pushed Element.
Step 13: Else if i is equal to 2 then display Popped Element.
Step 14: Else if i is equal to 3 then display Peeked Element.
Step 15: Else if i is equal to 4 then compute break.
Step 16: Stop the program.
Program:
class stack:
def __init__(s):
s.stack = list()
s.maxsize = 8
s.top = 0
def push(s, data):
if s.top >= s.maxsize:
return "Stack Full!"
s.stack.append(data)
1
Ex.no: 1 | STACK IMPLEMENTATION USING ARRAY
20CS2013 L-Data Structures and Algorithms Lab URK20CS1116
s.top += 1
return True
def pop(s):
if s.top <= 0:
return "Stack Empty!"
item = s.stack.pop()
s.top -= 1
return item
def peek(s):
if s.stack:
return s.stack[-1]
s = stack()
while True:
print('**** Stack ADT Implementation ****')
print('1) push')
print('2) pop')
print('3) peek')
print('4) quit')
i = int(input('Enter the option : '))
if i == 1:
data = input("Enter the data : ")
print("Status of Push : " + str(s.push(data)))
print("Pushed Element : " + str(data))
elif i == 2:
print("Popped Element : " + str(s.pop()))
elif i == 3:
print("Peeked Element : " + str(s.peek()))
elif i == 4:
break
Output:
2
Ex.no: 1 | STACK IMPLEMENTATION USING ARRAY
20CS2013 L-Data Structures and Algorithms Lab URK20CS1116
3
Ex.no: 1 | STACK IMPLEMENTATION USING ARRAY
20CS2013 L-Data Structures and Algorithms Lab URK20CS1116
Result:
The above program has been executed for sample input values and output is
verified.
4
Ex.no: 1 | STACK IMPLEMENTATION USING ARRAY
20CS2013 L-Data Structures and Algorithms Lab URK20CS1116
A)
Aim:
To implement the Infix to Postfix Expression using Stack.
Algorithm:
Step 1: Start the program.
Step 2: Declare an infix string and ask the user to enter the infix Expression.
Step 3: Declare operators = {'+', '-', '*', '/', '^', '(', ')'} and priority = {'-': 1, '+': 2, '/': 3, '*': 4,
'^': 5}.
Step 4: Create a postfix user defined function.
Step 5: Declare stack = [] and output = ‘’.
Step 6: Create a for loop i in expression.
Step 7: If i is not in operators then compute output += i.
Step 8: Else if i is equal to ‘(’ then push the element to stack.
Step 9: Else if i is equal to ‘)’ then check while stack and stack[-1] is not equal to ‘(’ and
compute output += stack.pop() and stack.pop().
Step 10: Else check while stack and stack[-1] != '(' and priority[i] < priority[stack[-1]] and
compute output += stack.pop() and stack.append(i).
Step 11: Return output and display output.
Step 12: Stop the program.
Program:
def postfix(expression):
stack = []
output = ''
for i in expression:
if i not in operators:
output += i
elif i == '(':
stack.append(i)
elif i == ')':
while stack and stack[-1] != '(':
output += stack.pop()
5
Ex.no: 2 | Applications of Stack
20CS2013 L-Data Structures and Algorithms Lab URK20CS1116
stack.pop()
else:
while stack and stack[-1] != '(' and priority[i] < priority[stack[-1]]:
output += stack.pop()
stack.append(i)
while stack:
output += stack.pop()
return output
infix = input('Enter the infix Expression : ')
operators = {'+', '-', '*', '/', '^', '(', ')'}
priority = {'-': 1, '+': 2, '/': 3, '*': 4, '^': 5}
print('Postfix Expression : '+str(postfix(infix)))
Output:
Result:
The above program has been executed for sample input values and output is verified.
B)
Aim:
To implement the Evaluation of Postfix Expression using Stack.
Algorithm:
Step 1: Start the program.
Step 2: Declare Postfix_Expression and ask the user to enter the Postfix Expression.
Step 3: Declare l is equal to list(Postfix_Expression) and l2 = [].
Step 4: Create a for loop i in range (length of l).
Step 5: If l[i] is alphabet then ask the user to enter the number l[i].
Step 6: Else push l[i] to l2.
Step 7: Convert l2 to string and it is equal to s.
Step 8: Create evaluation user defined function.
Step 9: Split s between ‘ ’ and it is equal to s and n is equal to len(s).
6
Ex.no: 2 | Applications of Stack
20CS2013 L-Data Structures and Algorithms Lab URK20CS1116
Program:
def evaluation(s):
s = [i for i in s.split(' ')]
n = len(s)
stack = []
for i in range(n):
if s[i].isdigit():
stack.append(int(s[i]))
elif s[i] == "+":
b = stack.pop()
a = stack.pop()
stack.append(int(a)+int(b))
elif s[i] == "-":
b = stack.pop()
a = stack.pop()
stack.append(int(a)-int(b))
elif s[i] == "*":
b = stack.pop()
a = stack.pop()
stack.append((int(a)*int(b)))
elif s[i] == "/":
b = stack.pop()
a = stack.pop()
stack.append((int(a)/int(b)))
return stack.pop()
7
Ex.no: 2 | Applications of Stack
20CS2013 L-Data Structures and Algorithms Lab URK20CS1116
Output:
Result:
The above program has been executed for sample input values and output is verified.
C)
Aim:
To implement the Reversal of a string using Stack using Stack.
Algorithm:
Step 1: Start the program.
Step 2: Declare a string and ask the user to enter the sentence.
Step 3: Declare l is equal to list(string), stack is equal to [] and reversed_string = ''.
Step 4: Create a for loop i in range(len(l)) and compute push l[i] to stack.
Step 5: Create a for loop i in range(len(l)) and compute the pop element from the stack &
add pop element to reversed_string.
Step 6: Display reversed_string.
Step 7: Stop the program.
8
Ex.no: 2 | Applications of Stack
20CS2013 L-Data Structures and Algorithms Lab URK20CS1116
Program:
string = input('Enter the sentence : ')
l = list(string)
stack = []
reversed_string = ''
for i in range(len(l)):
stack.append(l[i])
for i in range(len(l)):
a = stack.pop()
reversed_string += a
print('The reversed string is '+reversed_string)
Output:
Result:
The above program has been executed for sample input values and output is verified.
9
Ex.no: 2 | Applications of Stack
20CS2013 L-Data Structures and Algorithms Lab URK20CS1116
Aim:
To Implementation of queue data structure using arrays.
Algorithm:
Step 1: Start the program.
Step 2: Declare front = 0, rear = 0, capacity = 5 and queue = [].
Step 3: Create while loop and its true then display '***** Implementation of queue *****' and
1.insert\n2.delete\n3.display\n4.exit'.
Step 4: Read input integer choice from user.
Step 5: If choice is equal to 1 then check if rear is equal to capacity then display 'queue is full !'.
Step 6: Else read input integer new_element and insert new_element to queue.
Step 7: Compute rear += 1and display 'New element is successfully inserted'.
Step 8: Else if choice is equal to 2 then check if front is equal to rear then display 'queue is
empty!'.
Step 9: Else delete front element and display deleted element.
Step 10: Else if choice is equal to 3 then check if front is equal to rear then display 'queue is
empty!'.
Step 11: Else display elements of the queue.
Step 12: Else if choice is equal to 4 then break the while loop.
Step 13: Else display 'Invalid choice !'.
Step 14: Stop the program.
Program:
front = 0
rear = 0
capacity = 5
queue = []
while True:
print('***** Implementation of queue *****')
print('1.insert\n2.delete\n3.display\n4.exit')
choice = int(input('Enter your choice : '))
if choice == 1:
10
Ex.no: 3 | Implementation of queue data structure using array
20CS2013 L-Data Structures and Algorithms Lab URK20CS1116
if rear == capacity:
print('queue is full !')
else:
new_element = int(input('Enter the new element : '))
queue.append(new_element)
rear += 1
print('New element is successfully inserted')
elif choice == 2:
if front == rear:
print('queue is empty !')
else:
deleted_element = queue[front]
for i in range(rear-1):
queue[i] = queue[i + 1]
rear -= 1;
print('deleted element is ' + str(deleted_element))
elif choice == 3:
if front == rear:
print('queue is empty !')
else:
print('Elements of queue : ')
for i in range(rear):
print(queue[i])
elif choice == 4:
break
else:
print('Invalid choice !')
11
Ex.no: 3 | Implementation of queue data structure using array
20CS2013 L-Data Structures and Algorithms Lab URK20CS1116
Output:
Result:
The above program has been executed for sample input values and output is
verified.
12
Ex.no: 3 | Implementation of queue data structure using array
20CS2013 L-Data Structures and Algorithms Lab URK20CS1116
1)
Aim:
To create a singly linked list and implement the following functions in the list created:
● Insert data at the front of the list.
● Insert data at the rear of the list.
● Insert data after a given data in the list.
● Delete data from the list.
● Search the data in the list.
● Display the contents of the list.
Algorithm:
Step 1: Start the program.
Step 2: Create node class and init user defined function.
Step 3: Compute self.dataval = dataval and self.nextval = None.
Step 4: Create slinkedlist & init user defined function and Compute self.headval = None.
Step 5: Create listprint user defined function and Compute printval = self.headval
Step 6: Create a while loop and display all datas in a Singly linked List.
Step 7: Create search user defined function and use if statement compare given data to datas in
Singly linked List.
Step 8: Create len user defined function and use a while loop to count the number of datas in
Singly linked List.
Step 9: Create list object for slinkedlist.
Step 10: Create while loop and Display “'1) Insert a data at the front of the list, 2) Insert a data at
the rear of the list, 3) Insert a data after a given data in the list, 4) Delete a data from the list, 5)
Search a data in the list, 6) Display the contents of the list, 7) Exit”
Step 11: Read input integer n from user.
Step 12: If n is equal to 1 then read input data from the user and insert data to front of the Singly
linked List.
Step 13: Else if n is equal to 2 then read input data from user and insert data to rear of Singly
linked List.
Step 14: Elseif n is equal to 3 then read input j & data from user and insert data to after the
position j of Singly linked List.
13
Ex.no: 4 | Singly linked List
20CS2013 L-Data Structures and Algorithms Lab URK20CS1116
Step 15: Else if n is equal to 4 delete front data of Singly linked List and display that deleted
data.
Step 16: Else if n is equal to 5 then read input data from user and search data from rear of Singly
linked List and display its available or not.
Step 17: Else if n is equal to 6 then display contents of Singly linked List.
Step 18: Else if n is equal to 7 then use break to out from while loop.
Step 19: Stop the program.
Program:
class node:
def __init__(self, dataval = None):
self.dataval = dataval
self.nextval = None
class slinkedlist:
def __init__(self):
self.headval = None
def listprint(self):
printval = self.headval
while printval is not None:
print(printval.dataval)
printval = printval.nextval
def search(self,i):
printval = self.headval
while printval is not None:
if i == printval.dataval:
return str(i)+" is available on singly linked list"
printval = printval.nextval
else:
return str(i) + " is not available on singly linked list"
def len(self):
printval = self.headval
i = -1
while printval is not None:
i += 1
printval = printval.nextval
return i
def deleted_element(self,l):
14
Ex.no: 4 | Singly linked List
20CS2013 L-Data Structures and Algorithms Lab URK20CS1116
return l.dataval
list = slinkedlist()
while True:
print('''1) Insert a data at the front of the list
2) Insert a data at the rear of the list
3) Insert a data after a given data in the list
4) Delete a data from the list
5) Search a data in the list
6) Display the contents of the list
7) Exit''')
n = int(input('Enter your option : '))
if n == 1:
if list.headval is not None:
k = list.headval
list.headval = node(input('Enter the data : '))
list.headval.nextval = k
else:
list.headval = node(input('Enter the data : '))
m = list.headval
elif n== 2:
try:
if m != None:
m.nextval = node(input('Enter the data : '))
m = m.nextval
except NameError:
list.headval = node(input('Enter the data : '))
m = list.headval
elif n == 3:
l = list.headval
i=0
j = int(input('Enter the position which after insert the data : '))
a = list.len()
while l is not None:
if i == j:
if j < a:
k = l.nextval
l.nextval = node(input('Enter the data : '))
l.nextval.nextval = k
15
Ex.no: 4 | Singly linked List
20CS2013 L-Data Structures and Algorithms Lab URK20CS1116
else:
print("Invalid Position !")
l = l.nextval
i += 1
elif n == 4:
l = list.headval
list.headval = l.nextval
print("Deleted data is "+str(list.deleted_element(l)))
elif n == 5:
i = input("Enter the data to search : ")
print (list.search(i))
elif n == 6:
print('The contents of the list')
list.listprint()
elif n == 7:
break
else:
print("Invalid Option !")
16
Ex.no: 4 | Singly linked List
20CS2013 L-Data Structures and Algorithms Lab URK20CS1116
Output:
17
Ex.no: 4 | Singly linked List
20CS2013 L-Data Structures and Algorithms Lab URK20CS1116
18
Ex.no: 4 | Singly linked List
20CS2013 L-Data Structures and Algorithms Lab URK20CS1116
Result:
The above program has been executed for sample input values and output is
verified.
2)
19
Ex.no: 4 | Singly linked List
20CS2013 L-Data Structures and Algorithms Lab URK20CS1116
1)
Aim:
To implement selection sort algorithms to sort the given list.
Algorithm:
Step 1: Start the program.
Step 2: Declare list A and for loop and split function to read input from user.
Step 3: Create for loop i = 0 to length of A -1 and Declare minimum = i.
Step 4: Create for loop j = i+1 to length of A and if A[i] is less than A[minimum] then compute
minimum = i.
Step 5: Swap A[i] and A[minimum].
Step 6: Display Selection Sorted list A.
Step 7: Stop the program.
Program:
A = [int(i) for i in input('Enter the list A : ').split()]
for i in range(len(A)-1):
minimum = i
for j in range(i+1, len(A)):
if A[j] < A[minimum]:
minimum = j
A[minimum], A[i] = A[i], A[minimum]
print('Selection Sorted list A is '+''.join(str(A)))
Output:
20
Ex.no: 5 | Implementation of selection sort and insertion sort algorithms
20CS2013 L-Data Structures and Algorithms Lab URK20CS1116
Result:
The above program has been executed for sample input values and output is
verified.
2)
Aim:
To implement insertion sort algorithms to sort the given list.
Algorithm:
Step 1: Start the program.
Step 2: Declare list A and for loop and split function to read input from user.
Step 3: Declare n is equal to length of A.
Step 4: Create for loop i = 1 to n and Declare v = A[i] & j = i - 1.
Step 5: Create a while loop to check j is greater than or equal to 0 and A[j] is greater than v.
Step 6: Compute A[j+1] = A[j] and decrement j.
Step 7: Compute A[j+1] = v.
Step 8: Display Selection Sorted list A.
Step 9: Stop the program.
Program:
A = [int(i) for i in input('Enter the list A : ').split()]
n = len(A)
for i in range(1, n):
v = A[i]
j=i-1
while j >= 0 and A[j] > v:
A[j+1] = A[j]
21
Ex.no: 5 | Implementation of selection sort and insertion sort algorithms
20CS2013 L-Data Structures and Algorithms Lab URK20CS1116
j -= 1
A[j+1] = v
print('Insertion Sorted list A is '+''.join(str(A)))
Output:
Result:
The above program has been executed for sample input values and output is
verified.
22
Ex.no: 5 | Implementation of selection sort and insertion sort algorithms
20CS2013 L-Data Structures and Algorithms Lab URK20CS1116
1)
Aim:
To implement a Doubly Linked List.
Algorithm:
Step 1: Start the program.
Step 2: Create class node for create node.
Step 3: Create class DoublyLinkedList and create user defined function to initialize head is equal
to none.
Step 4: Create insert_front user defined function for insert data in front of Doubly Linked List.
Step 5: Create insert_After user defined function for insert data after given data the Doubly Linked
List.
Step 6: Create insert_Before user defined function for insert data before given data the Doubly
Linked List.
Step 7: Create insert_rear user defined function for insert data in rear of Doubly Linked List.
Step 8: Create Traversal_forward user defined function for display datas of Doubly Linked List in
forward direction.
Step 9: Create Traversal_reverse user defined function for display datas of Doubly Linked List in
reverse direction.
Step 10: Create a search user defined function for searching the given data.
Step 11: Create delete user defined function for delete the given data.
Step 12: Create object for Doubly Linked List.
Step 13: Create menu driven using a while loop and give 9 options.
Step 14: If user entered 1 then ask data and insert data to front of Doubly Linked List.
Step 15: Else if user entered 2 then ask data and insert data to rear of Doubly Linked List.
Step 16: Else if user entered 3 then ask data and insert data to after given data of Doubly Linked
List.
Step 17: Else if user entered 4 then ask data and insert data to before given data of Doubly Linked
List.
Step 18: Else if user entered 5 then ask data and delete that data from Doubly Linked List.
Step 19: Else if user entered 6 then ask data and search that data from Doubly Linked List.
23
Ex.no: 6 | Implementation of Doubly Linked List
20CS2013 L-Data Structures and Algorithms Lab URK20CS1116
Step 20: Else if user entered 7 then display datas of Doubly Linked List in forward direction.
Step 21: Else if user entered 8 then display datas of Doubly Linked List in reverse direction.
Step 22: Else if user entered 9 then break the while loop.
Step 23: Else user enters any other number then displays "Invalid Option !".
Step 24: Stop the program.
Program:
class Node:
def __init__(self, data):
self.data = data
self.next = None
self.prev = None
class DoublyLinkedList:
def __init__(self):
self.head = None
24
Ex.no: 6 | Implementation of Doubly Linked List
20CS2013 L-Data Structures and Algorithms Lab URK20CS1116
25
Ex.no: 6 | Implementation of Doubly Linked List
20CS2013 L-Data Structures and Algorithms Lab URK20CS1116
while last:
s += str(last.data) + " "
last = last.prev
print(s)
else:
print("No data on doubly linked list")
def search(self,node,i):
while node:
given_node = node
if (given_node.data == i):
print(str(i)+" is available on doubly linked list")
return
node = node.next
print(str(i) + " is not available on doubly linked list")
def delete(self,dele):
if self.head == dele:
self.head = dele.next
if dele.next is not None:
dele.next.prev = dele.prev
if dele.prev is not None:
dele.prev.next = dele.next
del dele
llist = DoublyLinkedList()
while True:
print('''1) Insert a data at the front of the list
2) Insert a data at the rear of the list
3) Insert a data after a given data in the list
4) Insert a data before a given data in the list
5) Delete a data from the list
6) Search a data in the list
7) Display the contents of the list in forward direction
8) Display the contents of the list in reverse direction
9) Exit''')
26
Ex.no: 6 | Implementation of Doubly Linked List
20CS2013 L-Data Structures and Algorithms Lab URK20CS1116
elif n == 4:
i = int(input('Enter data which before you want to insert new data : '))
node = llist.head
while node:
given_node = node
if (given_node.data == i):
break
node = node.next
if node is not None:
llist.insert_Before(given_node, int(input('Enter data to insert : ')))
else:
print(str(i)+" is not available on doubly linked list")
elif n == 5:
i = int(input('Enter data which you want to delete : '))
node = llist.head
while node:
given_node = node
if (given_node.data == i):
break
27
Ex.no: 6 | Implementation of Doubly Linked List
20CS2013 L-Data Structures and Algorithms Lab URK20CS1116
node = node.next
if node is not None:
llist.delete(given_node)
print(str(i) + " is deleted")
else:
print(str(i)+" is not available on doubly linked list")
elif n == 6:
llist.search(llist.head,int(input('Enter data to search : ')))
elif n == 7:
llist.Traversal_forward(llist.head)
elif n == 8:
llist.Traversal_reverse(llist.head)
elif n == 9:
break
else:
print("Invalid Option !")
Output:
28
Ex.no: 6 | Implementation of Doubly Linked List
20CS2013 L-Data Structures and Algorithms Lab URK20CS1116
29
Ex.no: 6 | Implementation of Doubly Linked List
20CS2013 L-Data Structures and Algorithms Lab URK20CS1116
30
Ex.no: 6 | Implementation of Doubly Linked List
20CS2013 L-Data Structures and Algorithms Lab URK20CS1116
31
Ex.no: 6 | Implementation of Doubly Linked List
20CS2013 L-Data Structures and Algorithms Lab URK20CS1116
Result:
The above program has been executed for sample input values and output is
verified.
32
Ex.no: 6 | Implementation of Doubly Linked List
20CS2013 L-Data Structures and Algorithms Lab URK20CS1116
1)
Aim:
To Implementation of stack data structure using linked list.
Algorithm:
Step 1: Start the program.
Step 2: Create a class node.
Step 3: Create __ init__ function to compute self.dataval is equal to dataval & self.nextaval is equal
to None.
Step 4: Create a class stack.
Step 5: Create __ init__ function to compute self.head is equal to None.
Step 6: Create push function to compute insert new data in front of stack.
Step 7: Create pop function to compute delete data in front of stack.
Step 8: Create peek function to compute ask user to enter a data and check that data is available or
not on stack.
Step 9: Create s object for stack class.
Step 10: Create while loop and it’s true then display '**** Stack ADT Implementation
****', '1) push', '2) pop’, ‘3) peek’ and '4) quit'.
Step 11: Display 'Enter the option : ' and Read the integer input i from user.
Step 12: If i is equal to 1 then read the input data from the user, use push function and display
Pushed Element.
Step 13: Else if i is equal to 2 then pop the element in rear of stack, use pop function and display
Popped Element.
Step 14: Else if i is equal to 3 then use peek function to return Peeked Element is available or not.
Step 15: Else if i is equal to 4 then compute break.
Step 16: Else then display 'Invalid choice !'.
Step 17: Stop the program.
Program:
33
Ex.no: 7 | Implementation of stack and queue data structure using linked list
20CS2013 L-Data Structures and Algorithms Lab URK20CS1116
class node:
def __init__(self, dataval = None):
self.dataval = dataval
self.nextval = None
class stack:
def __init__(self):
self.headval = None
def pop(self):
l = self.headval
self.headval = l.nextval
return l.dataval
def peek(self):
printval = self.headval
i = input("Enter the data to peek : ")
while printval is not None:
if i == printval.dataval:
return str(i) + " is available on stack"
printval = printval.nextval
else:
return str(i) + " is not available on stack"
s = stack()
while True:
print('**** Stack ADT Implementation ****')
print('1) push')
print('2) pop')
print('3) peek')
print('4) quit')
i = int(input('Enter the option : '))
if i == 1:
data = input("Enter the data : ")
34
Ex.no: 7 | Implementation of stack and queue data structure using linked list
20CS2013 L-Data Structures and Algorithms Lab URK20CS1116
s.push(data)
print("Pushed Element : " + str(data))
elif i == 2:
print("Popped Element : " + str(s.pop()))
elif i == 3:
print(s.peek())
elif i == 4:
break
else:
print('Invalid choice !')
Output:
35
Ex.no: 7 | Implementation of stack and queue data structure using linked list
20CS2013 L-Data Structures and Algorithms Lab URK20CS1116
Result:
The above program has been executed for sample input values and output is
verified.
2)
Aim:
To Implementation of queue data structure using linked list.
Algorithm:
Step 1: Start the program.
Step 2: Create a class Node.
Step 3: Create __ init__ function to compute self.data is equal to data & self.next and self.prev is
equal to None.
Step 4: Create a class queue.
Step 5: Create __ init__ function to compute self.head is equal to None.
Step 6: Create enqueue function to compute insert new data in front of queue.
Step 7: Create dequeue function to compute delete data in rear of queue.
Step 8: Create display function to display elements of queue.
Step 9: Create s object for queue class.
Step 10: Create while loop and it’s true then display '***** Implementation of queue *****' and
'1.enqueue\n2.dequeue\n3.display\n4.exit'.
Step 11: Display 'Enter the choice : ' and Read the integer input choice from user.
Step 12: If choice is equal to 1 then read the input data from the user and use enqueue function to
insert data in front of queue.
Step 13: Else if choice is equal to 2 then use dequeue function to delete element in rear of queue.
Step 14: Else if choice is equal to 3 then use display function to display all elements of queue.
Step 15: Else if choice is equal to 4 then compute break.
Step 16: Else then display 'Invalid choice !'.
Step 17: Stop the program.
Program:
class Node:
def __init__(self, data):
36
Ex.no: 7 | Implementation of stack and queue data structure using linked list
20CS2013 L-Data Structures and Algorithms Lab URK20CS1116
self.data = data
self.next = None
self.prev = None
class queue:
def __init__(self):
self.head = None
def enqueue(self, new_data):
new_node = Node(new_data)
new_node.next = self.head
if self.head is not None:
self.head.prev = new_node
self.head = new_node
def dequeue(self):
node = self.head
while node:
last = node
node = node.next
k = last
last = last.prev
last.next = None
return k.data
def display(self):
node = self.head
if node:
s = ""
print("Elements of queue : "),
while node:
s += str(node.data) + " "
node = node.next
print(s)
else:
print("No data on queue")
s = queue()
while True:
print('***** Implementation of queue *****')
print('1.enqueue\n2.dequeue\n3.display\n4.exit')
choice = int(input('Enter your choice : '))
if choice == 1:
37
Ex.no: 7 | Implementation of stack and queue data structure using linked list
20CS2013 L-Data Structures and Algorithms Lab URK20CS1116
Output:
38
Ex.no: 7 | Implementation of stack and queue data structure using linked list
20CS2013 L-Data Structures and Algorithms Lab URK20CS1116
Result:
The above program has been executed for sample input values and output is
verified.
39
Ex.no: 7 | Implementation of stack and queue data structure using linked list
20CS2013 L-Data Structures and Algorithms Lab URK20CS1116
1)
Aim:
To implementation of linear search algorithms.
Algorithm:
Step 1: Start the program.
Step 2: Create linear_search function with parameters list A and integer key.
Step 3: Declare i is equal to 0 and n is equal to length of list A.
Step 4: Create while loop to check i is less than n and A[i] is not equal to key then compute i +=1.
Step 5: If i is less than n then return i.
Step 6: Else return -1.
Step 7: In main program Create list A and get input elements from user.
Step 8: Create integer key and get input integer from user.
Step 9: Use linear_search function to display the index of element in the list.
Step 10: Stop the program.
Program:
def linear_search(A, key):
i=0
n = len(A)
while i < n and A[i] != key:
i += 1
if i < n:
return i
else:
return -1
40
Ex.no: 8 | Implementation of linear search and binary search algorithms
20CS2013 L-Data Structures and Algorithms Lab URK20CS1116
Output:
Result:
The above program has been executed for sample input values and output is
verified.
2)
Aim:
To Implementation of binary search algorithms.
Algorithm:
Step 1: Start the program.
Step 2: Create binary_search function with parameters list A and integer key.
Step 3: Declare low is equal to 0 and high is equal to length of list A - 1.
Step 4: Create while loop to check low is less than or equal to high then compute mid =
(low+high)//2.
Step 5: If A[mid] is equal to key then return mid.
Step 6: Else if key is greater than A[mid] then compute low = mid + 1.
Step 7: Else if key is less than A[mid] then compute low = mid - 1.
Step 8: In main program Create list A and get input elements from user.
Step 9: Create integer key and get input integer from user.
Step 10: Use binary_search function to display the index of element in the list.
Step 11: Stop the program.
Program:
def binary_search(A, key):
low = 0
high = len(A) - 1
41
Ex.no: 8 | Implementation of linear search and binary search algorithms
20CS2013 L-Data Structures and Algorithms Lab URK20CS1116
Output:
Result:
The above program has been executed for sample input values and output is
verified.
42
Ex.no: 8 | Implementation of linear search and binary search algorithms
20CS2013 L-Data Structures and Algorithms Lab URK20CS1116
1)
Aim:
To implementation of Merge Sort algorithms.
Algorithm:
Step 1: Start the program.
Step 2: Declare list A and get input from user.
Step 3: Create merge user defined function to get B, C & A.
Step 4: Declare i, j & k are equal to 0.
Step 5: Declare p is equal to length of B and q is equal to length of C.
Step 6: Create while loop to check i is less than p and j is less than q.
Step 7: If B[i] is less than or equal to C[j], then A[k] is equal to B[i] & increment i .
Step 8: Else A[k] is equal to C[j] & increment j.
Step 9: Increment k.
Step 10: If i is equal to p then create while loop to check j <= q-1 and Compute A[k] = C[j] &
increment j and k.
Step 11: Else create while loop to check i <= p-1 and Compute A[k] = B[i] & increment k and i.
Step 12: Create mergesort user defined function to get A list.
Step 13: Declare n is equal to length of A.
Step 14: If n is greater than 1 then declare B and C list and use for loop to copy first half elements
of A list to B list & copy second half elements of A list to C list.
Step 15: Use mergesort function and give B & C.
Step 16: Use merge function and give B, C & A.
Step 17: Use mergesort function and give A.
Step 18: Display sorted list A.
Step 19: Stop the program.
Program:
A = [int(i) for i in input('Enter the list A : ').split()]
43
Ex.no: 9 | Implementation of Quick Sort and Merge Sort algorithms
20CS2013 L-Data Structures and Algorithms Lab URK20CS1116
def merge(B,C,A):
i=0
j=0
k=0
p = len(B)
q = len(C)
while i < p and j < q:
if B[i] <= C[j]:
A[k] = B[i]
i += 1
else:
A[k] = C[j]
j += 1
k += 1
if i == p:
while j <= q-1:
A[k] = C[j]
k += 1
j += 1
else:
while i <= p - 1:
A[k] = B[i]
k += 1
i += 1
def Mergesort(A):
n = len(A)
if n > 1:
B = []
C = []
for i in range((n/2)):
B.append(A[i])
for i in range((n/2),n):
C.append(A[i])
Mergesort(B)
Mergesort(C)
merge(B,C,A)
Mergesort(A)
print('Sorted list A : '+str(A))
44
Ex.no: 9 | Implementation of Quick Sort and Merge Sort algorithms
20CS2013 L-Data Structures and Algorithms Lab URK20CS1116
Output:
Result:
The above program has been executed for sample input values and output is
verified.
2)
Aim:
To implementation of Quick Sort algorithms.
Algorithm:
Step 1: Start the program.
Step 2: Declare list A and get input from user.
Step 3: Create partition user defined function to get l, r & A.
Step 4: Declare p = A[l], i = l and j = r.
Step 5: While i is less than j then checks while A[i] <= p and i < r and increment i.
Step 6: While A[i] >= p and j > l and decrement j.
Step 7: Swap A[i] and A[j].
Step 8: Swap A[i] & A[j] and A[l] &A[j].
Step 9: Return j.
Step 10: Create quick_sort user defined function to get l, r & A.
Step 11: If l is less than r then compute p is equal to partition(l, r, A).
Step 12: Use quick_sort function and give l, p-1, A & p+1, r, A.
Step 13: Use quick_sort function and give 0, len(A)-1, A.
Step 14: Display sorted list A.
Step 15: Stop the program.
45
Ex.no: 9 | Implementation of Quick Sort and Merge Sort algorithms
20CS2013 L-Data Structures and Algorithms Lab URK20CS1116
Program:
A = [int(i) for i in input('Enter the list A : ').split()]
def partition(l,r,A):
p = A[l]
i=l
j=r
while i < j:
while A[i] <= p and i < r:
i += 1
while A[j] >= p and j > l:
j -= 1
A[i],A[j] = A[j],A[i]
A[i],A[j] = A[j],A[i]
A[l],A[j] = A[j],A[l]
return j
def quick_sort(l, r, A):
if (l < r):
p = partition(l, r, A)
quick_sort(l, p - 1, A)
quick_sort(p + 1, r, A)
quick_sort(0,len(A)-1,A)
print('Sorted list A : '+str(A))
Output:
Result:
The above program has been executed for sample input values and output is
verified.
46
Ex.no: 9 | Implementation of Quick Sort and Merge Sort algorithms
20CS2013 L-Data Structures and Algorithms Lab URK20CS1116
Aim:
To implement the Hash Table with collision resolution techniques.
Algorithm:
Step 1: Start the program.
Step 2: Create fun user defined function to get data and list l & return data % length of list l.
Step 3: Create insert user defined function to get data and list l.
Step 4: Declare pos is equal to fun(data,l).
Step 5: If l[pos] is equal to -1 then l[pos] is equal to data.
Step 6: Else use for loop to increment pos and use if statement to check l[i] is equal to -1 then l[i]
is equal to data.
Step 7: If i is equal to length of list l then display “Hash table is full”.
Step 8: Create display user defined function use for loop and if statement to display all elements
in hash table.
Step 9: Create search user defined function to search given data in hash table if its available then
return index of that element but it’s not available then return -1.
Step 10: In main program declare list l and use for loop to initialize hash table by insert -1.
Step 11: Use while loop to display four options.
Step 12: If user enter 1 then use insert function to insert data on hash table.
Step 13: Else if user enter 2 then use search function to search elements in hash table.
Step 14: Else if user enter 3 then use display function to display elements in hash table.
Step 15: Else if user enter 4 then use break to stop program.
Step 16: Else if user enter 5 then display ‘Invalid Option !’.
Step 17: Stop the program.
Program:
def fun(data,l):
return (data % len(l))
def insert(data,l):
47
Ex.no: 10 | Implementation of Hash Table with collision resolution techniques
20CS2013 L-Data Structures and Algorithms Lab URK20CS1116
pos = fun(data,l)
if l[pos] == -1:
l[pos] = data
else:
for i in range(pos+1,len(l)):
if l[i] == -1:
l[i] = data
break
i+=1
if i == len(l):
print("Hash table is full")
def display():
for i in range(len(l)):
if l[i] != -1:
print(l[i])
def search(data):
for i in range(len(l)):
if l[i] != -1:
if l[i] == data:
return i
else:
return -1
l = []
for i in range(15):
l.append(-1)
while(True):
print('1) Insert\n2) Search\n3) Display\n4) Exit')
op = int(input('Enter the option : '))
if op == 1:
data = int(input('Enter the data to insert : '))
insert(data, l)
elif op == 2:
data = int(input('Enter the data for search : '))
print('Position of searching element is ' + str(search(data)))
elif op == 3:
display()
elif op == 4:
break
48
Ex.no: 10 | Implementation of Hash Table with collision resolution techniques
20CS2013 L-Data Structures and Algorithms Lab URK20CS1116
else:
print("Invalid Option !")
Output:
49
Ex.no: 10 | Implementation of Hash Table with collision resolution techniques
20CS2013 L-Data Structures and Algorithms Lab URK20CS1116
Result:
The above program has been executed for sample input values and output is
verified.
50
Ex.no: 10 | Implementation of Hash Table with collision resolution techniques