-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkedlist.cpp
More file actions
162 lines (135 loc) · 3.3 KB
/
linkedlist.cpp
File metadata and controls
162 lines (135 loc) · 3.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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#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;
List* create_list(); // Creates a new list // Lembrar do 'int size' como parametro para array list
void clear(List* l);
void insert(List* l, int item);
void remove(List* l);
void moveToStart(List* l);
void moveToEnd(List* l);
void prev(List* l);
void next(List* l);
void printList(List* l);
Link* create_link(int it, Link* nextval);
Link* create_link(Link* nextval);
int main() {
List* l = create_list();
int comando = 0;
do { // MENU
cout << "=============MENU=============\n";
cout << "1) Insert\n2) Remove\n3) Move to Start\n4) Move to End\n5) Previous\n6) Next\n7) Print List\n8) Clear\n9) Leave\n.: ";
cin >> comando;
switch(comando) {
case 1:
int it;
cout << "Digite o elemento a ser inserido na lista: ";
cin >> it;
insert(l, it);
break;
case 2:
remove(l);
break;
case 3:
moveToStart(l);
break;
case 4:
moveToEnd(l);
break;
case 5:
prev(l);
break;
case 6:
next(l);
break;
case 7:
printList(l);
break;
case 8:
clear(l);
break;
case 9:
cout << "Leaving..." << endl;
break;
default:
cout << "Opcao Invalida" << endl;
break;
}
} while(comando != 9);
return 0;
}
Link* create_link(int it, Link* nextval) {
Link* n = (Link *) new int;
n->element = it;
n->next = nextval;
return n;
}
Link* create_link(Link* nextval) {
Link* n = (Link *) new int;
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) {
for(int i = 0; i < l->cnt; i++) {
delete [] l;
}
l->cnt = 0;
l->curr = l->tail = l->head = create_link(NULL);
}
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++;
}
void remove(List* l) {
if(l->curr->next == NULL) {
return;
}
//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) {
cout << l->tail->element << endl;
}