-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLista2A.cpp
More file actions
134 lines (111 loc) · 2.53 KB
/
Lista2A.cpp
File metadata and controls
134 lines (111 loc) · 2.53 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
#include <iostream>
#define endl "\n"
using namespace std;
typedef struct link {
char 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, char item);
void remove(List* l);
void prev(List* l);
void next(List* l);
void printList(List* l);
Link* create_link(char it, Link* nextval);
Link* create_link(Link* nextval);
int main(void) {
List* l = create_list();
string input;
while(cin >> input) {
for(unsigned int i = 0; i < input.size(); i++) {
if(input[i] == '[') {
l->curr = l->head;
}
else if(input[i] == ']') {
l->curr = l->tail;
}
else {
insert(l, input[i]);
next(l);
}
}
printList(l);
cout << endl;
l->curr = l->head;
clear(l);
}
clear(l);
return 0;
}
Link* create_link(char it, Link* nextval) {
Link* n = (Link *) new char;
n->element = it;
n->next = nextval;
return n;
}
Link* create_link(Link* nextval) {
Link* n = (Link *) new char;
n->next = nextval;
return n;
}
List* create_list() {
List* l = (List *) new char;
l->curr = l->tail = l->head = create_link(NULL); // header node
l->cnt = 0;
return l;
}
void clear(List* l) {
while(l->cnt != 0) {
remove(l);
}
}
void insert(List* l, char item) {
l->curr->next = create_link(item, l->curr->next);
if(l->tail == l->curr) {
l->tail = l->curr->next;
}
l->cnt++;
}
void remove(List* l) {
Link* tmp = l->curr->next;
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--;
delete tmp;
//return it;
}
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) {
Link *tmp = l->head;
while(tmp->next != NULL){
cout << tmp->next->element;
tmp = tmp->next;
}
}