6.20 Lab: Doubly-Linked List (Delete)

This commit is contained in:
Iurii Tatishchev 2024-04-28 12:25:34 -07:00
parent fa7aed64b2
commit a3cc673c86
Signed by: CaZzzer
GPG Key ID: 9A156B7DA6398968
3 changed files with 44 additions and 21 deletions

View File

@ -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. *

View File

@ -7,8 +7,6 @@
#ifndef STUDENTLIST_H
#define STUDENTLIST_H
#include <string>
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;

View File

@ -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;
@ -70,5 +69,3 @@ void buildList(StudentList &list) {
}
}