6.21 Lab: Doubly-Linked List (Insert - reject duplicates)
This commit is contained in:
parent
a3cc673c86
commit
2f6299ac85
@ -72,10 +72,14 @@ void StudentList::displayListBack() const {
|
||||
}
|
||||
|
||||
// **************************************************
|
||||
// The insertNode function inserts a node with
|
||||
// stu copied to its value member.
|
||||
// The insertNode function checks if a node with the
|
||||
// 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 *pCur; // To traverse the list
|
||||
|
||||
@ -91,6 +95,9 @@ void StudentList::insertNode(Student dataIn) {
|
||||
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
|
||||
ListNode *pPre = pCur->back; // The previous node
|
||||
pPre->forw = newNode;
|
||||
@ -101,38 +108,13 @@ void StudentList::insertNode(Student dataIn) {
|
||||
|
||||
// Update the counter
|
||||
count++;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// **************************************************
|
||||
// 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. *
|
||||
// Destructor
|
||||
// This function deletes every node in the list.
|
||||
// **************************************************
|
||||
StudentList::~StudentList() {
|
||||
|
||||
|
@ -32,10 +32,9 @@ public:
|
||||
// Linked list operations
|
||||
int getCount() const { return count; }
|
||||
|
||||
void insertNode(Student);
|
||||
|
||||
bool deleteNode(std::string);
|
||||
bool insertNode(Student);
|
||||
|
||||
// bool deleteNode(double);
|
||||
//void searchList() const;
|
||||
void displayListForw() const;
|
||||
|
||||
|
@ -2,16 +2,14 @@
|
||||
CIS 22C: 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.
|
||||
- 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)
|
||||
- Deletes nodes from the list
|
||||
- Displays the list backwards(Z to A)
|
||||
|
||||
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()
|
||||
- default constructor (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.
|
||||
|
||||
Written by: Iurii Tatishchev
|
||||
Reviewed by: Iurii Tatishchev
|
||||
@ -32,16 +30,18 @@ int main() {
|
||||
StudentList 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;
|
||||
cout << "Delete [Y/N]? ";
|
||||
getline(cin, answer);
|
||||
cout << "Insert [Y/N]? ";
|
||||
cin >> answer;
|
||||
if (answer == "Y" || answer == "y") {
|
||||
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;
|
||||
Student s;
|
||||
cout << "Enter gpa then enter name:" << endl;
|
||||
cin >> s.gpa >> s.name;
|
||||
if (!list.insertNode(s)) {
|
||||
cout << s.name << " - rejected: duplicate key!" << endl;
|
||||
}
|
||||
cout << "The number of elements in the list is " << list.getCount() << "." << endl;
|
||||
}
|
||||
cout << endl;
|
||||
list.displayListForw();
|
||||
@ -67,5 +67,4 @@ void buildList(StudentList &list) {
|
||||
list.insertNode(newStu);
|
||||
getline(cin, line);
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user