-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkTest.h
115 lines (112 loc) · 2.14 KB
/
LinkTest.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
const int defaultSize = 0;
template<class Elem>class Link{
public:
Elem element;
Link*next;
Link(const Elem&elemval, Link*nextval = NULL){
element = elemval;
next = nextval;
}
Link(Link*nextval = NULL){
next = nextval;
}
};
template<class Elem>class LList :public List<Elem>{
private:
Link<Elem>*head;
Link<Elem>*tail;
Link<Elem>*fence;
int leftcnt;
int rightcnt;
void init(){
fence = tail = head = new Link<Elem>;
leftcnt = rightcnt = 0;
}
void removeall(){
while (head!=NULL)
{
fence = head;
head = head->next;
delete fence;
}
}
public:
LList(int size = defaultSize){
init();
}
~LList(){
removeall();
}
void clear(){ removeall(); init(); }
bool insert(const Elem&);
bool append(const Elem&);
bool remove(Elem&);
void setStart(){
fence = head; rightcnt += leftcnt; leftcnt = 0;
}
void setEnd(){
fence = tail; leftcnt += rightcnt; rightcnt = 0;
}
void prev();
void next(){
if (fence != tail){
fence = fence->next;
rightcnt--;
leftcnt++;
}
}
int leftLength()const{
return leftcnt;
}
int rightLength()const{
return rightcnt;
}
bool setPos(int pos);
bool getValue(Elem&it)const{
if (rightLength() == 0)return false;
it = fence->next->element;
return true;
}
void print() const;
};
template<class Elem>
bool LList<Elem>::insert(const Elem&item){
fence->next = new Link<Elem>(item, fence->next);
if (tail == fence)
tail = fence->next;
rightcnt++;
//this->print();
return true;
}
template<class Elem>
bool LList<Elem>::append(const Elem&item){
//tail = tail->next
return true;
}
template<class Elem>
bool LList<Elem>::remove(Elem&it){
return true;
}
template<class Elem>
void LList<Elem>::prev(){
}
template<class Elem>
bool LList<Elem>::setPos(int pos){
return true;
}
template<class Elem>
void LList<Elem>::print()const{
Link<Elem>*temp = head;
cout << "<";
while (temp != fence){
cout << temp->next->element << " ";
temp = temp->next;
}
cout << "| ";
while (temp->next!=NULL)
{
cout << temp->next->element << " ";
temp = temp->next;
}
cout << ">\n";
}