You are on page 1of 2

GOVT POSTGRADUATE COLLEGE MANSEHRA

DEPARTMENT OF COMPUTER SCIENCE


Assignment:2
Subject: Data Structure and Algorithms Due Date: 26 Nov 2022
Instructor: Waqas A. Khan Max Marks: 100
Objective:
The objective of this assignment is to give you an idea about some operations on Linked List.
Instructions:
It should be clear that your assignment will not get any credit:
 If the assignment is submitted after due date.
 If two solutions are found similar, (no credit will be allocated to any of the students).

Question 1.
Write the following algorithms and implement them in Python.
1. Create a linked list
2. Determine whether the linked list is empty
3. Retrieve the first element in the linked list
4. Retrieve the last element in the linked list
5. Find the length of the linked list
6. Display the linked list (direct and reverse order)
7. Search the list (for an item, for a location, for sorted linked list)
8. Insert an item in the linked list(Beginning, any location, End)
9. Delete an item from the linked list (Beginning, any location, End)
10. Compare 2 lists for equality (equals)
Question 2.
Write an Algorithm and implement it in Python to delete a node from the middle of a circular linked list.

Question 3.
Write an Algorithm and implement it in Python to delete all occurrences of an item from a linked list. So if
the item is 7 and the list is [1,3,7,4,3,7,2], the result is [1,3,4,3,2].
Question 4.
Write an Algorithm and implement it in Python to count the number of times a particular item occurs in a
linked list. So if the item is 7 and the list is [1,3,7,4,3,7,2], the result is 2.
Question 5.
Write an Algorithm and implement it in Python to for given a linked list of numbers and a number, add the
number to all the numbers in the list, so if the number is 20 and the list is [1,5,12,9,7,16], the result is
[21,25,32,29,27,36].

Question 6.
Write an Algorithm and implement it in Python to for given a linked list and two items, insert the second item
after every occurrence of the first item in the list. So if the list is [2,4,3,2,8,2,5,1,2] and the items are 2 and 10,
the result is [2,10,4,3,2,10,8,2,10,5,1,2,10].

Question 7.
Write an Algorithm and implement it in Python to concatenate two linked lists. Given lists l1 = (2, 3, 1) and l2
= (4, 5), after return from concatenate (l1,l2) the list l1 should be changed to be l1 = (2, 3, 1, 4, 5). Your
function should not change l2 and should not directly link nodes from l1 to l2 (i.e. the nodes inserted into l1
should be copies of the nodes from l2.)

Question 8.
Write an Algorithm and implement it in Python to insert a number into a sorted linked list. Assume the list is
sorted from smallest to largest value. After insertion, the list should still be sorted. Given the list l1 = (3, 17,
18, 27) and the value 20, on return l1 be the list (3, 17, 18, 20, 27).

Question 9.
Write an Algorithm and implement it in Python to return the median value in a sorted linked list. If the length
i of the list is odd, then the median is the ceiling(i/2) member. For example, given the list (1, 2, 2, 5, 7, 9, 11)
as input, your function should return the value 5. If the length of the list is even, then the median is the mean
of the i/2 and (i/2)+1 members. Thus, the median of the sorted list (2, 4, 8, 9) is (4+8)/2. Finally, define the
median of an empty list to be 0.
Question 10.
Write an Algorithm and implement it in Python to reverse the nodes in a linked list. Your function should
have time complexity O(n), where n is the length of the list. You should create no new nodes.

**********GOOD LUCK**********

You might also like