From 276575ca6292760017b95083d33f7520a7d947b2 Mon Sep 17 00:00:00 2001 From: Iurii Tatishchev Date: Sun, 19 May 2024 13:46:15 -0700 Subject: [PATCH] 6.21 Lab: Doubly-Linked List (Insert - reject duplicates) Fix memory leak when attempting to insert a duplicate --- 04-dl-lists/StudentList.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/04-dl-lists/StudentList.cpp b/04-dl-lists/StudentList.cpp index 820e1f7..094a493 100644 --- a/04-dl-lists/StudentList.cpp +++ b/04-dl-lists/StudentList.cpp @@ -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;