Singly Linked List in Python: Objective

Download as pdf or txt
Download as pdf or txt
You are on page 1of 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) Pointer (Or 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 firm wants to maintain the data of its employees. The data
containing employee number, name, and salary and department # are saved in a singly linked
list.
Emp ID Next
Name
Salary
Dept no

Create following functions for the employee list.


InsertAthead: Insertion of a record at the head.
InsertAtEnd: Insertion of a record at the end.
Insert: Insertion of a record at any position in the list
Insert_before: Insertion of a record before the given Emp ID if present
Search: Searching any record based on employee number and dept no.
Display: Displaying all records(starting from head to last ).
DisplayII: Displaying all records(starting from last to head ).

Selective Display: records of the employees having the salary greater than the given value
are displayed
Count: Count the number of employees in a dept.
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.

POST LAB
Use this node definition on the questions that follow.
class node:
def __init__(i=0):
info=i
next=None

1. Write Python statements for the following


a. Create a node r. Initialize its info with 9 and its next with None

b. Create a node s. Initialize its info with 7 and its next with node r

c. Create a node t. Initialize its info with 3 and its next with node s

2. Draw a diagram of nodes created in part 1 and display the output on the basis of
following statement:
print( t.info + s.info + r.info)

3. Complete this swap function:


def swap (a, b)
// pre: a and b are initialized and not None
// post: the data stored in the info fields of a and b have been
exchanged

You might also like