-
Notifications
You must be signed in to change notification settings - Fork 0
/
List.h
92 lines (80 loc) · 1.91 KB
/
List.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
//
// Created by Macbook Pro on 10/12/2019.
//
#ifndef OP_PROJECT2_LIST_H
#define OP_PROJECT2_LIST_H
#pragma once
#include "Elem.h"
#include "String.h"
#include "Pair.h"
template <typename T>
class List {
public:
List();
void addElem(T &);
Elem<T>* getRoot();
Elem<T>* getTail();
int getSize();
Elem<T>* search(List<T> & list, T& item) {
Elem<T> * curr = list.root_;
while (curr != nullptr) {
if (curr->getValue() == item){
return curr;
} else if (curr->getValue() > item) {
return curr;
} else {
curr = curr->getNext();
}
}
return curr;
}
private:
Elem<T>* root_;
Elem<T>* tail_;
int size_;
};
template<typename T>
void List<T>::addElem(T & elem) {
if (size_ == 0) {
root_ = new Elem<T>(nullptr, nullptr, elem);
tail_= root_;
size_++;
} else {
Elem<T> * result = search(*this, elem);
if (result == nullptr) {
tail_->getNext() = new Elem<T>(nullptr, tail_, elem);
tail_ = tail_->getNext();
size_++;
} else if (result->getValue() == elem) {
result->getCount()++;
} else if (result == root_){
root_->getPrev() = new Elem<T>(root_, nullptr, elem);
root_ = root_->getPrev();
size_++;
} else{
Elem<T> * tmp = new Elem<T>(result, result->getPrev(), elem);
result->getPrev()->getNext() = tmp;
result->getPrev() = tmp;
size_++;
}
}
}
template<typename T>
List<T>::List() {
root_ = nullptr;
tail_ = nullptr;
size_ = 0;
}
template<typename T>
Elem<T> *List<T>::getRoot() {
return root_;
}
template<typename T>
Elem<T> *List<T>::getTail() {
return tail_;
}
template<typename T>
int List<T>::getSize() {
return size_;
}
#endif //OP_PROJECT2_LIST_H