SlideShare a Scribd company logo
1 of 51
Introduction
What Is Linked List? (2002)
 linked list is one in which all nodes are linked together
 Each node contains two parts.
Data contains elements .
 Next/Link contains address of next node .
Structure of singly linked list
nextdata
A B C
nextnext datadata
head
 The head always points to the first node .
 All nodes are connected to each other through Link fields.
 Null pointer indicated end of the list.
X
Singly Linked List Contd..
Benefits of linked list over array
 Disadvantages of arrays :
Insertion and deletion operations are slow.
Slow searching in unordered array.
Wastage of memory.
Fixed size.
More expensive
 Linked lists solve some of these problems :
Insertions and deletions are simple and faster.
Linked List reduces the access time.
No wastage of memory.
Linked list is able to grow in size.
Less expensive
Linked List Types
 Single Linked list
 Double linked list
 Circular single linked list
 Circular double linked list
1. Singly
Linked Listhead
Operations on linked list
The basic operations on linked lists are :
1. Insertion
2. Deletion
3. Searching
Singly Linked List Contd..
1. Insertion
 Insertion operation is used to insert a new node in the linked list at the
specified position.
 When the list itself is empty , the new node is inserted as a first node.
 Types of Insertion
There are 3 ways to insert a new node into a list :
 Insertion at the beginning
 Insertion at the end
 Insertion after a particular node
Singly Linked List Contd..
Structure of Node of Single Linked List
/* Class Node */
class Node
{
int data;
Node next;
/* Constructor */
public Node()
{
next= null;
data = 0;
}
/* Function to insert an element at begining */
public void insert()
{
Node newNode = new Node();
if (head == null)
{
newNode.next = null;
head = newNode;
tail = newNode;
} else {
newNode.next = head;
head = newNode;
}
}
}
newnode
Data Next
Algorithm
Step 1: Create a newNode with given value.
Step 2: Check whether list is Empty (head == NULL)
Step 3: If it is Empty then, set newNode→next = NULL and
head = newNode.
Step 4: If it is Not Empty then, set newNode→next = head
and head = newNode.
/* Function to insert an element at end */
public void insert()
{
Node newNode = new Node();
if (head == null) {
head = newNode;
tail = newNode;
} else {
tail.next = newNode;
tail = newNode;
}
}
}
Singly Linked List Contd..
2.Deletion
 Deletion operation is used to delete a particular node in the linked list.
 we simply have to perform operation on the link of the nodes(which
contains the address of the next node) to delete a particular element.
 Types of Deletion
Deletion operation may be performed in the following conditions:
 Delete first element.
 Delete last element.
 Delete a particular element
Singly Linked List Contd..
/* Function to delete an element from first */
public void removeFirst() {
if (head == null)
return;
else {
if (head == tail) {
head = null;
tail = null;
} else {
head = head.next;
}
}
}
Singly Linked List Contd..
Step 1: Check whether list is Empty (head == NULL)
Step 2: If it is Empty then, display 'List is Empty!!! Deletion is
not possible' and terminate the function.
Step 3: If it is Not Empty then, define a Node pointer 'temp'
and initialize with head.
Step 4: Check whether list is having only one node (temp →
next == NULL)
Step 5: If it is TRUE then set head = NULL and delete temp
(Setting Empty list conditions)
Step 6: If it is FALSE then set head = temp → next, and delete
temp.
Deleting the last node of a singly linked list
public void removeLast()
{
if (tail == null)
return;
else {
if (head == tail) {
head = null;
tail = null;
}
else {
Node prev = head;
while (previ.next != tail)
{
prev = prev.next;
}
tail = prev;
tail.next = null;
}
}
}
Singly Linked List Contd..
3. Searching
– Searching involves finding the required element in the list
– We can use various techniques of searching like linear search
or binary search where binary search is more efficient in case
of Arrays
– But in case of linked list since random access is not available it
would become complex to do binary search in it
– We can perform simple linear search traversal
3. Circular
linked list
Circular linked list
A circular linked list is one which has
 No ending.
 The last node of a linked list is connected with the address of its first node .
 In the circular linked list we can insert elements anywhere in the list whereas in the array we
