-
Notifications
You must be signed in to change notification settings - Fork 0
/
SortedLi.h
180 lines (143 loc) · 2.95 KB
/
SortedLi.h
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
* SortedList.h
*
* Created on: Jun 20, 2021
* Author: OS1
*/
#ifndef SORTEDLI_H_
#define SORTEDLI_H_
template<class T>
class SortedList{
public:
struct Node{
public:
T * tt;
Node * next;
Node(T * t, Node * n = 0){
tt = t;
next = n;
}
};
Node * head;
Node * tail;
Node * curr;
void add(T * t){ //add + sort
Node * newNode = new Node(t);
if(!head) head = tail = newNode;
else if(*t <= *head->tt){
newNode->next = head;
head = newNode;
}
else {
for(toHead(); hasCurr() && hasCurrNext() && *t >= *(curr->next->tt); toNext()){
//
}
if(curr == tail){
tail->next = newNode;
tail = newNode;
}
else {
newNode->next = curr->next;
curr->next = newNode;
}
}
}
/*void remove(T* t){ //brisemo element - TESTIRAJ
if(head){
if(this->count() == 1){
Node * deleted = head; //TESTIRAJ
head = tail = 0;
deleted->tt = 0;
delete deleted;
return;
}
Node * curr = head; //TESTIRAJ
Node * prev = 0;
Node * next = head->next;
while(curr != 0 && curr->tt != t){
prev = curr;
curr = next;
next = curr->next;
}
if(!curr) return; //ako nema
Node * deleted = curr; //TESTIRAJ
if(deleted == head) head = next;
if(deleted == tail) tail = prev;
if(prev) prev->next = next;
deleted->tt = 0;
delete deleted;
}
//return removedT; ako treba obrisani pcb odkomentarisati
}*/
void remove(){ //s pocetka brisemo
//T * removedT = 0; //ako nista ne brisemo, nemamo ni pcb koji brisemo
if(head){
Node * deleted = head;
//removedT = deleted->tt;
head = head->next;
if(!head) head = tail = 0;
deleted->tt = 0; //ne zelimo da obrisemo pcb !!!
delete deleted;
}
//return removedT; ako treba obrisani pcb odkomentarisati
}
void removeFromEnd(){ //s kraja brisemo
if(!head) return;
Node * temp = head;
Node * prev = 0;
while(temp->next){
prev = temp;
temp = temp->next;
}
Node * deleted = tail;
//removedT = deleted->tt;
tail = prev;
if(!tail) head = tail = 0;
deleted->tt = 0;
delete deleted;
tail->next = 0;
}
int count(){
Node * temp = head;
int counter = 0;
while(temp){
counter++;
temp = temp->next;
}
return counter;
}
T * getHead() {
return head->tt;
}
T * getTail() {
return head->tt;
}
void toHead() {
curr = head;
}
void toNext(){
if(curr) curr=curr->next;
}
int hasCurr(){
return curr != 0;
}
int hasCurrNext(){
return curr->next != 0;
}
T * getPointer(){
if(!curr) return 0;
return curr->tt;
}
SortedList(){
head = tail = curr = 0;
}
virtual ~SortedList(){
/*while(head != 0){
Node * old = head;
head = head->next;
delete old;
}
tail = 0;*/
}
};
#endif /* SORTEDLI_H_ */