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

Singly Linked List in Python: Objective

This document discusses implementing a singly linked list in Python. It begins by stating the objectives of understanding singly linked lists and implementing them using dynamic structures. It then provides information on what linked lists are, how they are represented, and how to structure classes for nodes and the linked list itself. It presents a scenario where a university library wants to maintain book data in a singly linked list. It lists functions to implement for this like insertion, deletion, splitting, joining and displaying the list. It ends with two post-lab tasks, one to complete a swap function and another to design a sorted insertion function.

Uploaded by

ALvi
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)
138 views3 pages

Singly Linked List in Python: Objective

This document discusses implementing a singly linked list in Python. It begins by stating the objectives of understanding singly linked lists and implementing them using dynamic structures. It then provides information on what linked lists are, how they are represented, and how to structure classes for nodes and the linked list itself. It presents a scenario where a university library wants to maintain book data in a singly linked list. It lists functions to implement for this like insertion, deletion, splitting, joining and displaying the list. It ends with two post-lab tasks, one to complete a swap function and another to design a sorted insertion function.

Uploaded by

ALvi
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

Singly Linked List in Python

Objective
– Understand the concepts of singly linked lists
– Implement singly linked list using dynamic structures

Linked Lists:

A linked list is a data structure consisting of a group of nodes which together represent a
sequence. Under the simplest form, each node is composed of a datum and a reference (in
other words, a link) to the next node in the sequence; more complex variants add additional
links. This structure allows for efficient insertion or removal of elements from any position in the
sequence.
last
first

Representation:

A linked list is represented by a pointer to the first node of the linked list. The first node is called
the head. If the linked list is empty, then the value of the head is NULL.

Each node in a list consists of at least two parts:

1) data

2) Reference to the next node


How to structure classes for linked list

# Node class

class Node:

# Function to initialize the node object


def __init__(self, data):
self.data = data # Assign data
self.next = None # Initialize
# next as null

# Linked List class


class LinkedList:

# Function to initialize the Linked


# List object
def __init__(self):
self.head = None

LAB TASK
Consider a scenario where a University wants to maintain the data of books in library. The data
containing Book ID, Book Name, Author Name are saved in a singly linked list.

Book ID Next
BName
AName

Create following functions for the employee list.


InsertAthead: Insertion of a record at the head.
Append: Insertion of a record at the end.
Insert: Insertion of a record at any position in the list(consider all the cases)
Delete_P: Deletion of a record at any position in the list(consider all the cases)
Display: Displaying all records (starting from head to last).
DisplayII: Displaying all records (starting from last to head).
Split_fun: a function to split the given linked list in two separate lists from the
given position and display the both lists using the display function.
Join_fun: A function that takes 2 lists as an argument and concatenate them in
single 3rd list. Display the third list using the display function
POST LAB

1. Complete this swap function:


def swap (a, b)
# swap the nodes without exchanging the data
# One of them may a head node
# consider all the cases
2. Design a function sorted_insert() that will insert the incoming node in the sorted order.
Sorting is done based on BookID in ascending order. Use the node structure of the lab
task for this function.

You might also like