Trouble with Linked Lists Delete Function

First: This is for homework and no I am not asking for the entire solution I just need help figuring out how to do this. Now, I've been really sick the past few days and I missed the class that went over linked lists so I'm really confused on this. The teacher gave us most of the code and asked us to add a SplitLists function which I already did and he asked us to change the DeleteItem function so that it will not do anything when an item specified is not in the list and make sure ALL copies of the item are removed if it exist. So the test input he gave us fills the list with these numbers: 5, 7, 6, 9, and 9. Right now when I run the code without changing the DeleteItem function and using the test inputs DeleteItem 5, DeleteItem 2, and DeleteItem 9, it deletes 5 but then it stops working when I try to Delete 2. So what I can't figure out is how to make it keep going even if the item is not in the list and how to make it delete both 9's at once. I did it with an array based list but I just can't figure out how to do it with linked list. I've tried looking at videos on linked lists, looking at slides from class, and other websites and references and I just can't figure it out (though it doesn't help that I'm still really sick). So I appreciate any advice or hints.
here is the code containing the DeleteItem() function and the other class that goes with it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// The following declarations and definitions go into file 
// ItemType.h. 

#include <fstream>
const int MAX_ITEMS = 5;
enum RelationType  {LESS, GREATER, EQUAL};

class ItemType 
{ 
   public:
      ItemType();
      RelationType ComparedTo(ItemType) const;
      void Print(std::ostream&) const;
      void Initialize(int number);
   private:
      int value;
};


ItemType.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <fstream>
#include <iostream>
#include "ItemType.h"

ItemType::ItemType()
{ 
   value = 0;
}

RelationType ItemType::ComparedTo(ItemType otherItem) const 
{
   if (value < otherItem.value)
      return LESS;
   else if (value > otherItem.value)
      return GREATER;
   else return EQUAL;
}

void ItemType::Initialize(int number) 
{
   value = number;
}

void ItemType::Print(std::ostream& out) const 
// pre:  out has been opened.
// post: value has been sent to the stream out.
{
   out << value;
}


UnsortedType.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "ItemType.h"
struct NodeType;
class UnsortedType 
{
   public:
      UnsortedType();
      // Constructor
      ~UnsortedType();
      // Destructor
      void MakeEmpty();
      bool IsFull() const;
      int GetLength() const;
      ItemType GetItem(ItemType& item, bool& found);
      void PutItem(ItemType item);
      void DeleteItem(ItemType item);
      void ResetList();
      ItemType GetNextItem();
       
   private:
      NodeType* listData;
      int length;
      NodeType* currentPos;
};

UnsortedType.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// This file contains the linked implementation of class
// UnsortedType.

#include "UnsortedType.h"

struct NodeType
{
   ItemType info;
   NodeType* next;
};


UnsortedType::UnsortedType()  // Class constructor
{
   length = 0;
   listData = NULL;
}


bool UnsortedType::IsFull() const
// Returns true if there is no room for another ItemType 
//  on the free store; false otherwise.
{
   NodeType* location;
   try
   {
      location = new NodeType;
      delete location;
      return false;
   }
   catch(std::bad_alloc exception)
   {
      return true;
   }
}


int UnsortedType::GetLength() const
// Post: Number of items in the list is returned.
{
   return length;
}


void UnsortedType::MakeEmpty()
// Post: List is empty; all items have been deallocated.
{
   NodeType* tempPtr;

   while (listData != NULL)
   {
      tempPtr = listData;
      listData = listData->next;
      delete tempPtr;
   }
   length = 0;
}


void UnsortedType::PutItem(ItemType item)
// item is in the list; length has been incremented.
{
   NodeType* location;		// Declare a pointer to a node

   location = new NodeType;	// Get a new node 
   location->info = item;	// Store the item in the node
   location->next = listData;	// Store address of first node 
 				//   in next field of new node
   listData = location;		// Store address of new node into
				//   external pointer
   length++;			// Increment length of the list
}


ItemType UnsortedType::GetItem(ItemType& item, bool& found)
// Pre:  Key member(s) of item is initialized.
// Post: If found, item's key matches an element's key in the 
//       list and a copy of that element has been stored in item;
//       otherwise, item is unchanged. 
{
   bool moreToSearch;
   NodeType* location;

   location = listData;
   found = false;
   moreToSearch = (location != NULL);

   while (moreToSearch && !found) 
   {
      switch (item.ComparedTo(location->info))
      {
         case LESS    : 
         case GREATER : location = location->next;
                        moreToSearch = (location != NULL);
                        break;
         case EQUAL   : found = true;
                        item = location->info;
                        break;
      }
   }
   return item;
}


void UnsortedType::DeleteItem(ItemType item)
// Pre:  item's key has been initialized.
//       An element in the list has a key that matches item's.
// Post: No element in the list has a key that matches item's.
{
   NodeType* location = listData;
   NodeType* tempLocation = NULL;

   // Locate node to be deleted.
   if (item.ComparedTo(listData->info) == EQUAL)
   {
      tempLocation = location;
      listData = listData->next;		// Delete first node.
   }
   else
   {
      while (item.ComparedTo((location->next)->info) != EQUAL)
         location = location->next;

      // Delete node at location->next
      tempLocation = location->next;
      location->next = (location->next)->next;
   }
   delete tempLocation;
   length--;
}


void UnsortedType::ResetList()
// Post: Current position has been initialized.
{
   currentPos = NULL;
}
 

ItemType UnsortedType::GetNextItem()
// Post:  A copy of the next item in the list is returned.
//        When the end of the list is reached, currentPos
//        is reset to begin again.
{
   ItemType item;
   if (currentPos == NULL)
      currentPos = listData;
   else
      currentPos = currentPos->next;
   item = currentPos->info;
   return item;
}


UnsortedType::~UnsortedType()
// Post: List is empty; all items have been deallocated.
{
   NodeType* tempPtr;

   while (listData != NULL)
   {
      tempPtr = listData;
      listData = listData->next;
      delete tempPtr;
   }
}


and here is the DeleteItem() function by itself:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
void UnsortedType::DeleteItem(ItemType item)
// Pre:  item's key has been initialized.
//       An element in the list has a key that matches item's.
// Post: No element in the list has a key that matches item's.
{
   NodeType* location = listData;
   NodeType* tempLocation = NULL;

   // Locate node to be deleted.
   if (item.ComparedTo(listData->info) == EQUAL)
   {
      tempLocation = location;
      listData = listData->next;		// Delete first node.
   }
   else
   {
      while (item.ComparedTo((location->next)->info) != EQUAL)
         location = location->next;

      // Delete node at location->next
      tempLocation = location->next;
      location->next = (location->next)->next;
   }
   delete tempLocation;
   length--;
}


Thanks in advance for any help!
Last edited on
What happens on line 10 of DeleteItem if it's called on an empty list?

What happens on line 17 if you don't encounter the item in the list?

In both cases you dereference a null pointer. Fix it so you don't.
Topic archived. No new replies allowed.