-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpartition.cpp
114 lines (103 loc) · 1.72 KB
/
partition.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
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>
using namespace std;
class Node{
public:
Node(){
next = NULL;
}
//void setValue(int value){
// data = value;
//}
//int getValue(){
// return data;
//}
int data;
Node* next;
};
class LinkedList{
public:
LinkedList(){
head = NULL;
}
void printAll();
void insert(int);
void setHead(Node*);
Node* getHead();
private:
Node* head;
};
void LinkedList::setHead(Node* node){
this->head = node;
}
Node* LinkedList::getHead(){
return this->head;
}
void LinkedList::insert(int value){
Node* node = new Node;
if(head == NULL){
head = node;
}
else{
Node* iter = head;
while(iter->next != NULL){
iter = iter->next;
}
iter->next = node;
}
node->data = value;
node->next = NULL;
}
void LinkedList::printAll(){
Node* p;
p = head;
while(p != NULL){
cout << p->data << endl;
p = p->next;
}
}
void partition(LinkedList* linked_list, int x){
Node* p_node = linked_list->getHead();
Node* p_prev_node = NULL;
Node* p_head2 = NULL;
Node* p_tail2 = NULL;
while(p_node != NULL){
if(p_node->data < x){
if(p_head2 == NULL){
p_head2 = p_node;
p_tail2 = p_node;
}
else{
p_tail2->next = p_node;
p_tail2 = p_node;
}
if(p_prev_node == NULL){
linked_list->setHead(p_node->next);
}
else{
p_prev_node->next = p_node->next;
}
}
else{
p_prev_node = p_node;
}
p_node = p_node->next;
}
p_tail2->next = linked_list->getHead();
linked_list->setHead(p_head2);
}
int main(){
LinkedList* list = new LinkedList;
list->insert(1);
list->insert(9);
list->insert(2);
list->insert(6);
list->insert(4);
list->insert(3);
list->insert(8);
list->insert(9);
list->printAll();
cout<< endl << endl;
partition(list,5);
list->printAll();
return 0;
}