cannot insert element anywhere in the list because it is in the contiguous memory.
/* Function to insert element at begining In Circular linked list*/
public void insert(int val) {
Node newNode= new Node();
if(start == null)
{
newNode.next=newNode
head = newNode;
tail = newNode;
}
else {
head.next=head
last.next = newNode;
head = newNode; }
size++ ;
}
Circular linked list contd..
/* Function to insert element at end In Circular linked list*/
public void insert()
{
Node newNode= new Node();
if(start == null)
{
head=tail= newNode;
newNode.next=newNode;
}
else
{
last= newNode;
last.next=newNode
newnode.next=head;
}
}
Circular linked list contd..
/* Function to insert element at position in circular linked list */
public void insertAtPos(int val , int pos)
{
Node newNode= new Node(val,null);
Node ptr = start;
pos = pos - 1 ;
for (int i = 1; i < size - 1; i++)
{
if (i == pos)
{
Node tmp = ptr.getLink() ;
ptr.setLink( newNode);
newNode.setLink(tmp);
break;
}
ptr = ptr.getLink();
}
size++ ;
}
Circular linked list contd..
public void deletet()
{
if(size==0){
System.out.println("nList is Empty");
}else{
System.out.println("ndeleting node " + head.data + " from start");
head = head.next;
tail.next=head;
size--;
}
}
Circular linked list contd..
Deletion at beginning in the Circular linked list
Function to count the total number of nodes in a linear linked list.
int count(int search_for)
{
Node current = head;
int count = 0;
while (current != null)
{
if (current.data == search_for)
count++;
current = current.next;
}
return count;
}
2. Doubly
Linked List
Doubly Linked List
1. Doubly linked list is a linked data structure that consists of a set of
sequentially linked records called nodes.
2 . Each node contains three fields ::
-: one is data part which contain data only.
-:two other field is links part that are point or references to the
previous or to the next node in the sequence of nodes.
 The beginning and ending nodes' previous and next links respectively, point to
some kind of terminator ,typically a sentinel node or null to facilitate traversal of
the list.
Hence each node has knowledge of its successor and also its predecessor.
In doubly linked list, from every node the list can be traversed in both the directions.
Doubly Linked List (contd.)
Doubly Linked List contd..
DLL’s compared to SLL’s
 Advantages:
 Can be traversed in either direction
(may be essential for some programs)
 Some operations, such as deletion and
inserting before a node, become
easier
 Disadvantages:
 Requires more space
 List manipulations are slower
(because more links must be changed)
 Greater chance of having bugs
