Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //basic.h
- #ifndef BASIC_H
- #define BASIC_H
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdlib.h>
- #include <stdio.h>
- #include <stdbool.h>
- #include <stdint.h>
- #include <string.h>
- typedef int element;
- typedef struct list_element {
- element value;
- struct list_element *next;
- }item;
- typedef item* list;
- extern list emptylist();
- extern void showList(list l);
- extern bool empty(list l);
- extern element head(list l);
- extern void* _abort();
- extern list tail(list l);
- extern list cons(element e, list l);
- extern list copyList(list l);
- extern void destroy(list l);
- extern list insert_FIFO(element e, list l);
- extern list toList(element *v, uint32_t size);
- extern element* toArray(list l, size_t *sz);
- extern list copyList(list l);
- extern list insert();
- #endif /*!BASIC_H*/
- //basic.c
- list emptylist()
- {
- return NULL;
- }
- bool empty(list l)
- {
- return (l == NULL);
- }
- element head(list l)
- {
- if (empty(l)) { _abort(); return EXIT_FAILURE; }
- return l->value;
- }
- void* _abort()
- {
- printf("\n\nIMPOSSIBILE ACCEDERE ALLA LISTA\n\n");
- return NULL;
- }
- list tail(list l)
- {
- if (empty(l))
- {
- _abort();
- return emptylist();
- }
- else
- return l->next;
- }
- list cons(element e, list l)
- {
- list t;
- t = malloc(sizeof(item));
- t->value = e;
- t->next = l;
- return t;
- }
- void showList(list l)
- {
- list temp = l;
- printf("[");
- while (temp != NULL)
- {
- printf("%d", temp->value);
- if (!empty(tail(temp))) printf(", ");
- temp = tail(temp);
- }
- printf("]");
- }
- list insert()
- {
- list l;
- uint32_t len;
- printf("Inserire il numero di elementi che si desidera inserire (il numero deve essere > di zero) : ");
- do
- {
- scanf("%d", &len);
- } while (len <= 0); printf("\n");
- l = NULL;
- for (uint32_t i = 0; i < len; i++)
- {
- int val;
- printf("\nInserire l'elemento n.%d : ", i + 1);
- scanf("%d", &val);
- l = cons(val, l);
- }
- return l;
- }
- void destroy(list l)
- {
- while (tail(l) != NULL)
- {
- list prossimo = tail(l);
- free(l);
- l = prossimo;
- }
- }
- list copyList(list l)
- {
- list temp = l, t = emptylist();
- while (!empty(temp))
- {
- t = insert_FIFO(head(temp), t);
- temp = tail(temp);
- }
- return t;
- }
- list insert_FIFO(element e, list l)
- {
- list l1 = l, t, start = l;
- t = cons(e, l);
- if (empty(l)) return t;
- while (!empty(l))
- {
- l1 = l;
- l = tail(l);
- }
- l1->next = t;
- t->next = emptylist();
- return start;
- }
- list toList(element *v, uint32_t size)
- {
- list l = emptylist();
- for (size_t i = 0; i < size; i++)
- l = cons(v[i], l);
- return l;
- }
- element *toArray(list l, size_t *sz)
- {
- element *v = NULL;
- size_t size = 0;
- list t = l;
- while (!empty(t))
- {
- v = realloc(v, (++size) * sizeof(element));
- v[size - 1] = head(t);
- t = tail(t);
- }
- *sz = size;
- return v;
- }
- int main(void)
- {
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment