Data Structures & Algorithms Fall 2019 Lab # 7
LAB 07: Linked List
CS211 – Data Structures and Algorithms
Usman Insti tute of Technology
Fall 2019
How to submit:
o Create an account on http://www.turnitin.com/ as a Student (if you don’t have already)
o Use following information at time of sign-up
CS Section A
Class ID: 22664649
Enrollment Key: DSFALL19CSA
CS Section B
Class ID: 22664651
Enrollment Key: DSFALL19CSB
A. Create a class LinkedList and implement the singly Linked List operations in
the following order.
1. Add a constructor of the class that takes one argument data in order to set the elements
for the List. The constructor should also initialize head and next node pointers
class LinkedList:
def __init__(self,data):
// your code goes here
2. Add a function InsertatFirst() that inserts a value at the beginning of the List.
def InsertatFirst(self,value):
// your code goes here
Example:
L1 = LinkedList()
L1.InsertatFirst(2)
L1.InsertatFirst(3)
# The items will be inserted like this:
3
2
Data Structures & Algorithms Fall 2019 Lab # 7
3. Add a function InsertatEnd() that inserts a value at the end of the List.
def InsertatEnd(self, value):
// your code goes here
Example:
L1 = LinkedList()
L1.InsertatFirst(2)
L1.InsertatFirst(3)
L1.InsertatEnd(4)
# The items will be inserted like this:
3
2
4
4. Add a function Insertafter() that inserts a value after a given item in the List.
def Insertafter(self,item,value):
// your code goes here
Example:
L1 = LinkedList()
L1.InsertatFirst(2)
L1.InsertatFirst(3)
L1.InsertatEnd(4)
L1.Insertafter(2,5)
# The items will be inserted like this:
3
2
5
4
5. Add a function DeleteatFirst() which deletes an element from start of the list.
def DeleteatFirst(self):
// your code goes here
Example:
L1 = LinkedList()
L1.InsertatFirst(2)
L1.InsertatFirst(3)
Data Structures & Algorithms Fall 2019 Lab # 7
L1.InsertatEnd(4)
L1.Insertafter(2,5)
L1.DeleteatFirst()
# The List will look like this:
2
5
4
6. Add a function DeleteatEnd() which deletes an element at the end of the list.
def DeleteatEnd():
// your code goes here
Example:
L1 = LinkedList()
L1.InsertatFirst(2)
L1.InsertatFirst(3)
L1.InsertatEnd(4)
L1.Insertafter(2,5)
L1.DeleteatFirst()
L1.DeleteatEnd()
# The List will look like this:
2
5
7. Add a function DeletebyValue() which deletes a node from the list by passing a value.
def DeletebyValue(self,value):
// your code goes here
Example:
L1 = LinkedList()
L1.InsertatFirst(2)
L1.InsertatFirst(3)
L1.InsertatEnd(4)
L1.Insertafter(2,5)
L1.DeletebyValue(3)
# The List will look like this:
2
5
4
Data Structures & Algorithms Fall 2019 Lab # 7
B. Create a class Stack and implement the Stack operations using Linked List.
class Stack:
def __init__(self):
// your code goes here
C. Create a class Queue and implement the Queue operations using Linked List.
class Queue:
def __init__(self):
// your code goes here
Home Assignment
Modify the Singly Linked List program to implement the operations of doubly linked list.