utils

old (and slightly embarrassing) C utility scripts
Log | Files | Refs | README | LICENSE

utilllist.h (1546B)


      1 #pragma once
      2 
      3 #include <stdlib.h>
      4 
      5 typedef struct fNode{
      6   struct fNode* next;
      7   struct fNode* prev;
      8   float val;
      9 }fNode;
     10 
     11 typedef struct fLList{
     12   fNode* first;
     13   fNode* last;
     14   int n;
     15 }fLList;
     16 
     17 fLList* initfLList(){
     18   fLList* ret = malloc(sizeof(fLList));
     19   ret->first = NULL;
     20   ret->last = NULL;
     21   ret->n = 0;
     22   return ret;
     23 }
     24 
     25 int appendfll(fLList* a0, float a1){
     26   fNode* temp = malloc(sizeof(fNode));
     27   temp->val = a1;
     28   temp->next = NULL;
     29   temp->prev = a0->last;
     30   if(a0->n == 0) a0->first = temp;
     31   else a0->last->next = temp;
     32   a0->n = a0->n + 1;
     33   a0->last = temp;
     34   return 0;
     35 }
     36 
     37 int prependfll(fLList* a0, float a1){
     38   fNode* temp = malloc(sizeof(fNode));
     39   temp->val = a1;
     40   temp->next = a0->first;
     41   temp->prev = NULL;
     42   if(a0->n == 0) a0->last = temp;
     43   else a0->first->prev = temp;
     44   a0->n = a0->n + 1;
     45   a0->first = temp;
     46   return 0;
     47 }
     48 
     49 int rmFirst(fLList* a0){
     50   if(a0->n == 0) return 0;
     51   else if(a0->n == 1){
     52     free(a0->first);
     53     a0->first = NULL;
     54     a0->last = NULL;
     55   }else{
     56     fNode* temp = a0->first;
     57     a0->first = temp->next;
     58     a0->first->prev = NULL;
     59     temp->next = NULL;
     60     free(temp);
     61   }
     62   a0->n = a0->n - 1;
     63   return 0;
     64 }
     65 
     66 int rmLast(fLList* a0){
     67   if(a0->n == 0) return 0;
     68   else if(a0->n == 1){
     69     free(a0->first);
     70     a0->first = NULL;
     71     a0->last = NULL;
     72   }else{
     73     fNode* temp = a0->last;
     74     a0->last = temp->prev;
     75     a0->last->next = NULL;
     76     temp->prev = NULL;
     77     free(temp);
     78   }
     79   a0->n = a0->n - 1;
     80   return 0;
     81 }
     82 
     83 int freefLList(fLList* a0){
     84   while(a0->n > 0) rmLast(a0);
     85   return 0;
     86 }

Generated using stagit (https://codemadness.org/stagit.html)