(because more links must be
manipulated)
Doubly Linked List contd..
Let’s see how the Node structure will look like
class Node
{
int data;
Node next, prev;
//constructs a new DoublyLinkedList object with head and tail as null
public Node()
{
next = null;
prev = null;
data = 0;
}
Insertion at the beginning of doubly linked list
Doubly Linked List contd..
public Node addAtStart(int data){
System.out.println("Adding Node " + data + " at the start");
Node newnode = new Node(data);
if(head=null)
{
head = newnode;
tail = newnode;
}
else
{
/*Make next of new node as head and previous as NULL */
newnode.next = head;
head.previous = newnode;
head = newnode;
}
size++;
return n;
}
Insertion at last of doubly linked list
Doubly Linked List contd..
public Node addAtEnd(int data)
{
System.out.println("Adding Node " + data + " at the End");
Node newNode= new Node(data);
if(head = =null{
head = newNode;
tail = newNode;
}else{
tail.next = newNode; /*Change the next of last/tail node */
newNode.previous = tail; /*Make last node as previous of new node */
tail =newNode;
}
size++;
return n;
}
Doubly Linked List contd..
Deletion at the beginning of doubly linked list
public void delete()
{
if(head==null)
{
System.out.println("nList is Empty");
}
Else if(head==tail)
{
head=tail=null;
}
else{
System.out.println("ndeleting node " + head.data + " from start");
head = head.next;
size--;
}
}
Doubly Linked List contd..
public void print()
{
Node temp = head;
System.out.print("Doubly Linked List: ");
while(temp!=null){
System.out.print(" " + temp.data);
temp = temp.next;
}
System.out.println();
}
SKIP
LIST
Definition :-
– Many a times a complex problem becomes easier to solve if you have the
appropriate data structure at hand. More so when the input size involved is very
large.
– Skip Lists are an alternative representation of balanced trees that provides
approximate O(log n) access times with the advantage that the implementation is
straightforward and the storage requirement is less than a standard balanced tree
structure.
– A distinct difference from most search structures is that the behavior of
skip lists is probabilistic without dependence on the order of insertion of elements.
SELF ORGANIZING
LIST
SELF ORGANIZING LIST
– A self-organizing list is a list that reorders its elements based on some self-organizing
heuristic to improve average access time.
– The aim of a self-organizing list is to improve efficiency of linear search by moving
more frequently accessed items towards the head of the list.
– The simplest implementation of self-organizing list is as a linked list.
– A self-organizing list achieves near constant time for element access in the best case.
– A self-organizing list uses a reorganizing algorithm to adapt to various query
distributions at runtime.
HISTORY
– The concept of self-organizing list was introduced by McCabe in 1965.
– In a pioneering work, he introduced two heuristics – the MTF rule and the
transposition rule
– Further improvements were made and algorithms suggested by Ronald
Rivest, nemes ,D Knuth and so on.
Self Organizing List Contd..
DIFFERENT TYPES OF TECHNIQUES TO
ORGANIZE LISTS
– MOVE TO FRONT(MTF)
– TRANSPOSE
– COUNT
– ORDERING
Self Organizing List Contd..
MOVE TO FRONT(MTF)
– This technique moves the element
which is accessed to the head of the list.
– This has the advantage of being easily
implemented and requiring no extra
memory.
– This heuristic also adapts quickly to
rapid changes in the query distribution.
Self Organizing List Contd..
B) TRANSPOSE
– This technique involves swapping an
accessed node with its predecessor.
– Therefore, if any node is accessed, it is
swapped with the node in front unless
it is the head node, thereby increasing
its priority.
– This algorithm is again easy to
implement and space efficient and is
more likely to keep frequently
accessed nodes at the front of the list.
Self Organizing List Contd..
C) COUNT
– In this technique, the number of times
each node was searched for is counted i.e.
every node keeps a separate counter
variable which is incremented every time it
is called.
– The nodes are then rearranged according
to decreasing count.
– Thus, the nodes of highest count i.e. most
frequently accessed are kept at the head of
the list.
Self Organizing List Contd..
ORDERING TECHNIQUE
– This technique orders a list with a particular criterion.
– For e.g in a list with nodes 2,3,7,10.
– If we want to access node 5, then the accessed node is
added between 3 and 7 to maintain the order of the
list i.e. increasing order.
Self Organizing List Contd..
DIFERENCE BETWEEN THESE
TECHNIQUES
– In the first three techniques i.e. move to front, transpose and count ,node is
added at the end whereas in the ordering technique , node is added
somewhere to maintain the order of the list.
Self Organizing List Contd..
In the first three techniques i.e. move to front, transpose and count ,node is added at the end whereas
in the ordering technique , node is added somewhere to maintain the order of the list.
APPLICATIONS
– Language translators like compilers and interpreters use self-organizing lists to
maintain symbol tables during compilation or interpretation of program source code.
– Currently research is underway to incorporate the self-organizing list data structure in
embedded systems to reduce bus transition activity which leads to power dissipation in
some circuits.
– These lists are also used in artificial intelligence and neural networks as well as self-
adjusting programs.
– The algorithms used in self-organizing lists are also used as caching algorithms as in
the case of LFU algorithm.
Self Organizing List Contd..
Sparse Tables
 Sparse table stores the information from one index ‘i’
to some index ‘j’ which is at a specific distance from ‘i’.
 Suppose our query is regarding minimum element in range
(L,R) or Range Minimum Query (RMQ) on a given array.
 Sparse Table uses dynamic programming to store the
index of minimum element in any range (i,j) .
 We can use Sparse table to store the minimum of the
