-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.c
179 lines (144 loc) · 4.12 KB
/
list.c
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/******************************************************************************
*
* File Name: lista.c
* (c) 2009 AED
* Authors: AED Team
* Last modified: ACR 2009-03-23
* Revision: v2.0
*
* COMMENTS
* implements functions for type t_listaPalavra
*
*****************************************************************************/
/*
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
*/
#include "list.h"
#include "prototipos.h"
/* Linked list */
struct _t_lista {
Item this;
struct _t_lista *prox;
};
/******************************************************************************
* iniLista ()
*
* Arguments: none
* Returns: t_lista *
* Side-Effects: list is initialized
*
* Description: initializes list
*
*****************************************************************************/
t_lista *iniLista(void) {
return NULL;
}
/******************************************************************************
* criaNovoNoLista ()
*
* Arguments: nome - Item to save in list node
* Returns: t_lista *
* Side-Effects: none
*
* Description: creates and returns a new node that can later be added to the
* list
*
*****************************************************************************/
t_lista *criaNovoNoLista (t_lista* lp, Item this, int *err) {
t_lista *novoNo;
novoNo = (t_lista*) x_malloc(sizeof(t_lista));
if(novoNo!=NULL) {
novoNo->this = this;
novoNo->prox = lp;
lp = novoNo;
*err = 0;
} else {
*err = 1;
}
return lp;
}
/******************************************************************************
* getItemLista ()
*
* Arguments: this - pointer to element
* Returns: Item
* Side-Effects: none
*
* Description: returns an Item from the list
*
*****************************************************************************/
Item getItemLista (t_lista *p) {
return p -> this;
}
/******************************************************************************
* getProxElementoLista ()
*
* Arguments: this - pointer to element
* Returns: pointer to next element in list
* Side-Effects: none
*
* Description: returns a pointer to an element of the list
*
*****************************************************************************/
t_lista *getProxElementoLista(t_lista *p) {
return p -> prox;
}
/******************************************************************************
* numItensNaLista ()
*
* Arguments: lp - pointer to list
* Returns: count of the number of items in list
* Side-Effects: none
*
* Description: returns the number of items (nodes) in the list
*
*****************************************************************************/
int numItensNaLista(t_lista *lp) {
t_lista *aux; /* auxiliar pointers to travel through the list */
int conta = 0;
aux = lp;
for(aux = lp; aux != NULL; aux = aux -> prox)
conta++;
return conta;
}
/******************************************************************************
* libertaLista ()
*
* Arguments: lp - pointer to list
* Returns: (void)
* Side-Effects: frees space occupied by list items
*
* Description: free list
*
*****************************************************************************/
void libertaLista(t_lista *lp, void freeItem(Item)) {
t_lista *aux, *newhead; /* auxiliar pointers to travel through the list */
for(aux = lp; aux != NULL; aux = newhead) {
newhead = aux->prox;
freeItem(aux->this);
free(aux);
}
return;
}
/******************************************************************************
* InverteLista ()
*
* Arguments: lp - pointer to list
* Returns: nova cabeça da lista
* Side-Effects: none
*
* Description: inverte lista
*
*****************************************************************************/
t_lista * InverteLista( t_lista * l ) {
t_lista * novo = NULL, * prox = NULL;
while(l != NULL) {
prox=l->prox;
l->prox= novo;
novo = l;
l = prox;
}
return novo;
}