SlideShare a Scribd company logo
LINKED LISTS
LINKED LISTS
data next
A singly linked list is a concrete data
structure consisting of a sequence of
nodes.
Each node stores:-
element
link to the next node
struct Node
{ int item
Node *next;
};
Inserting at the Head
1. Allocate a new
node
2. Insert new
element
3. Make new node
point to old
head
4. Update head to
point to new
node
void insertLL(Struct Node
*newNode)
{
if (newNode == NULL)
return;
else {
if (head == NULL) {
newNode->next = NULL;
head = newNode;
tail = newNode;
}
else {
newNode->next = head;
head = newNode;
}
}
}
Inserting at the Head
Update the next link of a new node, to
point to the current head node.
Update head link to point to the new
node.
Removing at the Head
void removeFirst()
{
if (head == NULL)
return;
else {
Struct Node *removedNode;
removedNode = head;
if (head == tail) {
head = NULL;
tail = NULL;
} else {
head = head->next;
}
delete removedNode;
}
}
1. Update head to
point to next node
in the list
2. Allow garbage
collector to reclaim
the former first
node
Update head link to point to the node,
next to the head.
Dispose removed node.
LINKED LISTS
void insert(Struct Node*newNode)
{
if (newNode == NULL)
return;
else {
newNode->next = NULL;
if (head == NULL) {
head = newNode;
tail = newNode;
} else {
tail->next = newNode;
tail = newNode;
}
}
}
1. Allocate a new
node
2. Insert new element
3. Have new node
point to null
4. Have old last node
point to new node
5. Update tail to point
to new node
Inserting at the Tail
Update the next link of the current tail
node, to point to the new node.
Update tail link to point to the new
node
Insertion between two nodes
New node is always inserted between two nodes, which
are already in the list. Head and tail links are not updated
in this case.
Update link of the "previous" node,
to point to the new node.
Update link of the new node, to
point to the "next" node.
 It is a way of going both directions in a
linked list, forward and reverse.
 Many applications require a quick access
to the predecessor node of some node in
list.
Doubly-Linked Lists
prevnext
Algorithm addFirst(v):
w = header.getNext() // the
current first node
v.setNext(w)
w.setPrev(v)
header.setNext(v)
v.setPrev(header)
size++
Algorithm removeLast():
v = trailer.getPrev() // the
current last node
if (v = = header) then
Indicate an error: the list
is empty
prev = v.getPrev()
prev.setNext(trailer)
trailer.setPrev(prev)
v.setPrev(null)
v.setNext(null)
size--
•If programming in C, there are no “grow able-arrays”, so
typically linked lists used when# elements in a collection
varies, isn’t known, can’t be fixed at compile time
Could grow array, potentially expensive/wasteful especially if #
elements is small.
Also need# elements in array, requires extra parameter
With linked list, one pointer used to access all the elements in a
collection
•Simulation/modelling of DNA gene-splicing
Given list of millions of CGTA... for DNA strand, find locations where
new DNA/gene can be spliced
•Remove element from middle of a collection,
maintain order, no shifting. Add an element in the
middle, no shifting
What’s the problem with a vector (array)?
Emacs visits several files, internally keeps a linked-list of
buffers
Naively keep characters in a linked list, but in practice too much storage,
 need more esoteric data structures
•What’s (3x5+ 2x3+ x + 5) + (2x4+ 5x3+ x2+4x) ?
As a vector (3, 0, 2, 0, 1, 5)and (0, 2, 5, 1, 4, 0)
As a list ((3,5), (2,3), (1,1), (5,0))and ________?
Most polynomial operations sequentially visit terms, don’t need random
access, do need “splicing”

More Related Content

PPTX
Lecture 6: linked list
PPTX
Stack and Queue
PPTX
Threaded Binary Tree.pptx
PDF
Linked list implementation of Queue
PPTX
Stacks in DATA STRUCTURE
PDF
Singly linked list
PPTX
PPTX
Doubly Linked List
Lecture 6: linked list
Stack and Queue
Threaded Binary Tree.pptx
Linked list implementation of Queue
Stacks in DATA STRUCTURE
Singly linked list
Doubly Linked List

What's hot (20)

