0% found this document useful (0 votes)
46 views24 pages

Linked Lists 2

Linked lists are dynamic data structures that store elements non-contiguously using nodes and pointers. Each node contains a data element and a pointer to the next node. This allows for easy insertion and deletion compared to arrays. There are different types of linked lists including singly, doubly, and circular linked lists as well as skip lists that can improve search performance. Self-organizing lists reorder elements based on search frequency to optimize for locality of reference.
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)
46 views24 pages

Linked Lists 2

Linked lists are dynamic data structures that store elements non-contiguously using nodes and pointers. Each node contains a data element and a pointer to the next node. This allows for easy insertion and deletion compared to arrays. There are different types of linked lists including singly, doubly, and circular linked lists as well as skip lists that can improve search performance. Self-organizing lists reorder elements based on search frequency to optimize for locality of reference.
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/ 24

102

LESSON 2. LINKED LISTS

A linked list is a sequence of data structures, which are connected together via links. They are a dynamic
in nature which allocates the memory when required. Insertion and deletion operations can be easily
implemented.

Lesson Objectives:
At the end of this lesson, you will be able to:
• Understand what is a linked list
• Learn the linked list representation
• Learn linked list basic operation
• Understand the different types of linked list
WHAT IS A LINKED LIST?
• A linked list is a linear data structure, in which the elements are not stored at contiguous memory
locations.
• A linked list consists of nodes where each node contains a data field and a reference (link) to the next
node in the list.
• Like arrays, Linked List is a linear data structure. Unlike arrays, linked list elements are not stored at a
contiguous location; the elements are linked using pointers.

COMPARISON OF LINKED LIST TO ARRAY


• Arrays can be used to store linear data of similar types, but arrays have the following limitations.
1. The size of the arrays is fixed: So we must know the upper limit on the number of elements in advance.
Also, generally, the allocated memory is equal to the upper limit irrespective of the usage.
2. Inserting a new element in an array of elements is expensive because the room has to be created for the
new elements and to create room existing elements have to be shifted.
• For example, in a system, if we maintain a sorted list of IDs in an array id[]
id[] = [1000, 1010, 1050, 2000, 2040].
And if we want to insert a new ID 1005, then to maintain the sorted order, we have to move all the
elements after 1000 (excluding 1000).
Deletion is also expensive with arrays until unless some special techniques are used. For example, to
delete 1010 in id[], everything after 1010 has to be moved.

Learning Module on PROG 201


103

ADVANTAGES OF LINKED LIST


a) Dynamic Size
b) Ease of insertion and deletion

IMPORTANT TERMS TO UNDERSTAND

Link- Each link of a linked list can store a data called an element.
Next- Each link of a linked list contains a link to the next link called Next.
LinkedList- contains the connection link to the first link called First.

LINKED LIST 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:
a) Data
b) Pointer
TYPES OF LINKED LIST
a. Single Linked List

• Linked List contains a


link element called first.
• Each link carries a data field(s) and a link field called next.

Learning Module on PROG 201


104

• Each link is linked with its next link using its next link.
• Last link carries a link as null to mark the end of the list.

SINGLE LINKED LIST BASIC OPERATIONS

INSERTION OPERATION

Learning Module on PROG 201


105

Learning Module on PROG 201


106

DELETION OPERATION

Learning Module on PROG 201


107

REVERSE OPERATION

Learning Module on PROG 201


108

SINGLY LINKED LIST EXAMPLE PROGRAM (INSERT, DELETE DISPLAY AND COUNT)

Learning Module on PROG 201


109

Learning Module on PROG 201


110

OUTPUT:

Learning Module on PROG 201


111

OUTPUT: INSERT & DISPLAY

Learning Module on PROG 201


112

b. DOUBLY LINKED LIST

Doubly Linked List Basic Operations

Doubly Linked List Implementation in C++


Example

Learning Module on PROG 201


113

OUTPUT

CIRCULAR LINKED LIST BASIC OPERATION

Learning Module on PROG 201


114

