You are on page 1of 9

3/9/23, 5:14 PM 2230851_assignment-3 - Colaboratory

1. Write a function getAverage(Head) that will take head of a singly linked list and return
the average.

1 class Node:
2     def __init__(self, data):
3         self.data = data
4         self.next = None
5
6 class LinkedList:
7     def __init__(self):
8         self.head = None
9         self.size = 0
10
11     def append(self, new_data):
12         new_node = Node(new_data)
13         if self.head is None:
14             self.head = new_node
15         else:
16             current_node = self.head
17             while current_node.next is not None:
18                 current_node = current_node.next
19             current_node.next = new_node
20         self.size += 1
21
22     def getAverage(self):
23         sum = 0
24         size = 0
25         current_node = self.head
26         while current_node is not None:
27             sum = sum + current_node.data
28             size = size + 1
29             current_node = current_node.next
30         avg = sum / size
31         return avg
32
33 print("Enter a list of numbers (separated by space):")
34 numbers = list(map(int, input().split()))
35
36 sll = LinkedList()
37 for i in numbers:
38     sll.append(i)
39
40 print(f"Average of the list: {sll.getAverage():.2f}")
41

Enter a list of numbers (separated by space):


5 6 7 8
Average of the list: 6.50

2.Write a function numOfOccurrences(Head, value) that will take Head of a singly linked
list and return the number of occurrences of the value in the list.

1 class Node:
2     def __init__(self, data):
3         self.data = data
4         self.next = None
5
6 class LinkedList:
7     def __init__(self):
8         self.head = None
9
10     def append(self, data):

https://colab.research.google.com/drive/1eUuUcuWvgwaZzY4jeNWrRAXGyN-aefuc?authuser=1#scrollTo=DbyN95Sk4u04&printMode=true 1/9
3/9/23, 5:14 PM 2230851_assignment-3 - Colaboratory
11         new_node = Node(data)
12         if self.head is None:
13             self.head = new_node
14         else:
15             current_node = self.head
16             while current_node.next is not None:
17                 current_node = current_node.next
18             current_node.next = new_node
19
20     def numOfOccurrences(self, value):
21         current_node = self.head
22         count = 0
23         while current_node is not None:
24             if current_node.data == value:
25                 count += 1
26             current_node = current_node.next
27         return count
28
29 numbers = [1, 2, 3, 4, 3, 5, 3]
30 sll = LinkedList()
31 for i in numbers:
32     sll.append(i)
33
34 value = 3
35 print(f"Number of occurrences of {value}: {sll.numOfOccurrences(value)}")
36
Number of occurrences of 3: 3

3.You’re given the pointer to the head nodes of two singly linked lists. Compare the data
in the nodes of the linked lists to check if they are equal. The lists are equal only if they
have the same number of nodes and corresponding nodes contain the same data.

1 class Node:
2     def __init__(self, data):
3         self.data = data
4         self.next = None
5
6 class LinkedList:
7     def __init__(self):
8         self.head = None
9         self.size = 0
10
11     def append(self, new_data):
12         new_node = Node(new_data)
13         if self.head is None:
14             self.head = new_node
15         else:
16             current_node = self.head
17             while current_node.next is not None:
18                 current_node = current_node.next
19             current_node.next = new_node
20         self.size += 1
21         
22 def compare_linked_lists(head1, head2):
23     while head1 and head2:
24         if head1.data != head2.data:
25             return False
26         head1 = head1.next
27         head2 = head2.next
28     if head1 or head2:
29         return False
30     return True
31
32