elements between index ‘i’ to i+’2^j’.
0 1 2 3 4 5 6 7
3 1 5 3 4 7 6 1
Build a Sparse Table on array 3 1 5 3 4 7 6 1
0 1 2 3 4 5 6 7
3 1 5 3 4 7 6 1
1 1 3 3 4 6 1
1 1 3 3 1
1

More Related Content

What's hot

What's hot (20)

Linklist
LinklistLinklist
Linklist
 
linked list
linked list linked list
linked list
 
Priority Queue in Data Structure
Priority Queue in Data StructurePriority Queue in Data Structure
Priority Queue in Data Structure
 
Linear data structure concepts
Linear data structure conceptsLinear data structure concepts
Linear data structure concepts
 
Linked list
Linked listLinked list
Linked list
 
single linked list
single linked listsingle linked list
single linked list
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
 
Linked list
Linked listLinked list
Linked list
 
Linked List
Linked ListLinked List
Linked List
 
Queues
QueuesQueues
Queues
 
Doubly & Circular Linked Lists
Doubly & Circular Linked ListsDoubly & Circular Linked Lists
Doubly & Circular Linked Lists
 
Array data structure
Array data structureArray data structure
Array data structure
 
List Data Structure
List Data StructureList Data Structure
List Data Structure
 
Doubly Linked List
Doubly Linked ListDoubly Linked List
Doubly Linked List
 
Data Structures- Part7 linked lists
Data Structures- Part7 linked listsData Structures- Part7 linked lists
Data Structures- Part7 linked lists
 
Binary Search Tree in Data Structure
Binary Search Tree in Data StructureBinary Search Tree in Data Structure
Binary Search Tree in Data Structure
 
Stacks in DATA STRUCTURE
Stacks in DATA STRUCTUREStacks in DATA STRUCTURE
Stacks in DATA STRUCTURE
 
Singly link list
Singly link listSingly link list
Singly link list
 
Linked list
Linked listLinked list
Linked list
 
Double ended queue
Double ended queueDouble ended queue
Double ended queue
 

Similar to Linked list

Data Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdfData Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdf
rohit219406
 
in Java (ignore the last line thats hidden) Create a doubly linked l.pdf
in Java (ignore the last line thats hidden) Create a doubly linked l.pdfin Java (ignore the last line thats hidden) Create a doubly linked l.pdf
in Java (ignore the last line thats hidden) Create a doubly linked l.pdf
sauravmanwanicp
 
linkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptxlinkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptx
MeghaKulkarni27
 

Similar to Linked list (20)

Unit 5 linked list
Unit   5 linked listUnit   5 linked list
Unit 5 linked list
 
Algo>ADT list & linked list
Algo>ADT list & linked listAlgo>ADT list & linked list
Algo>ADT list & linked list
 
Data Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdfData Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdf
 
Lec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdfLec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdf
 
Array linked list.ppt
Array  linked list.pptArray  linked list.ppt
Array linked list.ppt
 
Linked list and its operations - Traversal
Linked list and its operations - TraversalLinked list and its operations - Traversal
Linked list and its operations - Traversal
 
Abstract data types
Abstract data typesAbstract data types
Abstract data types
 
linked-list.ppt
linked-list.pptlinked-list.ppt
linked-list.ppt
 
Unit - 2.pdf
Unit - 2.pdfUnit - 2.pdf
Unit - 2.pdf
 
Unit ii(dsc++)
Unit ii(dsc++)Unit ii(dsc++)
Unit ii(dsc++)
 
DS Unit 2.ppt
DS Unit 2.pptDS Unit 2.ppt
DS Unit 2.ppt
 
linkrd_list.pdf
linkrd_list.pdflinkrd_list.pdf
linkrd_list.pdf
 
Unit7 C
Unit7 CUnit7 C
Unit7 C
 
List
ListList
List
 
Adt of lists
Adt of listsAdt of lists
Adt of lists
 
Linked List
Linked ListLinked List
Linked List
 
in Java (ignore the last line thats hidden) Create a doubly linked l.pdf
in Java (ignore the last line thats hidden) Create a doubly linked l.pdfin Java (ignore the last line thats hidden) Create a doubly linked l.pdf
in Java (ignore the last line thats hidden) Create a doubly linked l.pdf
 
