doublelist.c
1 #define _CRT_SECURE_NO_WARNINGS
2 #include "doublelist.h"
3 
4 #include <string.h>
5 #include <stdlib.h>
6 
7 /*****************************************************************************/
8 /* Item & Primitives */
9 /*****************************************************************************/
10 
12  return NULL;
13 }
14 
16  Item *n = malloc(sizeof(Item));
17  n->value = ElemCopy(e);
18  n->next = i;
19  n->prev = NULL;
20  if (!DListIsEmpty(i)) {
21  i->prev = n;
22  }
23  return n;
24 }
25 
26 bool DListIsEmpty(const Item *i) {
27  return i == NULL;
28 }
29 
30 const ElemType *DListGetHeadValue(const Item *i) {
31  if (DListIsEmpty(i)) {
32  printf("ERROR: Alla funzione 'DListGetHead()' e' stata passata una lista vuota (NULL).\n");
33  exit(1);
34  }
35  else {
36  return &i->value;
37  }
38 }
39 
40 Item *DListGetTail(const Item *i) {
41  if (DListIsEmpty(i)) {
42  printf("ERROR: Alla funzione 'ListGetTail()' e' stata passata una lista vuota (NULL).\n");
43  exit(2);
44  }
45  else {
46  return i->next;
47  }
48 }
49 
50 Item* DListGetPrev(const Item* i) {
51  if (DListIsEmpty(i)) {
52  printf("ERROR: Alla funzione 'DListGetHead()' e' stata passata una lista vuota (NULL).\n");
53  exit(3);
54  }
55  else {
56  return i->prev;
57  }
58 }
59 
61 
63 
64  if (DListIsEmpty(i)) {
65  return n;
66  }
67 
68  Item *tmp = i;
69  while (!DListIsEmpty(DListGetTail(tmp))) {
70  tmp = DListGetTail(tmp);
71  }
72 
73  tmp->next = n;
74  n->prev = tmp;
75  return i;
76 }
77 
78 static const Item* DListGetFirst(const Item* i) {
79  if (DListIsEmpty(i)) {
80  return NULL;
81  }
82 
83  while (!DListIsEmpty(i->prev)) {
84  i = i->prev;
85  }
86 
87  return i;
88 }
89 
90 void DListDelete(Item *i) {
91 
92  i = (Item*)DListGetFirst(i);
93 
94  while (!DListIsEmpty(i)) {
95  Item *tmp = i;
96  i = i->next;
97  ElemDelete(&tmp->value);
98  free(tmp);
99  }
100 }
101 
102 /*****************************************************************************/
103 /* Non Primitives */
104 /*****************************************************************************/
105 
106 void DListWrite(const Item *i, FILE *f) {
107 
108  i = DListGetFirst(i);
109 
110  fprintf(f, "[");
111  while (!DListIsEmpty(i)) {
112  ElemWrite(&i->value, f);
113  i = DListGetTail(i);
114  if (!DListIsEmpty(i)) {
115  fprintf(f, ", ");
116  }
117  }
118  fprintf(f, "]\n");
119 }
120 
121 void DListWriteStdout(const Item *i) {
122  DListWrite(i, stdout);
123 }
ElemType
int ElemType
Definizione di struct ElemType.
Definition: elemtype.h:13
Item::prev
struct Item * prev
Definition: doublelist.h:23
DListInsertBack
Item * DListInsertBack(Item *i, const ElemType *e)
La funzione DListInsertBack() aggiunge un elemento in coda ad una lista (anche vuota) e ritorna la li...
Definition: doublelist.c:60
ElemCopy
ElemType ElemCopy(const ElemType *e)
La funzione ElemCopy() crea e ritorna una copia dell'elemento dato.
Definition: elemtype.c:13
ElemDelete
void ElemDelete(ElemType *e)
La funzione ElemDelete() libera la memoria occupata dall'elemento specificato.
Definition: elemtype.c:23
ElemWrite
void ElemWrite(const ElemType *e, FILE *f)
La funzione ElemWrite() stampa un elemento su file.
Definition: elemtype.c:39
DListGetTail
Item * DListGetTail(const Item *i)
La funzione DListGetTail() ritorna la lista privata dell'elemento in testa. La funzione NON dealloca ...
Definition: doublelist.c:40
DListIsEmpty
bool DListIsEmpty(const Item *i)
La funzione DListIsEmpty() verifica se una lista รจ vuota.
Definition: doublelist.c:26
DListWriteStdout
void DListWriteStdout(const Item *i)
La funzione DListWriteStdout() stampa la lista specificata su stdout. Nello specifico,...
Definition: doublelist.c:121
doublelist.h
Item::value
ElemType value
Definition: doublelist.h:21
DListDelete
void DListDelete(Item *i)
La funzione ListDelete() libera la memoria occupata da tutti gli elementi di una lista,...
Definition: doublelist.c:90
DListCreateEmpty
Item * DListCreateEmpty(void)
La funzione DListCreateEmpty() crea e ritorna una lista vuota, ovvero NULL.
Definition: doublelist.c:11
DListWrite
void DListWrite(const Item *i, FILE *f)
La funzione DListWrite() stampa la lista specificata su file. Nello specifico, la funzione stampa il ...
Definition: doublelist.c:106
DListGetPrev
Item * DListGetPrev(const Item *i)
La funzione DListGetPrev() ritorna il puntatore all'elemento precedente.
Definition: doublelist.c:50
Item
Definizione del tipo struct Item.
Definition: doublelist.h:20
Item::next
struct Item * next
Definition: doublelist.h:22
DListInsertHead
Item * DListInsertHead(const ElemType *e, Item *i)
La funzione DListInsertHead() aggiunge un nuovo elemento in testa ad una lista e ritorna il puntatore...
Definition: doublelist.c:15
DListGetHeadValue
const ElemType * DListGetHeadValue(const Item *i)
La funzione DListGetHead() ritorna un puntatore all'elemento in testa alla lista, senza rimuoverlo.
Definition: doublelist.c:30