240 likes | 340 Views
CPCS204: Data Structure 1. Dr. Sahar Shabanah Lecture 4: Singly Linked Lists (3.2). student. John Smith 40725 3.58. Object References. An object reference is a variable that stores the address of an object A reference also can be called a pointer
E N D
CPCS204: Data Structure 1 Dr. Sahar Shabanah Lecture 4: Singly Linked Lists (3.2)
student John Smith 40725 3.58 Object References • An object referenceis a variable that stores the address of an object • A reference also can be called a pointer • References often are depicted graphically:
studentList John Smith 40725 3.57 Jane Jones 58821 3.72 References as Links • Object references can be used to createlinksbetween objects • Suppose a Student class contains a reference to another Student object • References can be used to create a variety of linked structures, such as a linked list:
list count: 4 front rear list Other Dynamic Representations • It may be convenient to implement as list as a doubly linked list, with next and previous references • It may be convenient to use a separate header node, with a count and references to both the front and rear of the list
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 D A B C
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. Head Tail D A B C
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
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(Objecte, Node n){ element= e; next= n;} Public Object getElement(){ return element;} public Node getNext(){ return next; } public void setElement(ObjectnewElem){element= newElem;} public void setNext(NodenewNext){ next= newNext; } }
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 boolIsEmpty() { return head == NULL; } public Node InsertNode(int index, double x); public intFindNode(doublex); public intDeleteNode(doublex); public void DisplayList(void); }
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
Inserting a new node • Fourcases of InsertNode: • Insert into an empty list: • when list is empty (head = NULL), • both head and tail must point to the new node. • Insert in front (at the head) • Insert at back ( at the tail) • Insertbetween two nodes head tail
Have new node point to old head Update head to point to new node Increment size of list Inserting at the Head tail head Φ tail head Φ
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;
Inserting at the Tail Have new node point to null Have old last node point to new node Update tail to point to new node tail head Φ tail head Φ Φ head tail Φ Tail.next =newNode; Tail=NewNode;
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
Insertingbetween two nodes Have new node point to next of previous node Have old last node point to new node Increment size of list head head tail tail currNode currNode newNode newNode newNode.next= currNode.next; currNode.next=newNode;
Removing Node • Four casesoccur 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 twonodes. Head and tail links are not updated head tail
Removing at the Head head tail tail head • Update head to point to next node in the list • Allow garbage collector to reclaim the former first node • Decrement size of list Φ Φ tail head Φ
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
Removing at the Tail tail tail tail tail tail head head head head head • Traverse the list to find the previous node of the tail • Set the tail to point to the previous node • Set next of previous node to NULL Φ Φ Φ Φ Φ temp temp temp temp
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
Removinga node between two nodes tail tail tail tail tail head head head head head • Traverse the list to find the previous node for deleted node • Set the previous node to point to the next of the deleted node • Remove deleted node Φ Φ Φ Φ Φ temp temp temp temp
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.