-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.cpp
More file actions
143 lines (118 loc) · 2.86 KB
/
Copy pathLinkedList.cpp
File metadata and controls
143 lines (118 loc) · 2.86 KB
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
//
// lecture09_Node_LL.cpp Copy Constructor
//
#include <iostream>
using namespace std;
class ListNode {
public:
ListNode();
ListNode(int iData);
ListNode(int iData, ListNode* iNext);
//mutators
void setData(int);
void setNext(ListNode*);
//accessors
int getData(void) const;
ListNode* getNext(void) const;
private:
int data;
ListNode* next;
};
class LinkedList {
public:
LinkedList();
LinkedList(ListNode* iHead);
//initialize the list with an int array
LinkedList(const int* arr, int arrSize);
//initialize the list with an exising list
LinkedList(const LinkedList &l);
~LinkedList();
//pre-append
void preAppend(int nv);
ListNode* getHead(void) const;
void display(void) const;
private:
ListNode* head;
};
int main(int argc, char* argv[]) {
int arr[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
LinkedList arrList(arr, 10);
arrList.display();
LinkedList copyList(arrList);
ListNode* node2 = copyList.getHead();
node2->setData(-1);
copyList.display();
arrList.display();
return 0;
}
ListNode::ListNode() {
data = 0;
next = nullptr;
}
ListNode::ListNode(int iData) {
data = iData;
next = nullptr;
}
ListNode::ListNode(int iData, ListNode* iNext) {
data = iData;
next = iNext;
}
int ListNode::getData(void) const {
return data;
}
ListNode* ListNode::getNext(void) const {
return next;
}
void ListNode::setData(int newData) {
data = newData;
}
void ListNode::setNext(ListNode* newNext) {
next = newNext;
}
////////// Linked List //////////
LinkedList::LinkedList() {
head = nullptr;
}
LinkedList::LinkedList(ListNode* iHead) {
head = iHead;
}
LinkedList::LinkedList(const LinkedList& l) {
ListNode* head = l.head;
ListNode* currentNode = head;
while (currentNode != nullptr) {
currentNode->getData() = l.head->getData();
currentNode->getNext() = l.head->getNext();
}
currentNode->getNext() = nullptr;
}
LinkedList::~LinkedList() {
ListNode* currentNode = head;
ListNode* next;
while (head != NULL) {
next = head->getNext();
delete head;
head = next;
}
}
//initialize the list with an int array
LinkedList::LinkedList(const int* arr, int arrSize) {
head = nullptr;
for (int i = arrSize - 1; i >= 0; i--) {
preAppend(arr[i]);
}
}
ListNode* LinkedList::getHead(void) const {
return head;
}
void LinkedList::display(void) const {
ListNode* cursor = head;
while (cursor != nullptr) {
cout << cursor->getData() << " ";
cursor = cursor->getNext();
}
cout << endl;
}
void LinkedList::preAppend(int nv) {
ListNode* node = new ListNode(nv, head);
head = node;
}