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

Fix memory leak when attempting to insert a duplicate
This commit is contained in:
Iurii Tatishchev 2024-05-19 13:46:15 -07:00
parent 30ad8892de
commit 276575ca62
Signed by: CaZzzer
GPG Key ID: 28BE602058C08557

View File

@ -83,10 +83,6 @@ bool StudentList::insertNode(Student dataIn) {
ListNode *newNode; // A new node
ListNode *pCur; // To traverse the list
// Allocate a new node and store num there.
newNode = new ListNode;
newNode->stu = dataIn;
// Initialize pointers
pCur = head->forw;
@ -98,6 +94,10 @@ bool StudentList::insertNode(Student dataIn) {
// Return false early if the name is a duplicate
if (pCur->stu.name == dataIn.name) return false;
// Allocate a new node and store num there.
newNode = new ListNode;
newNode->stu = dataIn;
// Insert the new node between pPre and pCur
ListNode *pPre = pCur->back; // The previous node
pPre->forw = newNode;