6.21 Lab: Doubly-Linked List (Insert - reject duplicates)

This commit is contained in:
Iurii Tatishchev 2024-04-28 12:38:23 -07:00
parent a3cc673c86
commit 2f6299ac85
Signed by: CaZzzer
GPG Key ID: 9A156B7DA6398968
3 changed files with 31 additions and 51 deletions

View File

@ -72,10 +72,14 @@ void StudentList::displayListBack() const {
} }
// ************************************************** // **************************************************
// The insertNode function inserts a node with // The insertNode function checks if a node with the
// stu copied to its value member. // same key as dataIn's key is found in the list
// If it is found, returns false, meaning that insert
// has failed to add the new data (duplicate!)
// otherwise inserts a node with
// stu copied to its value member, and returns true.
// ************************************************** // **************************************************
void StudentList::insertNode(Student dataIn) { bool StudentList::insertNode(Student dataIn) {
ListNode *newNode; // A new node ListNode *newNode; // A new node
ListNode *pCur; // To traverse the list ListNode *pCur; // To traverse the list
@ -91,6 +95,9 @@ void StudentList::insertNode(Student dataIn) {
pCur = pCur->forw; pCur = pCur->forw;
} }
// Return false early if the name is a duplicate
if (pCur->stu.name == dataIn.name) return false;
// Insert the new node between pPre and pCur // Insert the new node between pPre and pCur
ListNode *pPre = pCur->back; // The previous node ListNode *pPre = pCur->back; // The previous node
pPre->forw = newNode; pPre->forw = newNode;
@ -101,38 +108,13 @@ void StudentList::insertNode(Student dataIn) {
// Update the counter // Update the counter
count++; count++;
return true;
} }
// ************************************************** // **************************************************
// The deleteNode function deletes a node with // Destructor
// the same key as its paramenter, if found // This function deletes every node in the list.
// **************************************************
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. *
// ************************************************** // **************************************************
StudentList::~StudentList() { StudentList::~StudentList() {

View File

@ -32,10 +32,9 @@ public:
// Linked list operations // Linked list operations
int getCount() const { return count; } int getCount() const { return count; }
void insertNode(Student); bool insertNode(Student);
bool deleteNode(std::string);
// bool deleteNode(double);
//void searchList() const; //void searchList() const;
void displayListForw() const; void displayListForw() const;

View File

@ -2,16 +2,14 @@
CIS 22C: Sorted Circular Doubly-Linked List with Sentinel Node CIS 22C: Sorted Circular Doubly-Linked List with Sentinel Node
This program: This program:
- Creates a sorted linked list (student name and gpa). The list is sorted in ascending order by name. - Creates a sorted linked list (student name and gpa). The list is sorted in ascending order by name. Assume name is a unique identifier.
- Displays the list forwards(A to Z) - Displays the list forwards(A to Z)
- Deletes nodes from the list
- Displays the list backwards(Z to A) - Displays the list backwards(Z to A)
Your task is to finish writing the following three functions: Your task is to finish writing the following three functions:
- default constructor (reuse code from your previous Lab) - default constructor (reuse code from your previous lab)
- insertNode() (reuse code from your previous Lab) - `displayListBack()` (reuse code from your previous lab)
- displayListBack() (reuse code from your previous Lab) - `insertNode()` - change this function to reject duplicates. Assume `name` is a unique key.
- deleteNode()
Written by: Iurii Tatishchev Written by: Iurii Tatishchev
Reviewed by: Iurii Tatishchev Reviewed by: Iurii Tatishchev
@ -32,16 +30,18 @@ int main() {
StudentList list; StudentList list;
buildList(list); // insert data into the list buildList(list); // insert data into the list
cout << "There are " << list.getCount() << " elements in the list." << endl; cout << "The number of elements in the list is " << list.getCount() << "." << endl;
string answer; string answer;
cout << "Delete [Y/N]? "; cout << "Insert [Y/N]? ";
getline(cin, answer); cin >> answer;
if (answer == "Y" || answer == "y") { if (answer == "Y" || answer == "y") {
string targetName; Student s;
cout << "Enter name to be removed from the list:" << endl; cout << "Enter gpa then enter name:" << endl;
getline(cin, targetName); cin >> s.gpa >> s.name;
cout << targetName << (list.deleteNode(targetName) ? " - deleted!" : " - not found!") << endl; if (!list.insertNode(s)) {
cout << "There are " << list.getCount() << " elements in the list." << endl; cout << s.name << " - rejected: duplicate key!" << endl;
}
cout << "The number of elements in the list is " << list.getCount() << "." << endl;
} }
cout << endl; cout << endl;
list.displayListForw(); list.displayListForw();
@ -67,5 +67,4 @@ void buildList(StudentList &list) {
list.insertNode(newStu); list.insertNode(newStu);
getline(cin, line); getline(cin, line);
} }
}
}