120 likes | 228 Views
Cs212: DataStructures. Lab 6: Linked List (part 2). Review. Common operations on a linked list: Create a linked list. Insert a node into the list. Delete a node from the list. Retrieve a node from the list. Review (cont.). Node Class. class Node { // Declare Node class
E N D
Cs212: DataStructures Lab 6: Linked List (part 2)
Review • Common operations on a linked list: • Create a linked list. • Insert a node into the list. • Delete a node from the list. • Retrieve a node from the list.
Review (cont.) • Node Class classNode { // Declare Node class public intData; public Node next; // Reference to next node public Node(intd){ // constructor Data=d; // next by default initialized to null } public voiddisplayNode(){// to print the data in the node System.out.print("{"+Data+"} "); } }
Review (cont.) • List Class class List{ // Declare List class private Node head; // Reference to the first Node in the list private Node tail;// Reference to the last Node in the list private int count; // count the number of nodes in the list public List(){ // constructor count=0; // head & tail by default initialized to null } public intlistcount(){ // return the count of nodes in the list returncount; }
Review (cont.) • List Class – insertNode method publicvoidInsertNode (int key ){// insert in order Node NewNode = new Node(key); // create a new node if(count ==0){ // Is it empty list? head=tail=NewNode; count++;} else{if( key < head.data){ // At beginning of the list NewNode.next= head; head=NewNode;count++;} else{if(key > tail.data){ // at end of the list tail.next=NewNode; tail=NewNode;count++;} else{ // the node insert in the middle of the list or it have duplicate data Node Ppre = null; //reference to the previous node Node PLoc = head; //reference to the current node while( key > PLoc.data && PLoc.next != null ){// if the key greater than data //in the current node and the current node does not point to null Ppre= PLoc; PLoc= PLoc.next;} if ( key < PLoc.data){NewNode.next = PLoc; // or NewNode.next = Ppre.next; Ppre.next= NewNode;count++;} elseSystem.out.println("\nDuplicated data!\n"); // key == ploc.date } } } }
Review (cont.) • List Class – displayList method public voiddisplayList(){ // to print the data for each node in the list System.out.print("List (first-->last): "); Node current = head; while (current != null){ current.displayNode(); current = current.next; } System.out.println(""); }
Delete a Node • Purpose: removes a node from linked list • Pre • key is identifier of node to be deleted • dataout is variable to receive a copy of the deleted data • Post • copied to output variable and node deleted or not found • Return • false if not found • true if deleted
Delete a Node (cont.) • List Class – DeleteNode method publicbooleanDeleteNode (int key , Node dataout){ if (count == 0 || key < head.data || key > tail.data ) returnfalse; Node pPre = null; Node PLoc = head; while ( key != PLoc.data && PLoc.next != null ){ pPre = PLoc; PLoc = PLoc.next; } if ( PLoc.next == null && key != PLoc.data ) // the node does not existing in the list returnfalse; else{ if ( key == head.data ){ // At beginning of the list head = head.next; dataout.data=PLoc.data;} else{ if ( key == PLoc.data){ pPre.next=PLoc.next; dataout.data=PLoc.data;} } if ( PLoc.next == null ) tail = pPre; count--; returntrue; } }
Retrieve a Node • Purpose: Interface to search method • Pre • key is the search argument • dataOut is variable to receive data • Post • dataOut contains located data if found • if not found, contents are unchanged • Return • true if successful, false if not found
Retrieve a Node (cont.) • List Class – RetrieveNode method publicbooleanRetrieveNode( inttarget , Nodedataout){ if(count == 0 || target < head.data || target > tail.data ) returnfalse; NodepPre = null; NodepLoc = head; while(target != pLoc.data && pLoc.next != null) { pPre = pLoc; pLoc= pLoc.next; } if(target == pLoc.data){ dataout.data= pLoc.data; returntrue; } else returnfalse; }
Example 1 (cont.) • Main Method package orderdlist; publicclassOrderdList { publicstaticvoid main(String[] args) { List s = new List(); s.InsertNode(50); s.InsertNode(20); s.InsertNode(80); s.InsertNode(10); s.InsertNode(80); s.InsertNode(90); s.displayList(); System.out.println( " \n The number of nodes in the list is :"+ s.listcount()+ "\n"); Node d = new Node(0); System.out.println( s.DeleteNode(80, d) + " " + d.data); d.data=0; System.out.println( s.DeleteNode(30, d)+ " " + d.data); d.data=0; System.out.println( s.DeleteNode(10, d)+ " " + d.data); d.data=0; s.displayList(); System.out.println( s.RetrieveNode(20, d) + " " + d.data); d.data=0; System.out.println( s.RetrieveNode(100, d)+ " " + d.data); d.data=0; System.out.println( s.RetrieveNode(50, d)+ " " + d.data); s.displayList(); } }
Example 1 (cont.) • Output