Can someone tell me what I'm doing wrong?

My gcc compiler is screaming at me when I run this code, what am I doing wrong here? There is also a supplied.o file for the other functions.

arr.h file:

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
#include "arr.h"

arr.h file:
[code]
//arr.h
#include <iostream>
#include <cstring>
#include <cctype>

const int SIZE = 5;	//size of the array of head pointers

struct node
{
    int data;
    node * next;
};


/* These functions are already written and can be called to test out your code */
void build(node * head[]);  //supplied
void display(node * head[]);  //supplied
void destroy(node * head[]); //supplied

/* *****************YOUR TURN! ******************************** */
//Write your function prototype here:
int sum(node * head[]);
void removeTwo(node * head[]);


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
#include "arr.h"

int sum(node * head[])
{
	int total = 0;
	
	for(int i = 0; i < 5; i++)
	{
		node *temp = head[i];
		while(temp != NULL)
		{
			total += temp->data;
			temp = temp-> next;

		}
	}
	
	return total;
}


void removeTwo(node * head[])
{

	for(int i = 0; i < 5; i++)
	{	
		node *temp = head[i];
		node * prev = temp; 


		while(temp != NULL)
		{
			if(temp->data == 2)
			{
				if(temp == head[i])
				{
					prev->next = head[i]->next;
					delete temp;
					temp = prev->next;
					prev = temp;
					*head = temp;
				}
				else if(temp->next == NULL)
				{
					delete temp;
					prev->next = NULL;
				}
				else
				{
					prev->next = temp->next;
					delete temp;
					temp = prev->next;
					prev = temp;

				}
			}
			temp = temp->next;
			prev = temp;
		}
	
	}

	

}



main.cpp file:

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
#include <iostream>
#include "arr.h"

using namespace std;

int main()
{
    node * head[SIZE] = {0,0,0,0,0};
    build(head);
    display(head);

    //PLEASE PUT YOUR CODE HERE to call the function assigned

	int total = sum(head);
	//cout << "Sum of array of linked list is:" << endl;
	cout << total << '\n' << endl;

	removeTwo(head);
	//cout << "The array of linked list after removing two: " << endl;

    

    display(head);
    destroy(head);
    
    return 0;
}


Last edited on
What is the error message? If it is something like "undefined reference" about the "other functions" you are probably not linking the supplied .o file correctly.
The if clause on line 35 in removeTwo(...) is certainly wrong.

1
2
3
4
5
6
7
8
9
10
11
12
				if(temp == head[i])
				{
					if(head[i]->next)
					{
						temp = head[i]->next;
						head[i]->next = temp->next;
						delete temp;
						temp = head[i];
					}
					else
						??? // Problem here: What to do with the content of head[i]?
				}
Topic archived. No new replies allowed.