You are on page 1of 44

Unit 1- Linear data structures

Linked List Implementation

1
Introduction
• A linked list is a data structure which can change
during execution.
– Successive elements are connected by pointers.
– Last element points to NULL.
– It can grow or shrink in size during execution of a
program.
– It can be made just as long as required.
head
– It does not waste memory space.

A B C

2
• It improves on the construction and
management of an array and Python list by
requiring smaller memory allocations and no
element shifts for insertions and deletions.
• The singly linked list is a linear structure in which
traversals start at the front and progress, one
element at a time, to the end. Other variations
include the circularly linked, the doubly linked,
and the circularly doubly linked list

3
• Keeping track of a linked list:
– Must know the pointer to the first element of the
list (called start, head, etc.).

• Linked lists provide flexibility in allowing the


items to be rearranged efficiently.
– Insert an element.
– Delete an element.

Spring 2012 Programming and Data Structure 4


In essence ...
• For insertion:
– A record is created holding the new item.
– The next pointer of the new record is set to link it to
the item which is to follow it in the list.
– The next pointer of the item which is to precede it
must be modified to point to the new item.
• For deletion:
– The next pointer of the item immediately preceding
the one to be deleted is altered, and made to point
to the item following the deleted item.

Spring 2012 Programming and Data Structure 5


Illustration: Insertion
A B C

Item to be
tmp X inserted

A B C

curr
X

Spring 2012 Programming and Data Structure 6


Illustration: Deletion
Item to be deleted

A B C

tmp
curr

A B C

Spring 2012 Programming and Data Structure 7


Array versus Linked Lists
• Arrays are suitable for:
– Inserting/deleting an element at the end.
– Randomly accessing any element.
– Searching the list for a particular value.
• Linked lists are suitable for:
– Inserting an element.
– Deleting an element.
– Applications where sequential access is required.
– In situations where the number of elements cannot
be predicted beforehand.

8
Uses of Linked Lists
• Due to their dynamic size allocation and ease of
insertion/deletion, linked lists are applied in a lot of use cases.
• They’re used to implement a lot of complex data structures
like the adjacency list in graphs.
• They are used for lifecycle management in operating systems.
• A playlist in a music application is implemented using a
doubly linked list.
• Block chain, a complex data structure that is used for crypto
currencies and ledgers use a linked list at their core.

Spring 2012 Programming and Data Structure 9


Types of Lists
• Depending on the way in which the links are
used to maintain adjacency, several different
types of linked lists are possible.

– Linear singly-linked list (or simply linear list)


head
• One we have discussed so far.

A B C

Spring 2012 Programming and Data Structure 10


– Circular linked list
• The pointer from the last element in the list points back
to the first element.
head

A B C

Spring 2012 Programming and Data Structure 11


– Doubly linked list
• Pointers exist between adjacent nodes in both
directions.
• The list can be traversed either forward or backward.
• Usually two pointers are maintained to keep track of
head tail
the list, head and tail.

A B C

Spring 2012 Programming and Data Structure 12


Basic Operations on a List
• Creating a list
• Traversing the list
• Inserting an item in the list
• Deleting an item from the list
• Concatenating two lists into one

Spring 2012 Programming and Data Structure 13


List is an Abstract Data Type
• What is an abstract data type?
– It is a data type defined by the user.
– Typically more complex than simple data types like
int, float, etc.
• Why abstract?
– Because details of the implementation are hidden.
– When you do some operation on the list, say insert
an element, you just call a function.
– Details of how the list is implemented or how the
insert function is written is no longer required.
Spring 2012 Programming and Data Structure 14
Conceptual Idea

Insert
List
implementation
Delete
and the
related functions
Traverse

15
Pointer structures
• Pointer structures are lists of items that can be spread
out in memory.
• This is because each item contains one or more links
to other items in the structure.
• There are several benefits with pointer structures.
• First of all, they don't require sequential storage
space.
• Second, they can start small and grow arbitrarily as
you add more nodes to the structure

16
Singly Linked Lists

Spring 2012 Programming and Data Structure 17


Creating Node
• At the heart of lists (and several other data
structures) is the concept of a node.
• A node is a container of data, together with
one or more links to other nodes. A link is a
pointer

18
Introduction
• A singly linked list is a list with only one
pointer between two successive nodes.
• It can only be traversed in a single direction,
that is, you can go from the first node in the
list to the last node, but you cannot move
from the last node to the first node.

Spring 2012 Programming and Data Structure 19


Contd.
• If there are n number of nodes in the initial
linked list:
– Allocate n records, one by one.
– Read in the fields of the records.
– Modify the links of the records so that the chain is
formed.

A B C

Spring 2012 Programming and Data Structure 20


21
Singly linked list class
• A list is clearly a separate concept from a node.
• So we start by creating a very simple class to hold our
list.
• We will start with a constructor that holds a
reference to the very first node in the list.

22
SLL-Insert operations

23
SLL – Deletion operations

24
SLL –Display Operations

25
SLL- Searching Operations

26
• ll.search(head, data) -> Search the given
element in the Linked List.
• ll.print_list() -> Print the linked list.
• ll.size() -> Return the length of the linked list.
• ll.insert(ele) -> Insert the given node into the
linked list.
• ll.delete(data) -> Delete the given element
from the linked list.

27
Doubly Linked Lists

28
Introduction
• A node in a doubly linked list has two
pointers: a pointer to the next node and a
pointer to the previous node:

29
• Doubly linked lists can be traversed in any
direction.
• Since there is immediate access to both next
and previous nodes, deletion operations are
much easier to perform

30
Creating a Node in DLL

31
Doubly linked list
• It is still important to create a class that
captures the data that our functions will be
operating on:

32
DLL-Insert Operation.

33
DLL- Deletion Operations
The algorithm for removing nodes from a doubly linked
list caters for basically four scenarios before deletion of
a node is completed. These are:
•When the search item is not found at all
•When the search item is found at the very beginning of the list
•When the search item is found at the tail end of the list
•When the search item is found somewhere in the middle of the
list

Spring 2012 Programming and Data Structure 34


• When the search item is
not found at all
• When the search item is
found at the very
beginning of the list
• When the search item is
found at the tail end of
the list

• When the search item is


found somewhere in the
middle of the list

35
DLL –Deletion Operations

36
DLL- Display Operations

37
Circular Linked Lists

38
Introduction
• A circular list is a special case of a linked list. It is a list where
the endpoints are connected.
• That is, the last node in the list points back to the first node.
• Circular lists can be based on both singly and doubly linked
lists. In the case of a doubly linked circular list, the first node
also needs to point to the last node.

39
Implementing Circular Linked lists
• To create a circular linked list, we create two classes:
the first one for nodes and the second one for the
linked list that will use the nodes.
• For the node class, we have two members. One to
store data and the other to store the link to the next
node. The class definition will be:

40
Class: Circular Linked List
•This class will use nodes created by the
previous class to implement a circular linked list.
It will contain one head node, one count
member, and multiple methods for specific
tasks.

41
CLL – Appending Elements
• When we append an element to the circular list, we need to
make sure that the new node points back to the tail node

42
CLL- Deleting elements

43
Applications

Spring 2012 Programming and Data Structure 44

You might also like