Algorithms and Data Structures
Linked Lists
By- Priyanka
Linked Lists
LIST
Why Linked list?
Student 4
Student 3
Student 2
Student 1
Student 6
Student 5
Student 4
Student 3
Student 2
Student 1
ARRALinked List
Linked Lists Vs Arrays
In linked list:
 Elements can be added or removed from the
middle of the list.
 No need to define an initial size and it utilizes
memory space efficiently.
Linked List
Item of the
list
Link to the next
node
Linked List Operations
 Traversing
 Addition of node
 Add first
 Add last
 Add in between
 Removal of node
Traversing
5 1
3
2
HEA
D
NUL
L
tem
p
Node temp = head;
while(temp != null)
temp = temp.next;
tem
p
Addition of node (Add first)
5 1
3
2
New
HEA
D
NUL
L
public void addFirst(SinglyLinkedListNode newNode) {
            newNode.next = head;
            head = newNode;
 }
 
Addition of node (Add last)
5 1
3
2
New
HEA
D
NUL
L
public void addLast(SinglyLinkListNode newNode)
{
tail.next = newNode;
tail = newNode;
}
Addition of node (In between)
5 1
3
2
New
HEA
D
NUL
L
public void insertAfter(SinglyLinkedListNode previous,
SinglyLinkedListNode newNode) {
●
SinglyLinkedListNode next = previous.next;
●
previous.next = newNode;
●
newNode.next = next;
}
Removal of Node
5 1
3
2
HEA
D
NUL
L
Remove
13
Doubly-Linked List Node
Prev Objec
t
Item
Next
public class Node{
private Object item;
private Node next;
private Node previous;
//constructors and methods
}
Nod
e
Doubly Linked List
Sentinel Nodes : Do not store any
elements
Double linked list - Insertion Algorithm
Algorithm
Algorithm addAfter(p,x):
Create a new node q
q.setElement(x)
q.setPrev(p) {link q to its predecessor}
q.setNext(p.getNext()) {link q to its successor}
(p.getNext()).setPrev(q) {link p’s old successor to q}
p.setNext(q) {link p to its new successor q}
return q
Double linked list – Removal Algorithm
Algorithm
Algorithm remove(p):
t = p.element {temporary variable to hold the return value}
(p.getPrev()).setNext(p.getNext()) {linking out
p}
(p.getNext()).setPrev(p.getPrev())
p.setPrev(null) {invalidating the position p}
p.setNext(null)
return t
Circular linked list
It allows searches for sequences of items to continue
from a “current position”, rather than always starting at
the head of the list.
Summary
Lists can be implemented:
Statically using arrays
Dynamically using nodes
Linked Lists are useful:
Maintaining sorted sequences of values.
Representing sparse data.
Building other more complex data structures.
Thank You

Linked list

  • 1.
    Algorithms and DataStructures Linked Lists By- Priyanka
  • 2.
  • 3.
    Why Linked list? Student4 Student 3 Student 2 Student 1 Student 6 Student 5 Student 4 Student 3 Student 2 Student 1 ARRALinked List
  • 4.
    Linked Lists VsArrays In linked list:  Elements can be added or removed from the middle of the list.  No need to define an initial size and it utilizes memory space efficiently.
  • 5.
    Linked List Item ofthe list Link to the next node
  • 6.
    Linked List Operations Traversing  Addition of node  Add first  Add last  Add in between  Removal of node
  • 7.
    Traversing 5 1 3 2 HEA D NUL L tem p Node temp= head; while(temp != null) temp = temp.next; tem p
  • 8.
    Addition of node(Add first) 5 1 3 2 New HEA D NUL L public void addFirst(SinglyLinkedListNode newNode) {             newNode.next = head;             head = newNode;  }  
  • 9.
    Addition of node(Add last) 5 1 3 2 New HEA D NUL L public void addLast(SinglyLinkListNode newNode) { tail.next = newNode; tail = newNode; }
  • 10.
    Addition of node(In between) 5 1 3 2 New HEA D NUL L public void insertAfter(SinglyLinkedListNode previous, SinglyLinkedListNode newNode) { ● SinglyLinkedListNode next = previous.next; ● previous.next = newNode; ● newNode.next = next; }
  • 11.
    Removal of Node 51 3 2 HEA D NUL L Remove 13
  • 12.
    Doubly-Linked List Node PrevObjec t Item Next public class Node{ private Object item; private Node next; private Node previous; //constructors and methods } Nod e
  • 13.
    Doubly Linked List SentinelNodes : Do not store any elements
  • 14.
    Double linked list- Insertion Algorithm
  • 15.
    Algorithm Algorithm addAfter(p,x): Create anew node q q.setElement(x) q.setPrev(p) {link q to its predecessor} q.setNext(p.getNext()) {link q to its successor} (p.getNext()).setPrev(q) {link p’s old successor to q} p.setNext(q) {link p to its new successor q} return q
  • 16.
    Double linked list– Removal Algorithm
  • 17.
    Algorithm Algorithm remove(p): t =p.element {temporary variable to hold the return value} (p.getPrev()).setNext(p.getNext()) {linking out p} (p.getNext()).setPrev(p.getPrev()) p.setPrev(null) {invalidating the position p} p.setNext(null) return t
  • 18.
    Circular linked list Itallows searches for sequences of items to continue from a “current position”, rather than always starting at the head of the list.
  • 19.
    Summary Lists can beimplemented: Statically using arrays Dynamically using nodes Linked Lists are useful: Maintaining sorted sequences of values. Representing sparse data. Building other more complex data structures.
  • 20.

Editor's Notes

  • #2 <number>
  • #13 <number>