-
Notifications
You must be signed in to change notification settings - Fork 2
/
SinglelyLinkedList.cpp
82 lines (75 loc) · 1.66 KB
/
SinglelyLinkedList.cpp
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
#include "DataStructAndAlgorithm.h"
#include "SinglelyLinkedList.h"
LinkedListNode::LinkedListNode(int _data) : next(nullptr), data(_data) {
}
LinkedListNode* SinglelyLinkedList::find(int index) {
if (head == NULL) {
return NULL;
}
LinkedListNode *current_node = head;
int count = 0;
while (current_node != NULL&&count != index) {
current_node = current_node->next;
count++;
}
if (count == index) {
return current_node;
}
return NULL;
}
void SinglelyLinkedList::insert(LinkedListNode *node, int index) {
if (head == NULL) {
head = node;
return;
}
if (index == 0) {
node->next = head;
head = node;
}
LinkedListNode *pre_node = find(index - 1);
if (pre_node != NULL) {
node->next = pre_node->next;
pre_node->next = node;
}
}
void SinglelyLinkedList::remove(int index) {
if (head == NULL) {
return;
}
LinkedListNode *delete_node = NULL;
if (index == 0) {
delete_node = head;
head = head->next;
delete delete_node;
return;
}
LinkedListNode *pre_node = find(index - 1);
delete_node = pre_node->next;
pre_node->next = delete_node->next;
delete delete_node;
}
void SinglelyLinkedList::reverse() {
if (head == NULL) {
return;
}
LinkedListNode *current_node = head->next;
LinkedListNode *next_node = NULL;
head->next = NULL;
while (current_node != NULL) {
next_node = current_node->next;
current_node->next = head;
head = current_node;
current_node = next_node;
}
}
void SinglelyLinkedList::output() {
if (head == NULL) {
cout << "Linked List is NULL" << endl;
}
LinkedListNode *current_node = head;
while (current_node != NULL) {
cout << current_node->data << " ";
current_node = current_node->next;
}
cout << endl;
}