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

Cs212: DataStructures

Jan 02, 2016

Download

Documents

rae-bolton

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
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
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;

Node *next; // Pointer to next node

Node(int d){ // constructor Data=d;

next=NULL; }};

 

Page 4: Cs212:  DataStructures

4

Review (cont.)

List Class

class List{ // Declare List class

private:Node *head; // pointer to the first Node in the list

Node *tail; // pointer to the last Node in the listint count; // count the number of nodes in the list

public: List(){ // constructor

count=0;head=tail=NULL;

} int listcount(){ // return the count of nodes in the list

return count;

}

Page 5: Cs212:  DataStructures

5

Review (cont.) List Class – insertNode functionvoid InsertNode (int key ){ 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{ Node * Ppre = NULL; // pointer to the previous node Node * PLoc = head; // pointer to the current node while ( key > PLoc->data && PLoc->next != NULL ){ Ppre = PLoc; PLoc = PLoc->next;} if ( key < PLoc->data ){ NewNode->next = PLoc; // or NewNode->next = Ppre->next; Ppre->next = NewNode; count++;} else cout << endl<<"Duplicated data ! "<< endl ;

} } }}

Page 6: Cs212:  DataStructures

6

Review (cont.)

List Class – displayList function

void displayList(){ // to print the data for each node in the list cout<<"List (first-->last):"<<endl; Node *current = head; while (current != NULL){

cout<< "{"<<current->data<< "} "; current = current->next;

} cout<< endl; }

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 function

bool 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 exist 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--; delete PLoc;//freed memory return true; }}

Page 9: Cs212:  DataStructures

9

Retrieve a Node

Purpose: Interface to search function 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 function

bool 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;}}; // end of the list class

Page 11: Cs212:  DataStructures

11

Example 1 (cont.) Main function#include<iostream>using namespace std;

void main() { //Using ListList s;s.InsertNode(50); s.InsertNode(20);s.InsertNode(80);s.InsertNode(10);s.InsertNode(80);s.InsertNode(90);s.displayList();

cout << " \n The number of nodes in the list is :" << s.listcount()<< "\n"; Node d(0); s.DeleteNode(80, d); cout<< d.data<<endl; d.data=0; s.DeleteNode(30, d); cout<< d.data<<endl; d.data=0; s.DeleteNode(10, d); cout<< d.data<<endl;d.data=0; s.displayList(); s.RetrieveNode(20, d) ; cout<< d.data<<endl;d.data=0; s.RetrieveNode(100, d) ; cout<< d.data<<endl; d.data=0; s.RetrieveNode(50, d) ; cout<< d.data<<endl; d.data=0; s.displayList();

system("pause");}

Page 12: Cs212:  DataStructures

12

Example 1 (cont.) Output