C++ Implementation

Learning Module on PROG 201


115

Learning Module on PROG 201


116

OUTPUT:

Learning Module on PROG 201


117

SKIP LIST

• A skip list is a data structure that is used for storing a sorted list of items with a help of hierarchy of
linked lists that connect increasingly sparse subsequences of the items.

• A skip list allows the process of item look up in efficient manner. The skip list data structure skips
over many of the items of the full list in one step, that’s why it is known as skip list.

• The idea is simple, we create multiple layers so that we can skip some nodes.
• See the following example list with 16 nodes and two layers. The upper layer works as an “express
lane” which connects only main outer stations, and the lower layer works as a “normal lane” which
connects every station. Suppose we want to search for 50, we start from first node of “express lane”
and keep moving on “express lane” till we find a node whose next is greater than 50. Once we find
such a node (30 is the node in following example) on “express lane”, we move to “normal lane”
using pointer from this node, and linearly search for 50 on “normal lane”. In following example, we
start from 30 on “normal lane” and with linear search, we find 50.

INSERTION IN SKIP LIST


We will start from highest level in the list and compare key of next node of the current node with the key to be
inserted. Basic idea is If –
1. Key of next node is less than key to be inserted then we keep on moving forward on the same level
2. Key of next node is greater than the key to be inserted then we store the pointer to current
node i at update[i] and move one level down and continue our search.

Learning Module on PROG 201


118

C++ IMPLEMENTATION OF SKIP LIST INSERTION

Learning Module on PROG 201


119

Learning Module on PROG 201


120

Learning Module on PROG 201


121

Learning Module on PROG 201


122

OUTPUT:

SELF- ORGANIZING LIST

• Self-Organizing list basically updates the list of given range of items on the basis of last searched item.
In this method, the sequential searching approach is used. This algorithm shifts the more important data
to the beginning of the list. The time complexity of this search technique is O(n).

A Self Organizing list reorders its nodes based on searches which are done. The idea is to use locality of
reference (In a typical database, 80% of the access are to 20% of the items). Following are different strategies
used by Self Organizing Lists.
1) Move-to-Front Method: Any node searched is moved to the front. This strategy is easy to implement, but it
may over-reward infrequently accessed items as it always move the item to front.
2) Count Method: Each node stores count of the number of times it was searched. Nodes are ordered by
decreasing count. This strategy requires extra space for storing count.
3) Transpose Method: Any node searched is swapped with the preceding node. Unlike Move-to-front, this
method does not adapt quickly to changing access patterns.

Learning Module on PROG 201


123

Algorithm

Begin
Function FibonacciSearch().
Calculate the mid value using ‘start+fib[index-2]’ expression.
If the chosen item is equal to the value at mid index, print result and return to main.
If it is lesser than the value at mid index, proceed with the left sub-array.
If it is more than the value at mid index, proceed with the right sub-array.
If the calculated mid value is equal to either start or end then the item is not found in the array.
End
Note: The level of nodes is decided randomly, so output may differ.

Learning Module on PROG 201


124

OUPUT

Learning Module on PROG 201


125

SUMMARY
• A linked list is a linear data structure, in which the elements are not stored at contiguous memory
locations.
• A linked list consists of nodes where each node contains a data field and a reference (link) to the next
node in the list.
• Each link of a linked list can store a data called an element.
• Each link of a linked list contains a link to the next link called next.
• Single Linked List has basic operations such as insertion, deletion, display and search.
• In singly linked list, the next pointer of the last node points to the first node.
• Doubly linked list contains element called first and last
• In doubly linked list, the next pointer of the last node points to the first node and the previous pointer of
the first node points to the last node making the circular in both directions.
• Doubly linked list has basic operations such as insertion, deletion, insert last, delete last insert after,
delete last , insert after, delete, display forward, display backward
• The skip list idea is simple, creating multiple layers so can skip some nodes.
• Self-Organizing list basically updates the list of given range of items on the basis of last searched item.

Learning Module on PROG 201

You might also like