Difficulties with Linked List assignment

In file "LinkedList.cpp", you are given a layout of singly linked list in C++, and a main function (for testing) as well. You are to complete the code and get it work correctly.
To be specific, you need to complete the definition of the following functions:
a. LinkedList::LinkedList(LinkedList& ll)
b. LinkedList& LinkedList::operator=(LinkedList& ll)
c. Node* LinkedList::insert_after_current(int value)
d. void LinkedList::delete_after_current()

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

using namespace std;

struct Node {
int data;
Node* next;
Node(int d=0, Node* n=0) : data(d), next(n) {}
};

class LinkedList {
private:
Node* head;
Node* curr;
public:
LinkedList() : head(0), curr(0) {}
LinkedList(LinkedList& ll);
LinkedList& operator=(LinkedList& ll);
~LinkedList();
Node* insert_after_current(int value);
void delete_after_current();
void display_list();
void operator++() { curr = curr->next; }
void front() { curr = head; }
bool is_curr_nonnull() const { return curr != 0; }
};

LinkedList::LinkedList(LinkedList& ll)
{
// Please add codes here
}

LinkedList& LinkedList::operator=(LinkedList& ll)
{
if (this == &ll) return *this;

Node *p, *node = head;
while (node != 0) {
p = node;
node = node->next;
delete p;
}

// Please add codes here

return *this;
}


LinkedList::~LinkedList()
{
Node *p, *node = head;
while (node != 0) {
p = node;
node = node->next;
delete p;
}
}


Node* LinkedList::insert_after_current(int value) {
Node *newnode = new Node(value);
if (curr == 0) {
if (head != 0)
error("Invalid current pointer");
else
head = curr = newnode;
} else {
// Please add codes here
}
return curr;
}

void LinkedList::delete_after_current(){
if (curr == 0) {
if (head != 0)
error("Invalid current pointer");
} else {
// Please add codes here
}
}

void LinkedList::display_list() {
Node *node = head;
while(node != 0) {
cout << node->data << " ";
node = node->next;
}
cout << '\n';
}

int main(){
LinkedList L1;
L1.insert_after_current(10);
L1.insert_after_current(20);
L1.insert_after_current(30);
L1.insert_after_current(40);
L1.display_list();
L1.front();
L1.delete_after_current();
cout << "list L1:\n";
L1.display_list();
LinkedList L2(L1);
cout << "list L2:\n";
L2.display_list();
LinkedList L3;
cout << "list L3:\n";
L3.insert_after_current(40);
L3.display_list();
L3 = L2;
L3.display_list();
return 0;
}
Topic archived. No new replies allowed.