Common Operations on Linked Lists: Insertion, Deletion, and Retrieval Overview
This document provides a comprehensive review of common operations on linked lists, focusing on creating a linked list, inserting and deleting nodes, and retrieving data. It discusses the structure of node and list classes, including constructors and member functions for these operations. Key functions like insertNode, deleteNode, and retrieveNode are explained, highlighting their purposes, expected results, and edge cases. Practical examples demonstrate how to use these functions in a C++ program, making it easier to understand linked list management in programming.
Common Operations on Linked Lists: Insertion, Deletion, and Retrieval Overview
E N D
Presentation Transcript
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: int Data; Node *next; // Pointer to next node Node(int d){ // constructor Data=d; next=NULL; } };
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; }
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 ; } } } }
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; }
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 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; } }
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
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
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"); }
Example 1 (cont.) • Output