Delete from a singly linked list

I am trying to remove numbers that are less than a calculated average from a SSL.

Here is the section where I am running into error. I am having trouble at the test for the data.amt < average (here is where I want to remove the element and reassign the prev(ious) pointer). If I run this code it seems to test properly for what I am looking for, I just cant figure out to logic of changing the point values to "work around" or "ignore" the nodes that I do not want in the list any longer. Any advice would be much appreciated. Thank you.

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
void List::remove(float average){
	int x=0;
	int y=0;
	int z=0;
	Node* temp = head;
	Node* prev = head;
	while (temp->data.amt < average){
				head = temp->next;
				prev->next = head;
				x++;
			}
	while (temp !=NULL){
		
		if (temp->data.amt < average){
			prev->next = temp->next;
			
			y++;
		}
		prev = temp->next;
		temp = temp->next;
		z++;
	}
	cout << x << endl;
	cout << y << endl;
	cout << z << endl;
	cout << "-------------------------------" <<endl;
}






Here is the rest of my code for reference.

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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include <iostream>
#include <string>
#include <time.h>
#include <stdlib.h>
#include <iomanip>
using namespace std;
int r=0;
struct datatype
	{
		friend ostream &operator<< (ostream &stream, const datatype &dt){
			stream <<"Employee Name: "<<setw(6) <<dt.name << "\nID Number: " 
((((LINE BREAK FOR FORMAT))))<<setw (10) << dt.id <<"\nSalary: "<<setw(12)<< dt.amt <<"$\n"<<endl;
			return stream;
		}
		string name;
		int id;
		float amt;
        datatype(string Name, int Id, float Amt): name(Name), id(Id), amt(Amt) {}
        datatype(void): name(""), id(0), amt(0.0f) {}
		
	};
struct Node
{
		
	datatype data;
	Node *next;
};

class List 
{
	Node *head;
public:
			
			void insert(datatype dat);
			void remove(float average);
			void printEvery20();
			void print();
			List(){head = NULL;}
};

void List::insert( datatype dat )
{
    if( head == NULL )
    {
       head = new Node;
       head->data = dat;
       head->next = NULL;
	
    }
    else
    {
        Node* temp = head;
        while( temp->next != NULL ) temp = temp->next;
        temp->next = new Node;
        temp = temp->next;
        temp->data = dat;
        temp->next = NULL;
		 
    }
}
void List::remove(float average){
	int x=0;
	int y=0;
	int z=0;
	Node* temp = head;
	Node* prev = head;
	while (temp->data.amt < average){
				head = temp->next;
				prev->next = head;
				x++;
			}
	while (temp !=NULL){
		
		if (temp->data.amt < average){
			prev->next = temp->next;
			
			y++;
		}
		prev = temp->next;
		temp = temp->next;
		z++;
	}
	cout << x << endl;
	cout << y << endl;
	cout << z << endl;
	cout << "-------------------------------" <<endl;
}
void List::printEvery20()
{int r=1;
	int listCount=1;
    if( head == NULL )
    {
cout <<"list is empty " <<endl;
    }
    else
    {
        Node* temp = head;
		
		while (temp!= NULL){
			if((listCount % 50) == 0)
				{
					cout << temp->data;
					r++;
					listCount++;
				}
		temp = temp->next;
		listCount++;
		//cout <<" listCount: "<< listCount;
		}
		
		
    }
}
void List::print()
{int r=1;
	int listCount=1;
    if( head == NULL )
    {
cout <<"list is empty " <<endl;
    }
    else
    {
        Node* temp = head;
		
		while (temp!= NULL){
					cout << temp->data;
		temp = temp->next;
		r++;
		}
		
		
    }
	cout << " r is : : : " <<r<<endl;
}

char genFirst(){
char c=NULL;
do {c = (rand()%26)+65;}
while (c == 65 || c == 69 || c == 73 || c == 79 || c ==85);
return c;
}
char genVowel(){
char c=NULL;
while (c != 97 && c != 101 && c != 105 && c != 111 && c !=117)
	{
		c = (rand()%26)+97;
}
return c;
}
char genLast(){
char c=NULL;
do {c = (rand()%26)+97;}
while (c == 97 || c == 101 || c == 105 || c == 111 || c ==117);
return c;
}
int randId (){
	int id = rand();
	return id;
}
float randSal () {
	double sal = rand()%10000;
	return sal;
}

float averageRemove (float ar[]) {
	int size = sizeof(ar) / sizeof(float);
	float average =0;
	for (int i=0; i<size;i++){
	average += ar[i];
	average = average / size;
	}
	return average;
}
int main (){
int first=0;
string ok[1000];
int cc[1000];
float dd[1000];
srand (time(NULL));
List myList;
for (int i=0; i<1000; i++){
char f = genFirst();
ok[i] += f;
char s = genVowel();
ok[i] += s;
char l = genLast();
ok[i] += l;
cc[i] = randId();
dd[i] = randSal();
myList.insert(datatype(ok[i], cc[i], dd[i]));
}
float r =  averageRemove(dd);
cout <<"R is: "<< r <<endl;
cin.get();
myList.printEvery20();
system("cls");
cin.get();
myList.remove(r);
cin.get();
system("cls");
myList.print();
system ("color 4f");
cout << "\nProgram Complete"<<endl;
cin.get();
return 0;
}
Last edited on
Topic archived. No new replies allowed.