Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "includes.h"
- typedef struct Node {
- struct Node* prev;
- int data;
- struct Node* next;
- } node;
- #define NODESIZE sizeof(struct Node)
- struct Node* node_init(int dat) {
- struct Node* newNode = (struct Node*)malloc(NODESIZE);
- newNode->data = dat;
- newNode->prev = NULL;
- newNode->next = NULL;
- return newNode;
- }
- typedef struct List {
- struct Node* head;
- struct Node* tail;
- } list;
- #define LISTSIZE sizeof(struct List)
- struct List* list_init(void) {
- struct List* newList = (struct List*)malloc(LISTSIZE);
- newList->head = NULL;
- newList->tail = NULL;
- return newList;
- }
- bool list_is_empty(struct List* l) {
- return l->head == NULL;
- }
- int list_get_length(struct List* l) {
- if (list_is_empty(l)) {
- return 0;
- }
- if (l->tail == NULL) {
- return 1;
- }
- int ecx = 0;
- struct Node* i = l->head;
- while (i->next != NULL) {
- ecx++;
- i = i->next;
- }
- return ecx;
- }
- void list_insert_between(struct Node* before, struct Node* newNode, struct Node* after) {
- newNode->next = after;
- newNode->prev = before;
- before->next = newNode;
- after->prev = newNode;
- return;
- }
- void list_append(struct List* l, int dat) {
- struct Node* newNode = node_init(dat);
- if (list_is_empty(l)) {
- l->head = newNode;
- return;
- }
- if (l->tail == NULL) {
- newNode->prev = l->head;
- l->head->next = newNode;
- l->tail = newNode;
- return;
- }
- l->tail->next = newNode;
- newNode->prev = l->tail;
- l->tail = newNode;
- return;
- }
- void list_prepend(struct List* l, int dat) {
- struct Node* newNode = node_init(dat);
- if (list_is_empty(l)) {
- l->head = newNode;
- return;
- }
- l->head->prev = newNode;
- newNode->next = l->head;
- l->head = newNode;
- return;
- }
- void list_destroy(struct List* l) {
- struct Node* h = l->head;
- struct Node* curr = l->head;
- struct Node* next;
- while (next != NULL) {
- next = curr->next;
- free(curr);
- curr = next;
- }
- free(l);
- return;
- }
- void list_print(struct List* l) {
- if (list_is_empty(l)) {
- printf("[lst prnt] [ ]\n");
- return;
- }
- struct Node* curr = l->head;
- printf("[lst prnt] [ ");
- while (curr != NULL) {
- printf("%d ", curr->data);
- curr = curr->next;
- }
- printf("]\n");
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement