6.21 Lab: Doubly-Linked List (Insert - reject duplicates)
Fix memory leak when attempting to insert a duplicate
This commit is contained in:
parent
30ad8892de
commit
276575ca62
@ -83,10 +83,6 @@ 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
|
||||||
|
|
||||||
// Allocate a new node and store num there.
|
|
||||||
newNode = new ListNode;
|
|
||||||
newNode->stu = dataIn;
|
|
||||||
|
|
||||||
// Initialize pointers
|
// Initialize pointers
|
||||||
pCur = head->forw;
|
pCur = head->forw;
|
||||||
|
|
||||||
@ -98,6 +94,10 @@ bool StudentList::insertNode(Student dataIn) {
|
|||||||
// Return false early if the name is a duplicate
|
// Return false early if the name is a duplicate
|
||||||
if (pCur->stu.name == dataIn.name) return false;
|
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
|
// 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;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user