Linked list

372 views
278 views

Published on

Published in: Education, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
372
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
19
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Linked list

  1. 1. Disclaimer: This presentation is prepared by trainees ofbaabtra as a part of mentoring program. This is not officialdocument of baabtra –Mentoring PartnerBaabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt .Ltd
  2. 2. Linked ListAbhishek H Menonabhishekmenon@gmail.comwww.facebook.com/abhishekmenon5twitter.com/abhishekmenon121989in.linkedin.com/in/AbhishekH Menon9496519895
  3. 3. Linked List• Consists of– Sequence of nodes.Each node contains a value and a link to some othernode.(pointer or reference)The last node will be having a link which points tonull.The list may or may not have a header.
  4. 4. Linked Listsa b c dStructure of a Linked ListList(Header)
  5. 5. Linked List• Successor :successor of a node is the nodepointing next to that node.(suc(a)=b).• Predecessor: predecessor of a node is thenode pointing this node.(pre(b)=a).• The length of the linked list is the number ofnodes in it.
  6. 6. Linked Lists• In programming languages like C,CPP we areusing “pointers ” to implement linked lists.• But in java programming, we use referencesinstead of pointers.• To some extent both are same , but thedifference is that C and CPP allows to modifypointers in anyways, and can point toanything.
  7. 7. Creating references• Person p=new Person(“John”);the keyword ‘new’ creates the object andreturns a references.here new Person(“John”) creates the objectand returns a references to it.We can assign this reference to P.
  8. 8. Creating linksA B C DClass node{node next;char value;Node(char value, node n) //constructor{value=value;next=n;}}
  9. 9. 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);
  10. 10. Single linked list• Each node will have a value and a link to thenext value(successor).• There will be a header which points either tothe first element of the list, or if the list isempty it will point to the null link.
  11. 11. Singly Linked Lists(SLL)A B C D
  12. 12. Traversing a SLL• The following method traverses a list and printits 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.
  13. 13. Singly Linked Lists(SLL)A B C Dheaderhere
  14. 14. Inserting an element into a list• There are many ways to insert an element intoa 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
  15. 15. Inserting as a new first element• The easiest method to implement• In class NodeNode insertAtFront(Node oldFront, Object value) {Node newNode = new Node(value, oldFront);return newNode;}Use this as: myList = insertAtFront(myList, value);
  16. 16. Insert node after a given valuevoid 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;}}// Couldnt insert--do something reasonable here!}
  17. 17. Deleting a node from a SLL• In order to delete a node from a SLL, you haveto change the link in its predecessor• This is slightly tricky, because you can’t followa pointer backwards• Deleting the first node in a list is a specialcase, because the node’s predecessor is thelist header
  18. 18. Deleting a node from a SLLA B CheaderTo delete the first element, change the link in the header.
  19. 19. Deleting an element from a SLL• To delete some other element, change the linkin its predecessor.A B Cpredecessor
  20. 20. Doubly linked listsDoubly Linked ListEach node contain a pointer to the successor and a pointer to itspredecessor.Header points to the first element in the list and last elementA B C
  21. 21. Advantages and disadvantages of DLLover SLL• Advantages– Can be traversed in either direction.– Such operations such as insertion deletionbecomes much easier when compared to SLL.• Disadvantages Requires more space. List manipulations are slower because more links arechanged Greater chance of having bugs.
  22. 22. If this presentation helped you, please visit ourpage facebook.com/baabtra and like it.Thanks in advance.www.baabtra.com | www.massbaab.com |www.baabte.com
  23. 23. Contact UsEmerald Mall (Big Bazar Building)Mavoor Road, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550NC Complex, Near Bus StandMukkam, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550Start up VillageErnakulam,Kerala, India.Email: info@baabtra.com

×