linked_list.c
00001 #include <stddef.h>
00002 #include <stdio.h>
00003 #include <stdlib.h>
00004
00005
00006 struct cell {
00007 int data;
00008 struct cell * next;
00009 };
00010 typedef struct cell * list_t;
00011 typedef struct cell cell_t;
00012
00013
00014 list_t insert_in_head(int element, list_t L) {
00015
00016 cell_t * new_cell = (cell_t *) malloc(sizeof(cell_t));
00017 if (new_cell == NULL) return NULL;
00018 new_cell->data = element;
00019 new_cell->next = L;
00020
00021 return (list_t) new_cell;
00022 }
00023
00024
00025 void proc_insert_in_head(int element, list_t * L) {
00026 list_t* old = L;
00027 *L = insert_in_head(element, *L);
00028 if (L == NULL) *L = *old;
00029 }
00030
00031
00032 list_t insert_in_tail(int element, list_t L) {
00033
00034 cell_t * new_cell = (cell_t *) malloc(sizeof(cell_t));
00035 if (new_cell == NULL) return NULL;
00036 new_cell->data = element;
00037 new_cell->next = NULL;
00038
00039 if (L == NULL) return (list_t) new_cell;
00040
00041 list_t tmp = L;
00042 while (tmp->next != NULL) tmp = tmp->next;
00043
00044 tmp->next = (list_t) new_cell;
00045 return L;
00046 }
00047
00048
00049 void proc_insert_in_tail(int element, list_t * L) {
00050 list_t* old = L;
00051 *L = insert_in_tail(element, *L);
00052 if (L == NULL) *L = *old;
00053 }
00054
00055
00056 list_t delete_in_head(list_t L) {
00057 if (L == NULL) return L;
00058 list_t res = L->next;
00059 free(L);
00060 return res;
00061 }
00062
00063
00064 void proc_delete_in_head(list_t * L) {
00065 *L = delete_in_head(*L);
00066 }
00067
00068
00069 list_t delete_in_tail(list_t L) {
00070 if (L == NULL) return L;
00071 if (L->next == NULL) {
00072 free(L);
00073 return NULL;
00074 }
00075 list_t tmp = L->next;
00076 cell_t * prec = L;
00077 while(tmp->next != NULL) {
00078 prec = tmp;
00079 tmp = tmp->next;
00080 }
00081
00082 prec->next = NULL;
00083 free(tmp);
00084 return L;
00085 }
00086
00087
00088 void proc_delete_in_tail(list_t * L) {
00089 *L = delete_in_tail(*L);
00090 }
00091
00092
00093 void delete_list(list_t *L) {
00094 while(*L != NULL) proc_delete_in_head(L);
00095 }
00096
00097
00098 void print_list(list_t L) {
00099 while(L != NULL) {
00100 printf("%d -> ", L->data);
00101 L = L->next;
00102 }
00103 printf("NULL\n");
00104 }
00105
00106 list_t insert_in_head (int element, list_t L);
00107 list_t insert_in_tail (int element, list_t L);
00108 void proc_insert_in_head(int element, list_t * L);
00109 void proc_insert_in_tail(int element, list_t * L);
00110
00111 list_t delete_in_head (list_t L);
00112 list_t delete_in_tail (list_t L);
00113 void proc_delete_in_head(list_t * L);
00114 void proc_delete_in_tail(list_t * L);
00115
00116 void delete_list(list_t *L);
00117
00118 int main()
00119 {
00120
00121 list_t L = insert_in_head(11, insert_in_head(12, NULL));
00122 proc_insert_in_head(10, &L);
00123 L = insert_in_tail(14, insert_in_tail(13, L));
00124 proc_insert_in_tail(15, &L);
00125 printf("L = "); print_list(L);
00126
00127
00128 L = delete_in_head(L);
00129 proc_delete_in_head(&L);
00130 printf("Suppression de deux éléments en tête : L = ");
00131 print_list(L);
00132
00133
00134 L = delete_in_tail(L);
00135 proc_delete_in_tail(&L);
00136 printf("Suppression de deux éléments en queue : L = ");
00137 print_list(L);
00138
00139
00140 delete_list(&L);
00141 printf("Suppression de la liste : L = ");
00142 print_list(L);
00143 return 0;
00144 }
00145
00146
00147
00148