12
CS212: DATASTRUCTURES Lab 6: Linked List (part 2) 1

Cs212: DataStructures

Embed Size (px)

DESCRIPTION

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 - PowerPoint PPT Presentation

Citation preview

Page 1: Cs212:  DataStructures

CS212: DATASTRUCTURES

Lab 6: Linked List (part 2)

1

Page 2: Cs212:  DataStructures

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.

Page 3: Cs212:  DataStructures

3

Review (cont.)

Node Class

class Node { // Declare Node class

public int Data; public Node next; // Reference to next node public Node(int d){ // constructor

Data=d; // next by default initialized to null } public void displayNode(){// to print the data in the node System.out.print("{"+Data+"} "); }

}

 

Page 4: Cs212:  DataStructures

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 int listcount(){ // return the count of nodes in the list

return count;

}

Page 5: Cs212:  DataStructures

5

Review (cont.) List Class – insertNode methodpublic void InsertNode (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++;} else System.out.println("\nDuplicated data!\n"); // key == ploc.date } } } }

Page 6: Cs212:  DataStructures

6

Review (cont.)

List Class – displayList method

public void displayList(){ // 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(""); }

Page 7: Cs212:  DataStructures

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

Page 8: Cs212:  DataStructures

8

Delete a Node (cont.) List Class – DeleteNode method

public boolean DeleteNode (int key , Node dataout){

if (count == 0 || key < head.data || key > tail.data ) return false;

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 return false; 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--; return true; }}

Page 9: Cs212:  DataStructures

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

Page 10: Cs212:  DataStructures

10

Retrieve a Node (cont.)

List Class – RetrieveNode method

public boolean RetrieveNode( int target , Node dataout){

if (count == 0 || target < head.data || target > tail.data ) return false; Node pPre = null; Node pLoc = head; while (target != pLoc.data && pLoc.next != null) { pPre = pLoc; pLoc = pLoc.next; } if (target == pLoc.data){ dataout.data= pLoc.data; return true; } else return false; }

Page 11: Cs212:  DataStructures

11

Example 1 (cont.) Main Method

package orderdlist;public class OrderdList { public static void 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(); }}

Page 12: Cs212:  DataStructures

12

Example 1 (cont.) Output