C Exam Help
C Exam Help C Exam Help
C Exam Help
 
Linked list.docx
Linked list.docxLinked list.docx
Linked list.docx
 
linkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptxlinkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptx
 

Recently uploaded

Recently uploaded (20)

Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 

Linked list

  • 1. Introduction What Is Linked List? (2002)  linked list is one in which all nodes are linked together  Each node contains two parts. Data contains elements .  Next/Link contains address of next node .
  • 2. Structure of singly linked list nextdata A B C nextnext datadata head  The head always points to the first node .  All nodes are connected to each other through Link fields.  Null pointer indicated end of the list. X Singly Linked List Contd..
  • 3. Benefits of linked list over array  Disadvantages of arrays : Insertion and deletion operations are slow. Slow searching in unordered array. Wastage of memory. Fixed size. More expensive  Linked lists solve some of these problems : Insertions and deletions are simple and faster. Linked List reduces the access time. No wastage of memory. Linked list is able to grow in size. Less expensive
  • 4. Linked List Types  Single Linked list  Double linked list  Circular single linked list  Circular double linked list
  • 6. Operations on linked list The basic operations on linked lists are : 1. Insertion 2. Deletion 3. Searching Singly Linked List Contd..
  • 7. 1. Insertion  Insertion operation is used to insert a new node in the linked list at the specified position.  When the list itself is empty , the new node is inserted as a first node.  Types of Insertion There are 3 ways to insert a new node into a list :  Insertion at the beginning  Insertion at the end  Insertion after a particular node Singly Linked List Contd..
  • 8. Structure of Node of Single Linked List /* Class Node */ class Node { int data; Node next; /* Constructor */ public Node() { next= null; data = 0; }
  • 9. /* Function to insert an element at begining */ public void insert() { Node newNode = new Node(); if (head == null) { newNode.next = null; head = newNode; tail = newNode; } else { newNode.next = head; head = newNode; } } } newnode Data Next Algorithm Step 1: Create a newNode with given value. Step 2: Check whether list is Empty (head == NULL) Step 3: If it is Empty then, set newNode→next = NULL and head = newNode. Step 4: If it is Not Empty then, set newNode→next = head and head = newNode.
  • 10. /* Function to insert an element at end */ public void insert() { Node newNode = new Node(); if (head == null) { head = newNode; tail = newNode; } else { tail.next = newNode; tail = newNode; } } } Singly Linked List Contd..
  • 11. 2.Deletion  Deletion operation is used to delete a particular node in the linked list.  we simply have to perform operation on the link of the nodes(which contains the address of the next node) to delete a particular element.  Types of Deletion Deletion operation may be performed in the following conditions:  Delete first element.  Delete last element.  Delete a particular element Singly Linked List Contd..
  • 12. /* Function to delete an element from first */ public void removeFirst() { if (head == null) return; else { if (head == tail) { head = null; tail = null; } else { head = head.next; } } } Singly Linked List Contd.. Step 1: Check whether list is Empty (head == NULL) Step 2: If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminate the function. Step 3: If it is Not Empty then, define a Node pointer 'temp' and initialize with head. Step 4: Check whether list is having only one node (temp → next == NULL) Step 5: If it is TRUE then set head = NULL and delete temp (Setting Empty list conditions) Step 6: If it is FALSE then set head = temp → next, and delete temp.
  • 13. Deleting the last node of a singly linked list public void removeLast() { if (tail == null) return; else { if (head == tail) { head = null; tail = null; } else { Node prev = head; while (previ.next != tail) { prev = prev.next; } tail = prev; tail.next = null; } } } Singly Linked List Contd..
  • 14. 3. Searching – Searching involves finding the required element in the list – We can use various techniques of searching like linear search or binary search where binary search is more efficient in case of Arrays – But in case of linked list since random access is not available it would become complex to do binary search in it – We can perform simple linear search traversal
  • 15.
  • 17. Circular linked list A circular linked list is one which has  No ending.  The last node of a linked list is connected with the address of its first node .  In the circular linked list we can insert elements anywhere in the list whereas in the array we cannot insert element anywhere in the list because it is in the contiguous memory.
  • 18. /* Function to insert element at begining In Circular linked list*/ public void insert(int val) { Node newNode= new Node(); if(start == null) { newNode.next=newNode head = newNode; tail = newNode; } else { head.next=head last.next = newNode; head = newNode; } size++ ; } Circular linked list contd..
  • 19. /* Function to insert element at end In Circular linked list*/ public void insert() { Node newNode= new Node(); if(start == null) { head=tail= newNode; newNode.next=newNode; } else { last= newNode; last.next=newNode newnode.next=head; } } Circular linked list contd..
  • 20. /* Function to insert element at position in circular linked list */ public void insertAtPos(int val , int pos) { Node newNode= new Node(val,null); Node ptr = start; pos = pos - 1 ; for (int i = 1; i < size - 1; i++) { if (i == pos) { Node tmp = ptr.getLink() ; ptr.setLink( newNode); newNode.setLink(tmp); break; } ptr = ptr.getLink(); } size++ ; } Circular linked list contd..
  • 21. public void deletet() { if(size==0){ System.out.println("nList is Empty"); }else{ System.out.println("ndeleting node " + head.data + " from start"); head = head.next; tail.next=head; size--; } } Circular linked list contd.. Deletion at beginning in the Circular linked list
  • 22. Function to count the total number of nodes in a linear linked list. int count(int search_for) { Node current = head; int count = 0; while (current != null) { if (current.data == search_for) count++; current = current.next; } return count; }
  • 24. Doubly Linked List 1. Doubly linked list is a linked data structure that consists of a set of sequentially linked records called nodes. 2 . Each node contains three fields :: -: one is data part which contain data only. -:two other field is links part that are point or references to the previous or to the next node in the sequence of nodes.
  • 25.  The beginning and ending nodes' previous and next links respectively, point to some kind of terminator ,typically a sentinel node or null to facilitate traversal of the list. Hence each node has knowledge of its successor and also its predecessor. In doubly linked list, from every node the list can be traversed in both the directions. Doubly Linked List (contd.) Doubly Linked List contd..
  • 26. DLL’s compared to SLL’s  Advantages:  Can be traversed in either direction (may be essential for some programs)  Some operations, such as deletion and inserting before a node, become easier  Disadvantages:  Requires more space  List manipulations are slower (because more links must be changed)  Greater chance of having bugs (because more links must be manipulated) Doubly Linked List contd..
  • 27. Let’s see how the Node structure will look like class Node { int data; Node next, prev; //constructs a new DoublyLinkedList object with head and tail as null public Node() { next = null; prev = null; data = 0; }
  • 28. Insertion at the beginning of doubly linked list Doubly Linked List contd.. public Node addAtStart(int data){ System.out.println("Adding Node " + data + " at the start"); Node newnode = new Node(data); if(head=null) { head = newnode; tail = newnode; } else { /*Make next of new node as head and previous as NULL */ newnode.next = head; head.previous = newnode; head = newnode; } size++; return n; }
  • 29. Insertion at last of doubly linked list Doubly Linked List contd.. public Node addAtEnd(int data) { System.out.println("Adding Node " + data + " at the End"); Node newNode= new Node(data); if(head = =null{ head = newNode; tail = newNode; }else{ tail.next = newNode; /*Change the next of last/tail node */ newNode.previous = tail; /*Make last node as previous of new node */ tail =newNode; } size++; return n; }
  • 30. Doubly Linked List contd.. Deletion at the beginning of doubly linked list public void delete() { if(head==null) { System.out.println("nList is Empty"); } Else if(head==tail) { head=tail=null; } else{ System.out.println("ndeleting node " + head.data + " from start"); head = head.next; size--; } }
  • 32. public void print() { Node temp = head; System.out.print("Doubly Linked List: "); while(temp!=null){ System.out.print(" " + temp.data); temp = temp.next; } System.out.println(); }
  • 34. Definition :- – Many a times a complex problem becomes easier to solve if you have the appropriate data structure at hand. More so when the input size involved is very large. – Skip Lists are an alternative representation of balanced trees that provides approximate O(log n) access times with the advantage that the implementation is straightforward and the storage requirement is less than a standard balanced tree structure. – A distinct difference from most search structures is that the behavior of skip lists is probabilistic without dependence on the order of insertion of elements.
  • 35.
  • 36.
  • 37.
  • 39. SELF ORGANIZING LIST – A self-organizing list is a list that reorders its elements based on some self-organizing heuristic to improve average access time. – The aim of a self-organizing list is to improve efficiency of linear search by moving more frequently accessed items towards the head of the list. – The simplest implementation of self-organizing list is as a linked list. – A self-organizing list achieves near constant time for element access in the best case. – A self-organizing list uses a reorganizing algorithm to adapt to various query distributions at runtime.
  • 40. HISTORY – The concept of self-organizing list was introduced by McCabe in 1965. – In a pioneering work, he introduced two heuristics – the MTF rule and the transposition rule – Further improvements were made and algorithms suggested by Ronald Rivest, nemes ,D Knuth and so on. Self Organizing List Contd..
  • 41. DIFFERENT TYPES OF TECHNIQUES TO ORGANIZE LISTS – MOVE TO FRONT(MTF) – TRANSPOSE – COUNT – ORDERING Self Organizing List Contd..
  • 42. MOVE TO FRONT(MTF) – This technique moves the element which is accessed to the head of the list. – This has the advantage of being easily implemented and requiring no extra memory. – This heuristic also adapts quickly to rapid changes in the query distribution. Self Organizing List Contd..
  • 43. B) TRANSPOSE – This technique involves swapping an accessed node with its predecessor. – Therefore, if any node is accessed, it is swapped with the node in front unless it is the head node, thereby increasing its priority. – This algorithm is again easy to implement and space efficient and is more likely to keep frequently accessed nodes at the front of the list. Self Organizing List Contd..
  • 44. C) COUNT – In this technique, the number of times each node was searched for is counted i.e. every node keeps a separate counter variable which is incremented every time it is called. – The nodes are then rearranged according to decreasing count. – Thus, the nodes of highest count i.e. most frequently accessed are kept at the head of the list. Self Organizing List Contd..
  • 45. ORDERING TECHNIQUE – This technique orders a list with a particular criterion. – For e.g in a list with nodes 2,3,7,10. – If we want to access node 5, then the accessed node is added between 3 and 7 to maintain the order of the list i.e. increasing order. Self Organizing List Contd..
  • 46. DIFERENCE BETWEEN THESE TECHNIQUES – In the first three techniques i.e. move to front, transpose and count ,node is added at the end whereas in the ordering technique , node is added somewhere to maintain the order of the list. Self Organizing List Contd..
  • 47. In the first three techniques i.e. move to front, transpose and count ,node is added at the end whereas in the ordering technique , node is added somewhere to maintain the order of the list.
  • 48. APPLICATIONS – Language translators like compilers and interpreters use self-organizing lists to maintain symbol tables during compilation or interpretation of program source code. – Currently research is underway to incorporate the self-organizing list data structure in embedded systems to reduce bus transition activity which leads to power dissipation in some circuits. – These lists are also used in artificial intelligence and neural networks as well as self- adjusting programs. – The algorithms used in self-organizing lists are also used as caching algorithms as in the case of LFU algorithm. Self Organizing List Contd..
  • 50.  Sparse table stores the information from one index ‘i’ to some index ‘j’ which is at a specific distance from ‘i’.  Suppose our query is regarding minimum element in range (L,R) or Range Minimum Query (RMQ) on a given array.  Sparse Table uses dynamic programming to store the index of minimum element in any range (i,j) .  We can use Sparse table to store the minimum of the elements between index ‘i’ to i+’2^j’.
  • 51. 0 1 2 3 4 5 6 7 3 1 5 3 4 7 6 1 Build a Sparse Table on array 3 1 5 3 4 7 6 1 0 1 2 3 4 5 6 7 3 1 5 3 4 7 6 1 1 1 3 3 4 6 1 1 1 3 3 1 1