PPT
Doubly linked list
PPTX
Linked list
PDF
linked lists in data structures
PPT
Queue in Data Structure
PPTX
Linked List - Insertion & Deletion
PPTX
linked list in Data Structure, Simple and Easy Tutorial
PPTX
Tree_Definition.pptx
PPT
Linked list
PPTX
Linked list
PPT
Heap sort
PPTX
Queue - Data Structure - Notes
PPTX
Stack and queue
PPTX
Stacks IN DATA STRUCTURES
PPTX
Stack - Data Structure - Notes
PPTX
PPTX
Circular linked list
PPTX
Queue in Data Structure
PPTX
Linked list
PPTX
queue & its applications
PPTX
Binary Tree in Data Structure
Doubly linked list
Linked list
linked lists in data structures
Queue in Data Structure
Linked List - Insertion & Deletion
linked list in Data Structure, Simple and Easy Tutorial
Tree_Definition.pptx
Linked list
Linked list
Heap sort
Queue - Data Structure - Notes
Stack and queue
Stacks IN DATA STRUCTURES
Stack - Data Structure - Notes
Circular linked list
Queue in Data Structure
Linked list
queue & its applications
Binary Tree in Data Structure
Ad

Viewers also liked (6)

PPTX
Car Parking System (Singly linked list )
PPTX
Singly linked lists
PPT
Data Structure Lecture 6
PPTX
Linked list without animation
PPTX
linked list
Car Parking System (Singly linked list )
Singly linked lists
Data Structure Lecture 6
Linked list without animation
linked list
Ad

Similar to LINKED LISTS (20)

