0% found this document useful (0 votes)
31 views3 pages

Lab 10

The document describes the tasks for Lab 10 on data structures and algorithms. Students are instructed to implement doubly linked lists, circular linked lists, and multi-level linked lists. They must write functions for operations like insertion, deletion, searching, and printing for each linked list type. The tasks aim to practice various linked list data structures and their applications. Students are told to submit their solutions individually as Python files on Google Classroom by a certain naming convention.

Uploaded by

ali ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views3 pages

Lab 10

The document describes the tasks for Lab 10 on data structures and algorithms. Students are instructed to implement doubly linked lists, circular linked lists, and multi-level linked lists. They must write functions for operations like insertion, deletion, searching, and printing for each linked list type. The tasks aim to practice various linked list data structures and their applications. Students are told to submit their solutions individually as Python files on Google Classroom by a certain naming convention.

Uploaded by

ali ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Computer Science, Usman Institute of Technology

CS211 - Data Structures and Algorithms

Lab 10

Instructor: Dr. Sharaf Hussain


E-mail: [email protected]
Semester: Fall, 2021

1 Objective
The purpose of this lab session is to implement Doubly, Circular, and Multi Linked List data
structure and its applications.

2 Instructions
You have to perform the following tasks yourselves. Raise your hand if you face any difficulty in
understanding and solving these tasks. Plagiarism is an abhorrent practice and you should not
engage in it.

3 How to Submit
• Submit lab work in a single .py file on Google Classroom. (No other format will be accepted) •
Lab work file name should be saved with your roll number (e.g. 19a-001-SE_LW04.py) • Submit

home work in a single .py file on Google Classroom. (No other format will be accepted) • Lab

work file name should be saved with your roll number (e.g. 19a-001-SE_HW04.py)

Task 1 - Doubly Linked List


Implement following Doubly Linked List, and write delete, search, and printList operations in the
following code.

class Node ( object ) :


# Each node has its data and a pointer that points to next node in the Linked List
def __init__ ( self , data , next = None , previous = None ) :
self . data = data ;
self . next = next ;
self . previous = previous

class DoublyLinkedList ( object ) :


def __init__ ( self ) :
self . head = None

# for inserting at beginning of linked list


def insertAtStart ( self , data ) :
if self . head == None :
newNode = Node ( data )
self . head = newNode
else :
newNode = Node ( data )

Computer Science, Usman Institute of Technology

self . head . previous = newNode


newNode . next = self . head
self . head = newNode

# for inserting at end of linked list


def insertAtEnd ( self , data ) :
newNode = Node ( data )
temp = self . head
while ( temp . next != None ) :
temp = temp . next
temp . next = newNode
newNode . previous = temp

Task 2 - Circular Linked List


Implement following Singly Circular Linked List Queue and convert it into Doubly Circular Linked
List Queue.
class CircularQueue :
""" Queue implementation using circularly linked list for storage . """

#
--- --- ---- ---- --- ---- ---- ---- --- ---- ---- ---- --- ---- ---- --- ---- ---- ---- --- ---- ----

# nested _Node class


class _Node :
""" Lightweight , nonpublic class for storing a singly linked node . """ __slots__ = ’_element ’, ’_next ’ #
streamline memory usage

def __init__ ( self , element , next ) :


self . _element = element
self . _next = next

# end of _Node class


#
--- --- ---- ---- --- ---- ---- ---- --- ---- ---- ---- --- ---- ---- --- ---- ---- ---- --- ---- ----

def __init__ ( self ) :


""" Create an empty queue ."""
self . _tail = None # will represent tail of queue self . _size = 0 # number of queue elements

def __len__ ( self ) :


""" Return the number of elements in the queue . """
return self . _size

def is_empty ( self ) :


""" Return True if the queue is empty . """
return self . _size == 0

def first ( self ) :


""" Return (but do not remove ) the element at the front of the queue .

Raise Empty exception if the queue is empty .


"""
if self . is_empty () :
raise ValueError (’Queue is empty ’)
head = self . _tail . _next
return head . _element

def dequeue ( self ) :


""" Remove and return the first element of the queue (i.e. , FIFO ).

Raise Empty exception if the queue is empty .


"""
if self . is_empty () :
raise ValueError (’Queue is empty ’)
oldhead = self . _tail . _next
if self . _size == 1: # removing only element
self . _tail = None # queue becomes empty

Computer Science, Usman Institute of Technology

else :
self . _tail . _next = oldhead . _next # bypass the old head
self . _size -= 1
return oldhead . _element

def enqueue ( self , e ) :


""" Add an element to the back of queue . """
newest = self . _Node (e , None ) # node will be new tail node if self . is_empty () :
newest . _next = newest # initialize circularly else :
newest . _next = self . _tail . _next # new node points to head self . _tail . _next = newest # old tail points
to new node self . _tail = newest # new node becomes the tail self . _size += 1

def rotate ( self ) :


""" Rotate front element to the back of the queue . """
if self . _size > 0:
self . _tail = self . _tail . _next # old head becomes new tail

Task 3 - Multi-Linked List


Implement following Multi-Linked List and implement following operations.
• createNode(data) - create a Node in the linked list

• createChild(parent,child) - create a child node under the parent node •

printList() - Print a multi-linked list with all its associated childern

• searchItem(data) - Returns the position of data item in the list (e.g. parent, child, and node
number)

class Node :
def __init__ ( self , data ) :
self . data = data
self . next = None
self . child = None

class multiLevelLinkedList () :
def __init__ ( self ) :
self . _head = None
self . _size =0

def traverseNext ( self , Node , data ) :


curNode = Node
while ( curNode != None ) :
if curNode . data == data :
return nextlink , True
nextlink = curNode
curNode = curNode . next
return nextlink , False

def traverseChild ( self , Node ) :


curNode = Node
while ( curNode != None ) :
nextchild = curNode
curNode = curNode . child
return nextchild

You might also like