Philosophy of Education and Educational Philosophy
Linked list
2. 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
3. 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
4. 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.
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
• 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.
8. 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.
9. Creating links
A B C D
Class node
{
node next;
char value;
Node(char value, node n) //constructor
{
value=value;
next=n;
}
}
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.
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.
15. 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
16. 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);
17. 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!
}
18. 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
19. Deleting a node from a SLL
A B C
header
To delete the first element, change the link in the header.
20. Deleting an element from a SLL
• To delete some other element, change the link
in its predecessor.
A B C
predecessor
21. 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
22. 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.
23. 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
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