PPT
Linkedlist
PPT
17 linkedlist (1)
PPTX
Singly Linked List_Operations-Traversal.pptx
PDF
in Java (ignore the last line thats hidden) Create a doubly linked l.pdf
DOCX
Data Structure Suppose a singly linked list is implemented with refere.docx
PDF
Lab-2.4 101.pdf
PDF
LinkedList1LinkedList1LinkedList1111.pdf
PPTX
Data Structures and Agorithm: DS 04 Linked List.pptx
PPTX
Linked list
PPTX
Data Structures and Agorithm: DS 05 Doubly Linked List.pptx
PPT
Data structures cs301 power point slides lecture 03
PDF
C++Write a method Node Nodereverse() which reverses a list..pdf
PPTX
Data Structures - Lecture 9 [Stack & Queue using Linked List]
PPT
Linked list
DOCX
The MyLinkedList class used in Listing 24.6 is a one-way directional .docx
PPTX
Implemention of Linked list concept in Data Structures
PPT
dynamicList.ppt
PDF
This assignment and the next (#5) involve design and development of a.pdf
PPTX
CSE240 Doubly Linked Lists
PDF
I need to implment a function that can reverse a single linked list..pdf
Linkedlist
17 linkedlist (1)
Singly Linked List_Operations-Traversal.pptx
in Java (ignore the last line thats hidden) Create a doubly linked l.pdf
Data Structure Suppose a singly linked list is implemented with refere.docx
Lab-2.4 101.pdf
LinkedList1LinkedList1LinkedList1111.pdf
Data Structures and Agorithm: DS 04 Linked List.pptx
Linked list
Data Structures and Agorithm: DS 05 Doubly Linked List.pptx
Data structures cs301 power point slides lecture 03
C++Write a method Node Nodereverse() which reverses a list..pdf
Data Structures - Lecture 9 [Stack & Queue using Linked List]
Linked list
The MyLinkedList class used in Listing 24.6 is a one-way directional .docx
Implemention of Linked list concept in Data Structures
dynamicList.ppt
This assignment and the next (#5) involve design and development of a.pdf
CSE240 Doubly Linked Lists
I need to implment a function that can reverse a single linked list..pdf

Recently uploaded (20)

PPTX
Internet of Things (IOT) - A guide to understanding
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPTX
Lecture Notes Electrical Wiring System Components
PDF
Digital Logic Computer Design lecture notes
PPTX
Construction Project Organization Group 2.pptx
PPT
Project quality management in manufacturing
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
DOCX
573137875-Attendance-Management-System-original
PPTX
additive manufacturing of ss316l using mig welding
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PDF
Well-logging-methods_new................
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
CH1 Production IntroductoryConcepts.pptx
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
Sustainable Sites - Green Building Construction
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
Internet of Things (IOT) - A guide to understanding
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Foundation to blockchain - A guide to Blockchain Tech
Lecture Notes Electrical Wiring System Components
Digital Logic Computer Design lecture notes
Construction Project Organization Group 2.pptx
Project quality management in manufacturing
Operating System & Kernel Study Guide-1 - converted.pdf
573137875-Attendance-Management-System-original
additive manufacturing of ss316l using mig welding
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
Well-logging-methods_new................
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
CH1 Production IntroductoryConcepts.pptx
Embodied AI: Ushering in the Next Era of Intelligent Systems
Sustainable Sites - Green Building Construction
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf

LINKED LISTS

  • 3. data next A singly linked list is a concrete data structure consisting of a sequence of nodes. Each node stores:- element link to the next node struct Node { int item Node *next; };
  • 4. Inserting at the Head 1. Allocate a new node 2. Insert new element 3. Make new node point to old head 4. Update head to point to new node void insertLL(Struct Node *newNode) { if (newNode == NULL) return; else { if (head == NULL) { newNode->next = NULL; head = newNode; tail = newNode; } else { newNode->next = head; head = newNode; } } }
  • 6. Update the next link of a new node, to point to the current head node.
  • 7. Update head link to point to the new node.
  • 8. Removing at the Head void removeFirst() { if (head == NULL) return; else { Struct Node *removedNode; removedNode = head; if (head == tail) { head = NULL; tail = NULL; } else { head = head->next; } delete removedNode; } } 1. Update head to point to next node in the list 2. Allow garbage collector to reclaim the former first node
  • 9. Update head link to point to the node, next to the head.
  • 12. void insert(Struct Node*newNode) { if (newNode == NULL) return; else { newNode->next = NULL; if (head == NULL) { head = newNode; tail = newNode; } else { tail->next = newNode; tail = newNode; } } } 1. Allocate a new node 2. Insert new element 3. Have new node point to null 4. Have old last node point to new node 5. Update tail to point to new node Inserting at the Tail
  • 13. Update the next link of the current tail node, to point to the new node.
  • 14. Update tail link to point to the new node
  • 15. Insertion between two nodes New node is always inserted between two nodes, which are already in the list. Head and tail links are not updated in this case.
  • 16. Update link of the "previous" node, to point to the new node.
  • 17. Update link of the new node, to point to the "next" node.
  • 18.  It is a way of going both directions in a linked list, forward and reverse.  Many applications require a quick access to the predecessor node of some node in list. Doubly-Linked Lists prevnext
  • 19. Algorithm addFirst(v): w = header.getNext() // the current first node v.setNext(w) w.setPrev(v) header.setNext(v) v.setPrev(header) size++
  • 20. Algorithm removeLast(): v = trailer.getPrev() // the current last node if (v = = header) then Indicate an error: the list is empty prev = v.getPrev() prev.setNext(trailer) trailer.setPrev(prev) v.setPrev(null) v.setNext(null) size--
  • 21. •If programming in C, there are no “grow able-arrays”, so typically linked lists used when# elements in a collection varies, isn’t known, can’t be fixed at compile time Could grow array, potentially expensive/wasteful especially if # elements is small. Also need# elements in array, requires extra parameter With linked list, one pointer used to access all the elements in a collection •Simulation/modelling of DNA gene-splicing Given list of millions of CGTA... for DNA strand, find locations where new DNA/gene can be spliced
  • 22. •Remove element from middle of a collection, maintain order, no shifting. Add an element in the middle, no shifting What’s the problem with a vector (array)? Emacs visits several files, internally keeps a linked-list of buffers Naively keep characters in a linked list, but in practice too much storage,  need more esoteric data structures •What’s (3x5+ 2x3+ x + 5) + (2x4+ 5x3+ x2+4x) ? As a vector (3, 0, 2, 0, 1, 5)and (0, 2, 5, 1, 4, 0) As a list ((3,5), (2,3), (1,1), (5,0))and ________? Most polynomial operations sequentially visit terms, don’t need random access, do need “splicing”