https://colab.research.google.com/drive/1eUuUcuWvgwaZzY4jeNWrRAXGyN-aefuc?authuser=1#scrollTo=DbyN95Sk4u04&printMode=true 2/9
3/9/23, 5:14 PM 2230851_assignment-3 - Colaboratory
33 print("Enter a list of numbers (separated by space):")
34 numbers = list(map(int, input().split()))
35
36 sll1 = LinkedList()
37 for i in numbers:
38     sll1.append(i)
39
40 print("Enter a list of numbers (separated by space):")
41 numbers = list(map(int, input().split()))
42
43 sll2 = LinkedList()
44 for i in numbers:
45     sll2.append(i)
46
47 print(compare_linked_lists(sll1.head, sll2.head))  
48
49
50 print("Enter a list of numbers (separated by space):")
51 numbers = list(map(int, input().split()))
52
53 sll3 = LinkedList()
54 for i in numbers:
55     sll3.append(i)
56
57 print("Enter a list of numbers (separated by space):")
58 numbers = list(map(int, input().split()))
59
60 sll4 = LinkedList()
61 for i in numbers:
62     sll4.append(i)
63
64 print(compare_linked_lists(sll3.head, sll4.head)) 
65
Enter a list of numbers (separated by space):
10 11 12 13
Enter a list of numbers (separated by space):
10 11 12 13
True
Enter a list of numbers (separated by space):
1 2 3 4
Enter a list of numbers (separated by space):
1 2 3
False

4. Assume, Head is the head-node of a singly linked list (sll). Write a utility function
addNodeBeforeValue (self, givenValue, newValue) to insert a new value in this sll just
before the given value.

1 class Node:
2     def __init__(self, data):
3         self.data = data
4         self.next = None
5
6 class LinkedList:
7     def __init__(self):
8         self.head = None
9     
10     def append(self, data):
11         newNode = Node(data)
12         if self.head is None:
13             self.head = newNode
14             return
15         curr = self.head
16         while curr.next is not None:
17             curr = curr.next
18         curr.next = newNode

https://colab.research.google.com/drive/1eUuUcuWvgwaZzY4jeNWrRAXGyN-aefuc?authuser=1#scrollTo=DbyN95Sk4u04&printMode=true 3/9
3/9/23, 5:14 PM 2230851_assignment-3 - Colaboratory
19
20
21     def addNodeBeforeValue(self, givenValue, newValue):
22         if self.head is None:
23             return
24         
25         if self.head.data == givenValue:
26             newNode = Node(newValue)
27             newNode.next = self.head
28             self.head = newNode
29             return
30
31         prev = None
32         curr = self.head
33         while curr is not None and curr.data != givenValue:
34             prev = curr
35             curr = curr.next
36         
37         if curr is not None:
38             newNode = Node(newValue)
39             prev.next = newNode
40             newNode.next = curr
41         else:
42             print("Not found")
43
44 print("Enter a list of numbers (separated by space):")
45 numbers = list(map(int, input().split()))
46
47 linkedlist = LinkedList()
48 for i in numbers:
49     linkedlist.append(i)
50
51 linkedlist.addNodeBeforeValue(7, 4) # Insert 4 before 7
52 linkedlist.addNodeBeforeValue(14, 6) # Print "Not found"
53
54 curr = linkedlist.head
55 print("Updated list")
56 while curr is not None:
57     print(curr.data, end=" ")
58     curr = curr.next
59
Enter a list of numbers (separated by space):
3 6 7 10
Not found
Updated list
3 6 4 7 10

5. Remove the even numbers from the following singly linked list. Write a utility function
deleteeven(self) that will delete all even nodes from the singly linedlist

1 class Node:
2     def __init__(self, data):
3         self.data = data
4         self.next = None
5
6 class LinkedList:
7     def __init__(self):
8         self.head = None
9         self.size = 0
10
11     def append(self, new_data):
12         new_node = Node(new_data)
13         if self.head is None:
14             self.head = new_node
15         else:
16             current_node = self.head
17 while current node.next is not None:
https://colab.research.google.com/drive/1eUuUcuWvgwaZzY4jeNWrRAXGyN-aefuc?authuser=1#scrollTo=DbyN95Sk4u04&printMode=true 4/9
3/9/23, 5:14 PM 2230851_assignment-3 - Colaboratory
17             while current_node.next is not None:
18                 current_node = current_node.next
19             current_node.next = new_node
20         self.size += 1
21         
22     def deleteeven(self):
23         if self.head is None:
24             return
25         
26         while self.head is not None and self.head.data % 2 == 0:
27             self.head = self.head.next
28             self.size -= 1
29         current_node = self.head
30         while current_node is not None and current_node.next is not None:
31             if current_node.next.data % 2 == 0:
32                 current_node.next = current_node.next.next
33                 self.size -= 1
34             else:
35                 current_node = current_node.next
36     
37 ····def·__str__(self):
38 ········linked_list_str·=·""
39 ········current_node·=·self.head
40 ········while·current_node·is·not·None:
41 ············linked_list_str·+=·str(current_node.data)·+·"·"
42 ············current_node·=·current_node.next
43 ········return·linked_list_str.strip()
44
45
46 print("Enter a list of numbers (separated by space):")
47 numbers = list(map(int, input().split()))
48
49 sll = LinkedList()
50 for i in numbers:
51     sll.append(i)
52
53 print("Original linked list:")
54 print(sll)
55
56 sll.deleteeven()
57
58 print("Updated linked list:")
59 print(sll)
60

