PLEASE HELP

Consider the following implementation of the node and linked-list:
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
template <class type>
class node
{
public:
type info;
node *link;
};

template <class type>
class Linkedlist
{
private:
node<type> *head, *tail;
int counter;


public:
Linkedlist()
{
head = tail = NULL;
counter = 0;
}

bool isEmpty()
{
return (head==NULL);
}

type getLast()
{
if (isEmpty()) {
cout << "Linked list is empty !!!" << endl;  assert(!isEmpty());
}
return tail->info;
}

void Append(type e)
{
node<type> *newnode = new node<type>;
newnode->info = e;
newnode->link = NULL;// (*newnode).link=NULL

if (isEmpty())
{
head = tail = newnode;
}
else
{
tail->link = newnode;
tail = newnode;
}
++counter;
}

void print()
{
if (isEmpty()) { cout << "Linked list is empty !!!" << endl; return; }
node<type> *current = head;

while (current != NULL)
{
cout << current->info << ",";
current = current->link;
}
cout << endl;
}

void Destroy()
{
node<type> *temp;
while (head != NULL)
{
temp = head;
head = head->link;
delete temp;
}
tail = NULL;
counter = 0;
}

bool search(type e)
{
bool found = false;
node<type> * current = head;
while (current != NULL && !found)
{
if (current->data == e) found = true;
else
current = current->link;
}
if (!found) return false;
else
return true;
}


~LinkedList()
{
Destroy();
}

 

};

Extend the class Linkedlist by adding the following methods:

Average method .This method should return the average of the elements in the linked-list. (5 points)
DeleteAtIndex method .This method should delete a node at a particular index. Hint: Suppose the index of the linked-list starts with 1 (5 points)
Delete method. This method should delete all occurrence of an element (value) from a linked-list. (6 points)
Average method .
It is like the print(). Instead of output (line 62) you add the info and count it. Then divide the added value by count.

DeleteAtIndex method .
Similar to the search(...) function. Count the iteration. When met instead of returning the value remove the node.

Delete method.
Similar to the DeleteAtIndex. Remove the node each time the criterion is met.

It makes sense to have a function that removes a node.
For the provided implementation, there are some issues. A copy constructor and operator= are required (or the compiler generated ones deleted as they do a shallow copy rather than the required deep copy). nullptr should be used instead of NULL.

Simplifying this code consider:

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
template <class type>
struct node {
	type info {};
	node* link {};

	node() {}
	node(const type& data) : info(data) {}
};

template <class type>
class Linkedlist {
	node<type> *head {}, *tail {};
	size_t counter {};

public:
	Linkedlist() { }
	~Linkedlist() { Destroy(); }

	Linkedlist(const Linkedlist&) = delete;	// Copy constructor needed
	Linkedlist& operator=(const Linkedlist&) = delete;	// Operator= needed

	bool isEmpty() { return head == nullptr; }	// OR return counter == 0;

	type getLast() {
		if (isEmpty()) {
			cout << "Linked list is empty !!!\n";
			assert(!isEmpty());
		}

		return tail->info;
	}

	void Append(const type& e) {
		const auto newnode {new node<type> (e)};

		if (isEmpty())
			head = newnode;
		else {
			tail->link = newnode;
		}

		tail = newnode;
		++counter;
	}

	void print() {
		if (isEmpty()) {
			cout << "Linked list is empty !!!\n";
			return;
		}

		for (auto current {head}; current != nullptr; current = current->link)
			cout << (current != head ? "," : "") << current->info;

		cout << '\n';
	}

	void Destroy() {
		for (auto temp {head}; head != nullptr; head = temp) {
			temp = head->link;
			delete head;
		}

		tail = nullptr;
		counter = 0;
	}

	bool search(const type& e) {
		for (auto current {head}; current != nullptr; current = current->link)
			if (current->data == e)
				return true;

		return false;
	}
};

Topic archived. No new replies allowed.