-
Notifications
You must be signed in to change notification settings - Fork 0
/
List.cpp
142 lines (120 loc) · 1.68 KB
/
List.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
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
#include "List.hpp"
#include "ListItem.hpp"
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
/**
*
*/
void List::Add(ListItem *li)
{
if (this->first == nullptr)
{
this->first = li;
this->last = li;
}
else
{
this->last->SetNext(li);
this->last = li;
}
++count;
}
/**
*
*/
unsigned long List::Count()
{
return count;
}
/**
*
*/
ListItem *List::Get(unsigned long index)
{
if (index >= this->count)
{
throw invalid_argument("Valeur hors de la liste");
}
ListItem *li;
for (unsigned long i = 0; i <= index; ++i)
{
if (i == 0)
{
li = this->first;
}
else
{
li = li->Next();
}
}
return li;
}
/**
*
*/
void List::Remove(unsigned long index)
{
if (index >= this->count)
{
throw invalid_argument("Valeur hors de la liste");
}
if (this->count == 1)
{
this->first = nullptr;
this->last = nullptr;
}
else if (index == 0)
{
this->first = this->first->Next();
}
else
{
ListItem *li = this->Get(index - 1);
li->SetNext(li->Next()->Next());
}
--this->count;
}
/**
*
*/
bool List::IsSorted()
{
if (count < 2)
{
return true;
}
bool sorted = true;
ListItem *li = this->first;
ListItem *next = this->first->Next();
while (next != nullptr)
{
if (li->Value() > next->Value())
{
sorted = false;
break;
}
li = next;
next = li->Next();
}
return sorted;
}
/**
*
*/
void List::Show()
{
if (count == 0)
{
return;
}
ListItem *li = first;
cout << li->Value();
while (li->Next() != nullptr)
{
li = li->Next();
cout << " < " << li->Value();
}
cout << endl;
}