1 / 12

Cs212: DataStructures

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

rae-bolton
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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

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: int Data; Node *next; // Pointer to next node Node(int d){ // constructor Data=d; next=NULL; } };

  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 list intcount; // count the number of nodes in the list public: List(){ // constructor count=0; head=tail=NULL; } intlistcount(){ // return the count of nodes in the list returncount; }

  5. Review (cont.) • List Class – insertNode function voidInsertNode (intkey){ Node* NewNode = newNode(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++;} elsecout<< endl<<"Duplicated data ! "<< endl ; } } } }

  6. Review (cont.) • List Class – displayList function voiddisplayList(){ // 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; }

  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 function boolDeleteNode(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 exist 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--; deletePLoc;//freed memory returntrue; } }

  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

  10. Retrieve a Node (cont.) • List Class – RetrieveNode function boolRetrieveNode( inttarget , Node& dataout){ if(count == 0 || target < head->data || target > tail->data ) returnfalse; 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; returntrue; } else returnfalse; } }; // end of the list class

  11. Example 1 (cont.) • Main function #include<iostream> usingnamespacestd; voidmain() { //Using List List 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"; Noded(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"); }

  12. Example 1 (cont.) • Output

More Related