Disclaimer: This presentation is prepared by trainees of
baabtra as a part of mentoring program. This is not official
document of baabtra –Mentoring Partner
Baabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt .
Ltd
Linked List
Abhishek H Menon
abhishekmenon@gmail.com
www.facebook.com/abhishe
kmenon5
twitter.com/abhishekmenon
121989
in.linkedin.com/in/Abhishek
H Menon
9496519895
Linked List
• Consists of
– Sequence of nodes.
Each node contains a value and a link to some other
node.(pointer or reference)
The last node will be having a link which points to
null.
The list may or may not have a header.
Linked Lists
a b c d
Structure of a Linked List
List(Header)
Linked List
• Successor :successor of a node is the node
pointing next to that node.(suc(a)=b).
• Predecessor: predecessor of a node is the
node pointing this node.(pre(b)=a).
• The length of the linked list is the number of
nodes in it.
Linked Lists
• In programming languages like C,CPP we are
using “pointers ” to implement linked lists.
• But in java programming, we use references
instead of pointers.
• To some extent both are same , but the
difference is that C and CPP allows to modify
pointers in anyways, and can point to
anything.
Creating references
• Person p=new Person(“John”);
the keyword ‘new’ creates the object and
returns a references.
here new Person(“John”) creates the object
and returns a references to it.
We can assign this reference to P.
Creating links
A B C D
Class node
{
node next;
char value;
Node(char value, node n) //constructor
{
value=value;
next=n;
}
}
Creating Links
• Node temp=new node(D, null);
• temp=new node(C,temp);
• temp=new node(B,temp);
• temp=new node(A,temp);
• Temp=new node(null,temp);
Single linked list
• Each node will have a value and a link to the
next value(successor).
• There will be a header which points either to
the first element of the list, or if the list is
empty it will point to the null link.
Singly Linked Lists(SLL)
A B C D
Traversing a SLL
• The following method traverses a list and print
its elements.
public void printFirstToLast(Node here) {
while (here != null) {
System.out.print(here.value + " ");
here = here.next;
}
}
You would write this as an instance method of the Node class.
Singly Linked Lists(SLL)
A B C D
header
here
Inserting an element into a list
• There are many ways to insert an element into
a singly linked list.
– As the new first element
– As the new last element
– Before a given node
– After a given node
– Before a given value
– After a given value
Inserting as a new first element
• The easiest method to implement
• In class Node
Node insertAtFront(Node oldFront, Object value) {
Node newNode = new Node(value, oldFront);
return newNode;
}
Use this as: myList = insertAtFront(myList, value);
Insert node after a given value
void insertAfter(Object target, Object value) {
for (Node here = this; here != null; here = here.next) {
if (here.value.equals(target)) {
Node node = new Node(value, here.next);
here.next = node;
return;
}
}
// Couldn't insert--do something reasonable here!
}
Deleting a node from a SLL
• In order to delete a node from a SLL, you have
to change the link in its predecessor
• This is slightly tricky, because you can’t follow
a pointer backwards
• Deleting the first node in a list is a special
case, because the node’s predecessor is the
list header
Deleting a node from a SLL
A B C
header
To delete the first element, change the link in the header.
Deleting an element from a SLL
• To delete some other element, change the link
in its predecessor.
A B C
predecessor
Doubly linked lists
Doubly Linked List
Each node contain a pointer to the successor and a pointer to its
predecessor.
Header points to the first element in the list and last element
A B C
Advantages and disadvantages of DLL
over SLL
• Advantages
– Can be traversed in either direction.
– Such operations such as insertion deletion
becomes much easier when compared to SLL.
• Disadvantages
 Requires more space.
 List manipulations are slower because more links are
changed
 Greater chance of having bugs.
If this presentation helped you, please visit our
page facebook.com/baabtra and like it.
Thanks in advance.
www.baabtra.com | www.massbaab.com |www.baabte.com
Contact Us
Emerald Mall (Big Bazar Building)
Mavoor Road, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550
NC Complex, Near Bus Stand
Mukkam, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550
Start up Village
Ernakulam,
Kerala, India.
Email: info@baabtra.com

