-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinklist.cpp
More file actions
93 lines (73 loc) · 1.5 KB
/
linklist.cpp
File metadata and controls
93 lines (73 loc) · 1.5 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
#include <iostream>
using namespace std;
typedef struct link {
int element; // E type, value stored in this link/node
struct link* next; // Reference to the next link/node
} Link;
typedef struct list {
Link* head;
Link* tail;
Link* curr;
int cnt; // list size
} List;
Link* create_link(int it, Link* nextval) {
Link* n;
n->element = it;
n->next = nextval;
return n;
}
Link* create_link(Link* nextval) {
Link* n;
n->next = nextval;
return n;
}
List* create_list() {
List* l = (List *) new int;
l->curr = l->tail = l->head = create_link(NULL); // header node
l->cnt = 0;
return l;
}
void clear(List* l) {
}
void insert(List* l, int it) {
l->curr->next = create_link(it, l->curr->next);
if(l->tail == l->curr) {
l->tail = l->curr->next;
}
l->cnt++;
}
int remove(List* l) {
if(l->curr->next == NULL) {
return -1;
}
int it = l->curr->next->element;
if(l->tail == l->curr->next) {
l->tail = l->curr;
}
l->curr->next = l->curr->next->next;
l->cnt--;
return it;
}
void moveToStart(List* l) {
l->curr = l->head;
}
void moveToEnd(List* l) {
l->curr = l->tail;
}
void prev(List* l) {
if(l->curr == l->head) {
return;
}
Link* temp = l->head;
while(temp->next != l->curr) {
temp = temp->next;
}
l->curr = temp;
}
void next(List* l) {
if(l->curr != l->tail) {
l->curr = l->curr->next;
}
}
void printList(List* l) {
}