cs212 datastructures n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Cs212: DataStructures PowerPoint Presentation
Download Presentation
Cs212: DataStructures

Loading in 2 Seconds...

play fullscreen
1 / 12
sierra-dorsey

Cs212: DataStructures - PowerPoint PPT Presentation

95 Views
Download Presentation
Cs212: DataStructures
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. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Cs212: DataStructures Lab 6: Linked List (part 2)

  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.

  3. 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+"} "); } }

  4. 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; }

  5. 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 } } } }

  6. 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(""); }

  7. 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

  8. 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; } }

  9. 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

  10. 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; }

  11. 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(); } }

  12. Example 1 (cont.) • Output