Linked list

  • 2.
    Disclaimer: This presentationis prepared by trainees of baabtra as a part of mentoring program. This is not official document of baabtra –Mentoring Partner Baabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt . Ltd
  • 3.
    Linked List Abhishek HMenon abhishekmenon@gmail.com www.facebook.com/abhishe kmenon5 twitter.com/abhishekmenon 121989 in.linkedin.com/in/Abhishek H Menon 9496519895
  • 4.
    Linked List • Consistsof – Sequence of nodes. Each node contains a value and a link to some other node.(pointer or reference) The last node will be having a link which points to null. The list may or may not have a header.
  • 5.
    Linked Lists a bc d Structure of a Linked List List(Header)
  • 6.
    Linked List • Successor:successor of a node is the node pointing next to that node.(suc(a)=b). • Predecessor: predecessor of a node is the node pointing this node.(pre(b)=a). • The length of the linked list is the number of nodes in it.
  • 7.
    Linked Lists • Inprogramming languages like C,CPP we are using “pointers ” to implement linked lists. • But in java programming, we use references instead of pointers. • To some extent both are same , but the difference is that C and CPP allows to modify pointers in anyways, and can point to anything.
  • 8.
    Creating references • Personp=new Person(“John”); the keyword ‘new’ creates the object and returns a references. here new Person(“John”) creates the object and returns a references to it. We can assign this reference to P.
  • 9.
    Creating links A BC D Class node { node next; char value; Node(char value, node n) //constructor { value=value; next=n; } }
  • 10.
    Creating Links • Nodetemp=new node(D, null); • temp=new node(C,temp); • temp=new node(B,temp); • temp=new node(A,temp); • Temp=new node(null,temp);
  • 11.
    Single linked list •Each node will have a value and a link to the next value(successor). • There will be a header which points either to the first element of the list, or if the list is empty it will point to the null link.
  • 12.
  • 13.
    Traversing a SLL •The following method traverses a list and print its elements. public void printFirstToLast(Node here) { while (here != null) { System.out.print(here.value + " "); here = here.next; } } You would write this as an instance method of the Node class.
  • 14.
    Singly Linked Lists(SLL) AB C D header here
  • 15.
    Inserting an elementinto a list • There are many ways to insert an element into a singly linked list. – As the new first element – As the new last element – Before a given node – After a given node – Before a given value – After a given value
  • 16.
    Inserting as anew first element • The easiest method to implement • In class Node Node insertAtFront(Node oldFront, Object value) { Node newNode = new Node(value, oldFront); return newNode; } Use this as: myList = insertAtFront(myList, value);
  • 17.
    Insert node aftera given value void insertAfter(Object target, Object value) { for (Node here = this; here != null; here = here.next) { if (here.value.equals(target)) { Node node = new Node(value, here.next); here.next = node; return; } } // Couldn't insert--do something reasonable here! }
  • 18.
    Deleting a nodefrom a SLL • In order to delete a node from a SLL, you have to change the link in its predecessor • This is slightly tricky, because you can’t follow a pointer backwards • Deleting the first node in a list is a special case, because the node’s predecessor is the list header
  • 19.
    Deleting a nodefrom a SLL A B C header To delete the first element, change the link in the header.
  • 20.
    Deleting an elementfrom a SLL • To delete some other element, change the link in its predecessor. A B C predecessor
  • 21.
    Doubly linked lists DoublyLinked List Each node contain a pointer to the successor and a pointer to its predecessor. Header points to the first element in the list and last element A B C
  • 22.
    Advantages and disadvantagesof DLL over SLL • Advantages – Can be traversed in either direction. – Such operations such as insertion deletion becomes much easier when compared to SLL. • Disadvantages  Requires more space.  List manipulations are slower because more links are changed  Greater chance of having bugs.
  • 23.
    If this presentationhelped you, please visit our page facebook.com/baabtra and like it. Thanks in advance. www.baabtra.com | www.massbaab.com |www.baabte.com
  • 24.
    Contact Us Emerald Mall(Big Bazar Building) Mavoor Road, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 NC Complex, Near Bus Stand Mukkam, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 Start up Village Ernakulam, Kerala, India. Email: info@baabtra.com