Enter a list of numbers (separated by space):


3 10 16 61 108
Original linked list:
3 10 16 61 108
Updated linked list:
3 61

6. Write a utility function function named sorted_insert(self, v) that takes a parameter v


and inserts it into a singly linked list in ascending order. For example, if the list contains
the following items:

1 class Node:
2     def __init__(self, data):
3         self.data = data
4         self.next = None
5         
6 class LinkedList:
7     def __init__(self):
8         self.head = None
9         self.size = 0
10         

https://colab.research.google.com/drive/1eUuUcuWvgwaZzY4jeNWrRAXGyN-aefuc?authuser=1#scrollTo=DbyN95Sk4u04&printMode=true 5/9
3/9/23, 5:14 PM 2230851_assignment-3 - Colaboratory
11     def sorted_insert(self, v):
12         new_node = Node(v)
13
14         if self.head is None or self.head.data >= v:
15             new_node.next = self.head
16             self.head = new_node
17         else:
18             current_node = self.head
19             while current_node.next is not None and current_node.next.data < v:
20                 current_node = current_node.next
21             new_node.next = current_node.next
22             current_node.next = new_node
23             
24     def print_list(self):
25         current_node = self.head
26         while current_node is not None:
27             print(current_node.data, end=" ")
28             current_node = current_node.next
29         print()
30
31
32 print("Enter a list of numbers (separated by space):")
33 numbers = list(map(int, input().split()))
34
35 sll = LinkedList()
36 for i in numbers:
37     sll.sorted_insert(i)
38
39 print("Sorted List")
40 sll print list()
Enter a list of numbers (separated by space):
3 6 10 7 5
Sorted List
3 5 6 7 10

7. Assume there is a singly linked list. HEAD is the pointer for head node. Write a
function reverseList(HEAD) to reverse the linked list. It means, the function will return
the new head. Then print the reversed list.

1 class Node:
2     def __init__(self, data):
3         self.data = data
4         self.next = None
5
6 class LinkedList:
7     def __init__(self):
8         self.head = None
9
10     def append(self, new_data):
11         new_node = Node(new_data)
12         if self.head is None:
13             self.head = new_node
14         else:
15             current_node = self.head
16             while current_node.next is not None:
17                 current_node = current_node.next
18             current_node.next = new_node
19     
20     def print_list(self):
21         current_node = self.head
22         while current_node is not None:
23             print(current_node.data, end=" ")
24             current_node = current_node.next
25         print()
26

https://colab.research.google.com/drive/1eUuUcuWvgwaZzY4jeNWrRAXGyN-aefuc?authuser=1#scrollTo=DbyN95Sk4u04&printMode=true 6/9
3/9/23, 5:14 PM 2230851_assignment-3 - Colaboratory
27 def reverseList(head):
28     prev = None
29     curr = head
30     while curr is not None:
31         next_node = curr.next
32         curr.next = prev
33         prev = curr
34         curr = next_node
35     return prev
36
37 print("Enter a list of numbers (separated by space):")
38 numbers = list(map(int, input().split()))
39
40 sll = LinkedList()
41 for i in numbers:
42     sll.append(i)
43
44 new_head = reverseList(sll.head)
45
46 print("Reversed List")
47 curr = new_head
48 while curr is not None:
49     print(curr.data, end=" ")
50     curr = curr.next
51
Enter a list of numbers (separated by space):
3 10 17 61 107
Reversed List
107 61 17 10 3

