1 / 24

Understanding Singly Linked Lists in Data Structures

Singly linked lists are a fundamental data structure where each element holds a pointer to the next element, forming a linear order. This text explains object references, pointers, and the implementation of singly linked lists in Java using nodes and list classes. It covers operations like insertion, deletion, and display, highlighting efficiency improvements by maintaining pointers at the head and tail. Detailed algorithms for adding nodes at the head and tail, as well as between nodes, are provided for a clear understanding of linked list operations.

nofre
Download Presentation

Understanding Singly Linked Lists in Data Structures

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. Dr. Sahar Shabanah Lecture 4: Singly Linked Lists (3.2)

  2. Object References  An object reference is a variable that stores the address of an object  A reference also can be called a pointer  References often are depicted graphically: student John Smith 40725 3.58

  3. References as Links  Object references can be used to create links between objects  Suppose a Student class contains a reference to another Student object John Smith 40725 3.57 Jane Jones 58821 3.72  References can be used to create a variety of linked structures, such as a linked list: studentList

  4. Other Dynamic Representations  It may be convenient to implement as list as a doubly linked list, with next and previous references list  It may be convenient to use a separate header node, with a count and references to both the front and rear of the list list count: 4 front rear

  5. Intermediate Nodes  The objects being stored should not be concerned with the details of the data structure in which they may be stored  For example, the Student class should not have to store a link to the next Student object in the list  Instead, we can use a separate node class with two parts: 1) a reference to an independent object and 2) a link to the next node in the list  The internal representation becomes a linked list of nodes

  6. Singly Linked List  A singly linked list is a concrete data structure consisting of a sequence of nodes, forming a linear ordering  Each element of the list contains a pointer to its successor;  The last element contains a null pointer.  A pointer to the first element of the list (called head) is used to keep track of the list. Hea d   A B C D 6

  7. Singly Linked List  The basic singly linked list is inefficient when adding elements to the end of the list (the tail ), need to locate the last element by traversing the entire list to find its tail.  To make adding elements to the tail of a list more efficient, keep a second pointer, tail , which points to the last element of the list. Tail Hea d   A B C D 7

  8. Singly Linked List  Each node stores  Element (data)  link to the next node next  Empty Linked List An empty linked list is defined by: Head = Tail = Null node elem Head Tail  List with Single Node Head = Tail ≠Null   Linked Lists 8

  9. Simply Linked List Classes  Two classes are used: Node and List  Declare Node class for the nodes  element: object  next: a link (object reference) to the next node in the list public class Node { private Object element; private Node next; //pointer to node public Node(){this (null,null);} // Creates a null node public Node(Object e, Node n){ element= e; next= n;} Public Object getElement(){ return element;} public Node getNext(){ return next; } public void setElement(Object newElem){element= newElem;} public void setNext(Node newNext){ next= newNext; } }

  10. Simply Linked List Classes  Declare List, which contains  head: a pointer to the first node in the list. Since the list is empty initially, head is set to NULL  Operations on List public class List { private Node head; public void List(){ head = NULL; }//constructor public bool IsEmpty() { return head == NULL; } public Node InsertNode(int index, double x); public int FindNode(double x); public int DeleteNode(double x); public void DisplayList(void); }

  11. Simply Linked List Operations Operations of LinkedList IsEmpty: determine whether or not the list is empty InsertNode: insert a new node at a particular position FindNode: find a node with a given value DeleteNode: delete a node with a given value DisplayList: print all the nodes in the list

  12. Inserting a new node  Four cases of InsertNode: Insert into an empty list: when list is empty (head = NULL), both head and tail must point to the new node. head tail 1. 1.   2. 2. Insert in front (at the head) 3. Insert at back ( at the tail) 4. Insert between two nodes

  13. Inserting at the Head Have new node point to old head 1. tail head Φ Update head to point to new node 1. tail head Φ Increment size of list 1. 13

  14. Algorithm for Inserting at Head //Create a new Node new (T); // store element data T.data = A; //new node points to old Head node T.next = Head; If (Head = Null) then Tail = T // Update Head to point to the new node Head = T; //single node-list 14

  15. Inserting at the Tail tail head 1. Have new node point to null Φ Φ tail head 1. Have old last node point to new node Φ head tail 1. Update tail to point to new node Φ Tail.next = newNode; Tail= NewNode; 15

  16. Algorithm for Inserting at Tail //Create a new Node new (T); // store element data T.data = A; //new node points to Null T.next = Null; If (Head = Null) then Head = T//have a single node-list else Tail.next = T; // Tail to point to new node Tail= T; Linked Lists 16

  17. Inserting between two nodes head tail 1. Have new node point to next of previous node currNode newNode 1. Have old last node point to new node 2. Increment size of list head tail currNode newNode newNode.next= currNode.next; currNode.next=newNode; 17

  18. Removing Node  Four cases occur while removing a node from a linked list: When the list has only one node: dispose the node, pointed by head (or tail) sets both head and tail to NULL. Remove first: first node (current head node) is removed from the list. Remove last: last node (current tail node) is removed from the list. General case: node to be removed is located between two nodes. Head and tail links are not updated head tail 1. 1.   2. 2. 3. 4. 18

  19. Removing at the Head tail head Update head to point to next node in the list 1. Φ head tail Allow garbage collector to reclaim the former first node Decrement size of list 1. Φ tail head Φ 2. 19

  20. Algorithm for Removing at the Head If (Head = Null) then Output “error” //empty list else{ T = Head;// temporary pointer y = T.data; //save elem data Head = T.next; //update Head If (Head = Null) then Tail = Null; //single node Delete(T); //Garbage collection } //End of Algorithm  Notice: Any Removal Algorithm consists of a single statement Linked Lists 20

  21. Removing at the Tail tail head Traverse the list to find the previous node of the tail 2. Set the tail to point to the previous node 3. Set next of previous node to NULL 1. Φ temp tail head Φ temp tail head Φ temp tail head Φ tail temp head Φ 21

  22. Algorithm for Removing at the Tail Must cycle through the list, starting at the Head, to determine the new Tail-node. If (Head = Null) then Output “error” //empty list else { T = Head; // temporary pointer If (Head=Tail) then {Head=Null; T=Null}// test for one node else { while T.next≠Tail do T=T.next; T.next = Null } y = Tail.data; //save elem data Delete(Tail); //Garbage collection Tail = T; //update Tail } //End of Algorithm Linked Lists 22

  23. Removing a node between two nodes head tail Traverse the list to find the previous node for deleted node 2. Set the previous node to point to the next of the deleted node 3. Remove deleted node 1. Φ temp tail head Φ temp tail head Φ tail head temp Φ tail head temp Φ 23

  24. Array versus Linked Lists  Linked lists are more complex to code and manage than arrays, but they have some distinct advantages.  Dynamic: a linked list can easily grow and shrink in size.  We don’t need to know how many nodes will be in the list. They are created in memory as needed.  In contrast, the size of the array is fixed at compilation time.  Easy and fast insertions and deletions  To insert or delete an element in an array, we need to copy to temporary variables to make room for new elements or close the gap caused by deleted elements.  With a linked list, no need to move other nodes. Only need to reset some pointers.

More Related