Skip to content

Commit b0d107c

Browse files
committed
add addBegin and addend
1 parent 5855a1c commit b0d107c

File tree

3 files changed

+64
-11
lines changed

3 files changed

+64
-11
lines changed

OneWayLinkedList.cpp

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,21 @@ OneWayLinkedList::OneWayLinkedList() {
2222
this->size = 0;
2323
}
2424

25-
// Add new node to the end of the list
26-
void OneWayLinkedList::add(int data) {
25+
// Add a new node to the beginning of the list
26+
void OneWayLinkedList::addBegin(int data) {
27+
Node* node = new Node(data);
28+
if(this->head == NULL) {
29+
this->head = node;
30+
this->tail = node;
31+
} else {
32+
node->next = this->head;
33+
this->head = node;
34+
}
35+
this->size++;
36+
}
37+
38+
// Add a new node to the end of the list
39+
void OneWayLinkedList::addEnd(int data) {
2740
Node* node = new Node(data);
2841
if(this->head == NULL) {
2942
this->head = node;
@@ -37,10 +50,32 @@ void OneWayLinkedList::add(int data) {
3750

3851
// Print all nodes
3952
void OneWayLinkedList::print() {
53+
if (this->head == NULL) {
54+
std::cout << "List is empty" << std::endl;
55+
return;
56+
}
57+
4058
Node* node = this->head;
41-
while(node != NULL) {
59+
while (node != NULL) {
4260
std::cout << node->data << std::endl;
4361
node = node->next;
4462
}
4563
}
4664

65+
// Delete all nodes
66+
void OneWayLinkedList::deleteAll() {
67+
Node* node = this->head;
68+
while (node != NULL) {
69+
Node* next = node->next;
70+
delete node;
71+
node = next;
72+
}
73+
this->head = NULL;
74+
this->tail = NULL;
75+
this->size = 0;
76+
}
77+
78+
// Support delete operator
79+
// OneWayLinkedList::~OneWayLinkedList() {
80+
// this->deleteAll();
81+
// }

OneWayLinkedList.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,18 @@ class OneWayLinkedList {
2323
// OneWayLinkedList Constructor
2424
OneWayLinkedList();
2525

26+
// Add new node to the beginning of the list
27+
void addBegin(int data);
28+
2629
// Add new node to the end of the list
27-
void add(int data);
28-
30+
void addEnd(int data);
31+
2932
// Print all nodes
3033
void print();
34+
35+
// Delete all nodes
36+
void deleteAll();
37+
38+
// Support delete operator
39+
// ~OneWayLinkedList();
3140
};

example.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,25 @@
44
using namespace std;
55

66
int main(int argc, char** argv) {
7+
// Create new list
78
OneWayLinkedList* list = new OneWayLinkedList();
8-
list->add(1);
9-
list->add(2);
10-
list->add(3);
11-
list->add(4);
12-
list->add(5);
9+
// Add some nodes
10+
list->addEnd(1);
11+
list->addEnd(2);
12+
list->addEnd(3);
13+
list->addEnd(4);
14+
list->addEnd(5);
15+
list->addEnd(110);
16+
list->addBegin(100);
1317

18+
// Print all nodes
1419
list->print();
1520

16-
delete list;
21+
// Delete all nodes
22+
list->deleteAll();
23+
24+
// Print all nodes
25+
list->print();
1726

1827
return 0;
1928
}

0 commit comments

Comments
 (0)