-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist.cpp
More file actions
74 lines (60 loc) · 1.3 KB
/
list.cpp
File metadata and controls
74 lines (60 loc) · 1.3 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
#include <iostream>
using namespace std;
typedef struct list {
int maxSize; int listSize; int curr; int* listArray;
} List;
List* create_list(int size) {
List* l = (List*) new int[size]; // precisa??
l->maxSize = size;
l->listSize = l->curr = 0;
l->listArray = new int[size];
return l;
}
void insert(List* l, int it) {
if(l->listSize >= l->maxSize) {
return; // Error
}
int i = l->listSize;
while(i > l->curr) {
l->listArray[i] = l->listArray[i - 1]; // shift right
i--;
}
l->listArray[l->curr] = it;
l->listSize++;
}
void remove(List* l) {
if(l->curr < 0 || l->curr >= l->listSize) {
return; // NULL??
}
int it = l->listArray[l->curr];
int i = l->curr;
while(i < ((l->listSize) - 1)) {
l->listArray[i] = l->listArray[i + 1]; // shift left
i++;
}
l->listSize--;
}
void moveToStart(List* l) {
l->curr = 0;
}
void moveToEnd(List* l) {
l->curr = l->listSize;
}
void prev(List* l) {
if(l->curr != 0) {
l->curr--;
}
}
void next(List* l) {
if(l->curr < l->listSize) {
l->curr++;
}
}
void printList(List* l) {
for(int i = 0; i < l->listSize; i++) {
cout << l->listArray[i] ;
}
}
void currentpos(List* l) {
cout << l->curr << endl;
}