From a3cc673c86b7dc6e6fbf9920ac4235384a31599e Mon Sep 17 00:00:00 2001 From: Iurii Tatishchev Date: Sun, 28 Apr 2024 12:25:34 -0700 Subject: [PATCH] 6.20 Lab: Doubly-Linked List (Delete) --- 04-dl-lists/StudentList.cpp | 27 +++++++++++++++++++++++++++ 04-dl-lists/StudentList.h | 5 ++--- 04-dl-lists/main.cpp | 33 +++++++++++++++------------------ 3 files changed, 44 insertions(+), 21 deletions(-) diff --git a/04-dl-lists/StudentList.cpp b/04-dl-lists/StudentList.cpp index 5077c2e..912d5db 100644 --- a/04-dl-lists/StudentList.cpp +++ b/04-dl-lists/StudentList.cpp @@ -103,6 +103,33 @@ void StudentList::insertNode(Student dataIn) { count++; } +// ************************************************** +// The deleteNode function deletes a node with +// the same key as its paramenter, if found +// ************************************************** +bool StudentList::deleteNode(string target) { + ListNode *pCur; // To traverse the list + bool success = false; + + pCur = head->forw; + while (pCur != head && pCur->stu.name < target) { + pCur = pCur->forw; + } + + if (pCur != head && pCur->stu.name == target) { + ListNode *pPrev = pCur->back; + ListNode *pNext = pCur->forw; + pPrev->forw = pNext; + pNext->back = pPrev; + delete pCur; + success = true; + count--; + } + + return success; +} + + // ************************************************** // Destructor * // This function deletes every node in the list. * diff --git a/04-dl-lists/StudentList.h b/04-dl-lists/StudentList.h index 5a6d8b6..0782110 100644 --- a/04-dl-lists/StudentList.h +++ b/04-dl-lists/StudentList.h @@ -7,8 +7,6 @@ #ifndef STUDENTLIST_H #define STUDENTLIST_H -#include - struct Student { double gpa; std::string name; @@ -36,7 +34,8 @@ public: void insertNode(Student); - //bool deleteNode(double); + bool deleteNode(std::string); + //void searchList() const; void displayListForw() const; diff --git a/04-dl-lists/main.cpp b/04-dl-lists/main.cpp index 0caf983..00d78bd 100644 --- a/04-dl-lists/main.cpp +++ b/04-dl-lists/main.cpp @@ -1,17 +1,17 @@ /* - CIS 22C +CIS 22C: Sorted Circular Doubly-Linked List with Sentinel Node - Sorted Circular Doubly-Linked List with Sentinel Node - - This program: -- Creates a sorted linked list (student name and gpa) . The list is sorted in ascending order by name. +This program: +- Creates a sorted linked list (student name and gpa). The list is sorted in ascending order by name. - Displays the list forwards(A to Z) +- Deletes nodes from the list - Displays the list backwards(Z to A) - Requirements: Finish writing the following three functions: -- default constructor -- insertNode() -- displayListBack() +Your task is to finish writing the following three functions: +- default constructor (reuse code from your previous Lab) +- insertNode() (reuse code from your previous Lab) +- displayListBack() (reuse code from your previous Lab) +- deleteNode() Written by: Iurii Tatishchev Reviewed by: Iurii Tatishchev @@ -33,15 +33,14 @@ int main() { buildList(list); // insert data into the list cout << "There are " << list.getCount() << " elements in the list." << endl; - string answer; - cout << "Insert [Y/N]? "; + cout << "Delete [Y/N]? "; getline(cin, answer); if (answer == "Y" || answer == "y") { - Student s; - cout << "Enter gpa then enter name:" << endl; - cin >> s.gpa >> s.name; - list.insertNode(s); + string targetName; + cout << "Enter name to be removed from the list:" << endl; + getline(cin, targetName); + cout << targetName << (list.deleteNode(targetName) ? " - deleted!" : " - not found!") << endl; cout << "There are " << list.getCount() << " elements in the list." << endl; } cout << endl; @@ -69,6 +68,4 @@ void buildList(StudentList &list) { getline(cin, line); } -} - - +} \ No newline at end of file