-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathList.h
More file actions
144 lines (114 loc) · 2.29 KB
/
List.h
File metadata and controls
144 lines (114 loc) · 2.29 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
144
/*
* List.h
*
* Created on: Jun 1, 2021
* Author: OS1
*/
#ifndef LIST_H_
#define LIST_H_
//class PCB;
template<class T>
class LList{
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){
if(!head) head = tail = new Node(t);
else {
tail->next = new Node(t);
tail = tail->next;
}
}
T* remove(T* t){ //brisemo element - TESTIRAJ
T * removedT = 0;
if(head){
if(this->count() == 1){
Node * deleted = head; //TESTIRAJ
head = tail = 0;
removedT = deleted->tt;
deleted->tt = 0;
delete deleted;
return removedT;
}
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 0; //ako nema
Node * deleted = curr; //TESTIRAJ
if(deleted == head) head = next;
if(deleted == tail) tail = prev;
if(prev) prev->next = next;
removedT = deleted->tt;
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
}
int count(){
Node * temp = head;
int counter = 0;
while(temp){
counter++;
temp = temp->next;
}
return counter;
}
T * getHead() {
return head->tt;
}
T * getCurr() {
return curr->tt;
}
void toHead() {
curr = head;
}
void toNext(){
if(curr) curr=curr->next;
}
int hasCurr(){
return curr != 0;
}
T * getPointer(){
if(!curr) return 0;
return curr->tt;
}
LList(){
head = tail = curr = 0;
}
virtual ~LList(){
/*while(head != 0){
Node * old = head;
head = head->next;
delete old;
}
tail = 0;*/
}
};
#endif /* LIST_H_ */