1 / 30

Singly Linked Lists

Singly Linked Lists. - Ed. 2, 3: Chapter 4 - Ed. 4.: Chapter 3. tail. pointer to a next node. node. pointer to an element. Illustration of a linked list in memory:. pointer to a next node. node. pointer to an element. pointer to a next node. node. pointer to an element.

Download Presentation

Singly Linked Lists

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Singly Linked Lists - Ed. 2, 3: Chapter 4- Ed. 4.: Chapter 3

  2. tail

  3. pointer to a next node node pointer to an element Illustration of a linked list in memory:

  4. pointer to a next node node pointer to an element

  5. pointer to a next node node pointer to an element

  6. pointer to a next node node pointer to an element

  7. Singly Linked Lists and Arrays

  8. Class Node Here is an implementation of nodes in Java: public class Node { private Object element; private Node next; public Node() { this( null, null ); } public Node( Object e, Node n ) { element = e; next = n; }

  9. Object getElement() { return element } Node getNext() { return next; } void setElement( Object newElem ) { element = newElem; } void setNext( Node newNext ) { next = newNext; } }

  10. Insertion of an Element at the Head

  11. Node x = new Node(); x.setElement(new String(“Baltimore”)); The following statement is not correct: x.element = new String(“Baltimore”));

  12. x.setNext(head); head = x;

  13. Deleting an Element at the Head

  14. head = head.getNext();

  15. Insertion of an Element at the Tail

  16. Node x = new Node( ); x.setElement(new String(“Baltimore”)); x.setNext(null); tail.setNext(x); tail = x;

  17. How to keep “head” and “tail”? public class Head_and_Tail { Node head; Node tail; Head_and_Tail(Node x, Node y) { head = x; tail = y; } }

  18. How to keep “head” and “tail”? public class GeneratingList { Node head = null; Node tail = null; Public Head_and_Tail linked_list () { Node x = null; for (int i = 0; i < 10; i++) {x = new Node(); x.setElement(new Integer(i)); if (i == 0 ) {x.setNext(null); tail = x;} else x.setNext(head); head = x; } return new Head_and_Tail(head, tail);} }

  19. Deleting an Element at the Tail

  20. should be removed

  21. How to insert a new node in the middle of a singly linked list? How to remove a node which in the middle of a singly linked list?

  22. … …  0 1 9 Data Structure Exercises 5.1 Write a Java program to create a linked list as shown below. head tail

More Related