CPCS 204 Data Structure 1 Dr Sahar Shabanah
CPCS 204: Data Structure 1 Dr. Sahar Shabanah Lecture 4: Singly Linked Lists (3. 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
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: student. List
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 count: 4 front rear
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
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. Head A B C D 6
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 Head A B C D 7
Singly Linked List �Each node stores �Element (data) �link to the next node �Empty Linked List An empty linked list is defined by: Head = Tail = Null �List with Single Node Head = Tail ≠Null next node elem Head Tail Linked Lists 8
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 get. Element(){ return element; } public Node get. Next(){ return next; } public void set. Element(Object new. Elem){element= new. Elem; } public void set. Next(Node new. Next){ next= new. Next; } }
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 Is. Empty() { return head == NULL; } public Node Insert. Node(int index, double x); public int Find. Node(double x); public int Delete. Node(double x); public void Display. List(void); }
Simply Linked List Operations �Operations of Linked. List �Is. Empty: determine whether or not the list is empty �Insert. Node: insert a new node at a particular position �Find. Node: find a node with a given value �Delete. Node: delete a node with a given value �Display. List: print all the nodes in the list
Inserting a new node �Four cases of Insert. Node: 1. Insert into an empty list: 1. 2. when list is empty (head = NULL), both head and tail must point to the new node. 2. Insert in front (at the head) 3. Insert at back ( at the tail) 4. Insert between two nodes head tail
Inserting at the Head 1. Have new node point to old head 1. Update head to point to new node head tail Φ 1. Incremen t size of list 13
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 //single node-list // Update Head to point to the new node Head = T; 14
Inserting at the Tail head tail 1. Have new node Φ point to null Φ 1. Have old last node head tail point to new node Φ 1. Update tail to point to new node head tail Φ Tail. next = new. Node; Tail= New. Node; 15
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
Inserting between two nodes 1. Have new node head curr. Node point to next of previous node 1. Have old last node tail new. Node head point to new node 2. Increment size of list tail curr. Node new. Node. next= curr. Node. next; curr. Node. next=new. Node; 17
Removing Node �Four cases occur while removing a node from a linked list: 1. When the list has only one node: 1. 2. 3. 4. dispose the node, pointed by head (or tail) sets both head and tail to NULL. head tail 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 18
Removing at the Head 1. Update head to point to next node in the list tail head Φ head 1. 2. Allow garbage collector to reclaim the former first node Decrement size of list tail Φ head tail Φ 19
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
Removing at the Tail 1. 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 tail head Φ tail head temp Φ head temp tail Φ head tail temp Φ 21
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
Removing a node between two nodes tail head 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. Φ tail head temp Φ head temp tail Φ 23
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.
- Slides: 24