0% found this document useful (0 votes)
4 views

ds_exp5

Uploaded by

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

ds_exp5

Uploaded by

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

Experiment 5

Aim: Implementation of Singly Linear Linked List.

Practical Learning Objectives:


The objective of this practical is to teach

1. To implement single linked list using Python.

Software Required:
Python 3

Theory:
Linked List may access its storage in a random fashion, because each piece of information
carries with it a link to the next data, item in the chain. A linked list requires complex data
structure, whereas a stack or queue can operate on both simple and complex data item. A linked-
list retrieval operation does not remove and destroy an item from the list; a specific deletion
operation must be added to do this,

Linked lists are used for two main purpose. The First purpose is to create arrays of unknown size
in memory. If you know the amount of storage in advance, you can use an array; but if you do
not know the actual size of a. list, you must use a linked list. The second main usage is for disk-
file storage of databases. The linked list allows you to insert and delete items quickly and easily
without rearranging- the entire disk file. For these reasons, linked lists are used extensively in
database managers.

Linked lists can be either singly linked or doubly linked, A singly linked list contains a link to
the next data item A doubly linked list contains link to both next and the previous element in the
list The type you use depends upon your application.

Linked Lists vs Arrays

Arrays are contiguous: In an array, the elements have to be in a contiguous connected and
sequential) portion of memory. Memory immediately next to the array may already be in use for
something else. So programming languages don't generally provide for arrays that can grow and
shrink in place.

Linked lists are not contiguous: In a linked list, the reference in each node says where to find
the next one. So the nodes can be all over memory.
Implications

With an array, we must choose a size once and for all. (This can waste memory, or we could run
out of spaces).

With a linked list, we can add and remove elements at will. (Each one does take up a little more
space for its link field, and it may take time to `get to' the insertion / removal spot).

With an array, we can immediately access the nth element. With a linked list, we cannot. We
have to follow n references

ARRAY LINKED LIST

1. Array is the collection of similar types of 1. Linked list is collection of nodes, which
data elements. In array all the elements are are stored at different memory location. Every
stored in sequential memory location starting node consists of two part.
from index from zero to (n-1).
i. Data

ii. Link
1
Link Stores address of next node.
3
Structure of node.
2
Node
4
Data link
5

1001-1002 a[0]
head
1003-1004 a[1]

1005-1006 a[2]
20 9100 30 null
1007-1008 a[3]

1009-1010 a[4] a[4]


8200 9100
2. With a linked list, we cannot. We have
to follow n references

3. No shifting is required.

4. Memory is actually deleted/ free in case of


Linked List. Physically node is destroyed.
2. With an array, we can immediately 5. node is an object. All the object is created
access the nth element. at random memory address. In a linked list,
3. Insertion and deletion operation the reference in each node says where to
requires shifting towards right and left find the next one. So, the nodes can be all
respectively. over memory.

4. Deletion takes place only logically. 6. On demand nodes are created.

5. Memory immediately next to the array 7. There is no limitation to create the number
may already be in use for something else. So, of nodes. With a linked list, we can add and
programming languages don't generally remove elements at will. (Each one does take
provide for arrays that can grow and shrink in up a little more space for its link field, and it
place. may take time to `get to' the insertion /
removal spot).
6. In advanced array is created with
specified size

7. Insertion operation fails when array is full.

PROCEDURE:
Algorithm: add(data)
1. Create a new node with the data
2. If the linked list is empty (head node is not referring to any other node), make the head node
and the tail node refer to the new node
3. Otherwise,
a. Make the tail node’s link refer to new node
b. Call the new node as tail node

Algorithm: insert(data, data_before)


1. Create a new node with the given data
2. If the data before is None,
a. Make the new node's link refer to head node
b. Call the new node as head node
c. If the new node's link is None, make it the tail node
3. Else
a. Find the node with data before, once found consider it as node before
b. Make the new node’s link refer to node_before’s link.
c. Make the node_before’s link refer to new node
d. If new node’s link is None, make it the tail node
4. If node with data_before is not found, display appropriate error message

Algorithm: delete(data):
1. Find the node with the given data. If found,
a. If the node to be deleted is head node, make the next node as head node
1. If it is also the tail node, make the tail node as None
b. Otherwise,
1. Traverse till the node before the node to be deleted, call it temp
2. Make temp’s link refer to node’s link.
3. If the node to be deleted is the tail node, call the temp as tail node
4. Make the node's link as None
2. If the node to be deleted is not found, display appropriate error message

CODE:

class Node:

def init (self, data):


self.data = data
self.next = None
class SinglyLinkedList:
def init (self):
self.head = None
self.tail = None
def repr (self):
return ' -> '.join(map(str, self.get_nodes()))
def print_list(self):
nodes = []
current = self.head
while current:
nodes.append(current.data)
current = current.next
return nodes
def add(self, data):
new_node = Node(data)
if self.head is None:
self.head = new_node
self.tail = new_node
else:
self.tail.next = new_node
self.tail = new_node
def delete(self, data):
if self.head is None:
print("The list is empty")
elif self.head.data == data:
self.head = self.head.next
if self.head is None:
self.tail = None
else:
current = self.head
while current.next:
if current.next.data == data:
current.next = current.next.next
if current.next is None:
self.tail = current
return
current = current.next
print("Node not found")
linked_list = SinglyLinkedList()
linked_list.add(1)
linked_list.add(2)
linked_list.add(3)
linked_list.add(4)
linked_list.add(5)
print(linked_list)
linked_list.delete(2)
print(linked_list)
Results:

Conclusion:

Practical Learning Outcomes:


After performing the practical, the learner is able to: Marked

1. To implement single linked list using Python. ✓

Binary Scaling Factor for Practical Assessment

Scaling Factor Attainment Remarks


1 Attained Visible in observation table/
plot/results
0.5 Partially Attained Partially visible in observation
table/
plot/results
0 Not Attained Not visible in observation table/
plot/results
×
Parameter Catego Weightage Marks
ry
Scaling

5 × (1/0)
Factor

performin 5 × (1/0)
Technical Prerequisites
Knowledge Skills in
(Preparedness and experiment g
5 × (1/0)
Execution)
Learning

5 × (1/0)
5 × (1/0)
Technical Format
Documentation
5 × (1/0)
Contents as per format
(Developing skills Quality
for journal writing
and maintenance
of
lab
notebook)

5 × (1/0)
5 × (1/0)
Level of interaction Level of understanding
(Developing
5 × (1/0)
Questions and Answers
expression Application
power)

5 × (1/0)
5 × (1/0)
Behaviour Attitude
al toward Regularity
(Attitude s Team work/ Group 5 × (1/0)
learning) activity

Compliance Course objective 10 × (1/0.5/0)


(attainment attainment

×
Practical Learning 10 (1/0.5/0)
of objectives/
10 × (1/0.5/0)
Outcome 1
outcomes)
Practical Learning

10 × (1/0.5/0)
Outcome 2
Practical Learning
Outcome 3
Total 100
Date of Date of Correction Roll No. Faculty
Performance Signature

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy