Linked List Errors

Hello everybody,
I have this program that I created, where the insert and remove functions were used to edit a linked list with other values. I've fixed problems so far, but there is a new one I cant figure out. If I comment out lines 91-93, the program breaks. I run the debugger and the print function is where it says the problem is. I dont know what the problem is. There are comments in the code where I encounter the problems.

If anyone could please advise, i've had trouble with this for a bit now and I dont know where to look. Thank you in advance

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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include <iostream>
#include <string>
using namespace std;

struct Cafe
	{
	string item;
	int count;
	Cafe *link;
	};

typedef Cafe* CafePtr; 

void head_insert(CafePtr& head, string i, int c);
CafePtr searchnum(CafePtr& head, int target);
CafePtr searchstr(CafePtr& head, string target); 
void insert (CafePtr after_me, string i, int c);//insert a node in the list
void print (CafePtr head);						//print the list
void remove(CafePtr before, CafePtr discard);	//remove a node from the list

int main(){

	CafePtr head;
	CafePtr iter, h;

	head = new Cafe;
	head->link = NULL;

	head_insert(head, "QL", 3808);
	head_insert(head, "CHGHS", 1312);
	head_insert(head, "REI", 10466);
	head_insert(head, "CPT", 1678);
	head_insert(head, "NetApp", 6887);
	head_insert(head, "EJ", 36937);
	head_insert(head, "WFM", 41717);
	head_insert(head, "SAS", 6046);
	head_insert(head, "BCG", 1958);
	head_insert(head, "Google", 18500);

	print (head);

	CafePtr before = searchstr(head, "Google"); 
	CafePtr discard = searchstr(head, "BCG");
	remove(before, discard);

	before = searchstr(head, "Google");
	discard = searchstr(head, "SAS"); 
	remove(before, discard);

	CafePtr after_me = searchstr(head, "Google");
	insert(after_me, "SAS", 6373);

	after_me = searchstr(head, "SAS");
	insert(after_me, "CHGHS", 1378);

	before = searchstr(head, "CHGHS");
	discard = searchstr(head, "WFM"); 
	remove(before, discard);

	after_me = searchstr(head, "CHGHS");
	insert(after_me, "BCG", 2314);

	before = searchstr(head, "BCG");
	discard = searchstr(head, "EJ"); 
	remove(before, discard);

	after_me = searchstr(head, "BCG");
	insert(after_me, "WFM", 43927);

	before = searchstr(head, "WFM");
	discard = searchstr(head, "NetApp"); 
	remove(before, discard);

	after_me = searchstr(head, "WFM");
	insert(after_me, "NetApp", 7426);

	before = searchstr(head, "NetApp");
	discard = searchstr(head, "CPT"); 
	remove(before, discard);

	after_me = searchstr(head, "NetApp");
	insert(after_me, "Hilcorp", 1012);

	before = searchstr(head, "Hilcorp");
	discard = searchstr(head, "REI"); 
	remove(before, discard);

	after_me = searchstr(head, "Hilcorp");
	insert(after_me, "EJ", 35114);

	//before = searchstr(head, "EJ");
	//discard = searchstr(head, "CHGHS"); <- If this bunch gets uncommented, the program breaks.
	//remove(before, discard);

/*	after_me = searchstr(head, "EJ");
	insert(after_me, "Ultimate", 1440);

	after_me = searchstr(head, "CHGHS"); //These things just need to be added after, these are the two final additions to the list
	insert(after_me, "CPT", 1896);
	*/


	cout << endl;
	print(head); //If i remove this from the program, and uncomment the problem lines above, the program runs fine.
	return 0;

}

void head_insert(CafePtr& head, string i, int c)
{
	CafePtr temp_ptr;
	temp_ptr = new Cafe;

	temp_ptr -> item = i;
	temp_ptr -> count = c;

	temp_ptr -> link = head;
	head = temp_ptr;

}

CafePtr searchnum(CafePtr& head, int target)
{
	CafePtr here = head;

	if(here == NULL)
	{
		return NULL;
	}
	else
	{
		while(here->count !=target && here->link != NULL)
			here = here->link;
		if(here->count == target)
			return here;
		else
			return NULL;
	}
}

CafePtr searchstr(CafePtr& head, string target)
{
	CafePtr here = head;

	if(here == NULL)
	{
		return NULL;
	}
	else
	{
		while(here->item !=target && here->link != NULL)
			here = here->link;
		if(here->item == target)
			return here;
		else
			return NULL;
	}
}

void insert (CafePtr after_me, string i, int c)
{
	CafePtr temp_ptr;
	temp_ptr = new Cafe;
	temp_ptr->item = i;
	temp_ptr->count = c;

	temp_ptr->link = after_me->link;
	after_me->link = temp_ptr;			
}


void print (CafePtr head)
{

	for (CafePtr iter = head; iter !=NULL; iter=iter->link)
	{
		cout << (iter->item) << "\t" << (iter->count) << endl; //This line is where the debugger encounters a problem
	}
}

void remove(CafePtr before, CafePtr discard)
{
	before->link = discard->link;
	delete discard;					
}
Last edited on
Your `remove()' function is error-prone.
If you set a breakpoint in the remove function, you'll see that `before->link' does not point to `discard' when is invoked in line 93.
That's because your list at that point is
Google  18500
SAS     6373
CHGHS   1378
BCG     2314
WFM     43927
NetApp  7426
Hilcorp 1012
EJ      35114
CHGHS   1312
QL      3808
        0
`discard' is pointing to the third cell and `before' to the eight.
Last edited on
Topic archived. No new replies allowed.