8. Assume, HEAD is the head-node of a doubly linked list (dll). Write a utility function
addNodeAfterValue(self, givenValue, newValue) to insert a new value in this dll just after
a given value.

1 class Node:
2     def __init__(self, data):
3         self.data = data
4         self.prev = None
5         self.next = None
6
7 class DoublyLinkedList:
8     def __init__(self):
9         self.head = None
10
11     def append(self, data):
12         newNode = Node(data)
13         if self.head is None:
14             self.head = newNode
15             return
16         curr = self.head
17         while curr.next is not None:
18             curr = curr.next
19         curr.next = newNode    
20
21     def addNodeAfterValue(self, givenValue, newValue):
22         new_node = Node(newValue)
23         if self.head is None:
24             return
25         curr_node = self.head
26         while curr_node is not None and curr_node.data != givenValue:
27             curr_node = curr_node.next
28         if curr_node is None:
29             return
30         new_node.prev = curr_node
31         new_node.next = curr_node.next

https://colab.research.google.com/drive/1eUuUcuWvgwaZzY4jeNWrRAXGyN-aefuc?authuser=1#scrollTo=DbyN95Sk4u04&printMode=true 7/9
3/9/23, 5:14 PM 2230851_assignment-3 - Colaboratory
32         if curr_node.next is not None:
33             curr_node.next.prev = new_node
34         curr_node.next = new_node
35
36 print("Enter a list of numbers (separated by space):")
37 numbers = list(map(int, input().split()))
38
39 dll = DoublyLinkedList()
40 for i in numbers:
41     dll.append(i)
42
43 dll.addNodeAfterValue(4, 7) 
44 dll.addNodeAfterValue(14, 6) 
45
46 curr = dll.head
47 print("Updated list")
48 while curr is not None:
49     print(curr.data, end=" ")
50     curr = curr.next
51
Enter a list of numbers (separated by space):
2 3 4 8 9
Updated list
2 3 4 7 8 9

9. Given a sorted doubly linked list and a value to insert, write a function to insert the
value in sorted way (and in efficient way). Initial doubly linked list

1 class Node:
2     def __init__(self, data):
3         self.data = data
4         self.prev = None
5         self.next = None
6
7 class DoublyLinkedList:
8     def __init__(self):
9         self.head = None
10
11     def append(self, data):
12         newNode = Node(data)
13         if self.head is None:
14             self.head = newNode
15             return
16         curr = self.head
17         while curr.next is not None:
18             curr = curr.next
19         curr.next = newNode
20         newNode.prev = curr
21
22     def insertSorted(self, data):
23         new_node = Node(data)
24         if self.head is None:
25             self.head = new_node
26             return
27         curr = self.head
28         while curr is not None:
29             if data <= curr.data:
30                 new_node.prev = curr.prev
31                 new_node.next = curr
32                 if curr.prev is not None:
33                     curr.prev.next = new_node
34                 else:
35                     self.head = new_node
36                 curr.prev = new_node
37                 return
38             elif curr.next is None:
https://colab.research.google.com/drive/1eUuUcuWvgwaZzY4jeNWrRAXGyN-aefuc?authuser=1#scrollTo=DbyN95Sk4u04&printMode=true 8/9
3/9/23, 5:14 PM 2230851_assignment-3 - Colaboratory

39                 curr.next = new_node
40                 new_node.prev = curr
41                 return
42             curr = curr.next
43
44
45 print("Enter a list of numbers (separated by space):")
46 numbers = list(map(int, input().split()))
47
48 dll = DoublyLinkedList()
49 for i in numbers:
50     dll.append(i)
51
52 dll.insertSorted(9) 
53
54 print("After inserting the value in sorted way")
55 curr = dll.head
56 while curr is not None:
57     print(curr.data, end=" ")
58     curr = curr.next
59

Enter a list of numbers (separated by space):


3 5 8 10 12
After inserting the value in sorted way
3 5 8 9 10 12

Colab paid products - Cancel contracts here

check 12s completed at 5:12 PM

https://colab.research.google.com/drive/1eUuUcuWvgwaZzY4jeNWrRAXGyN-aefuc?authuser=1#scrollTo=DbyN95Sk4u04&printMode=true 9/9

You might also like