school

thing1's amazing school repo
Log | Files | Refs | Submodules | README

ll.c (943B)


      1 #include <stdlib.h>
      2 #include <stdio.h>
      3 #include <string.h>
      4 
      5 typedef struct ll_t ll_t;
      6 
      7 typedef struct ll_t {
      8 	void *data;
      9 	ll_t *next;
     10 } ll_t;
     11 
     12 ll_t *llalloc(){
     13 	ll_t *output = malloc(sizeof(ll_t));
     14 	if (output == NULL)
     15 		return NULL;
     16 	output->next = NULL;
     17 	return output;
     18 }
     19 
     20 void llsetdata(ll_t *node, void *data){
     21 	node->data = malloc(sizeof(data));
     22 	memcpy(node->data, data, sizeof(data));
     23 }
     24 
     25 void llsetnext(ll_t *node, ll_t *next){
     26 	if (node->next == NULL)
     27 		node->next = next;
     28 	else
     29 		llsetnext(node->next, next);
     30 }
     31 
     32 void *llgetat(ll_t *head, int index){
     33 	if (index == 0)
     34 		return head->data;
     35 	else {
     36 		if (head->next != NULL)  {
     37 			return llgetat(head->next, index - 1);
     38 		}else {
     39 			return NULL;
     40 		}
     41 	}
     42 }
     43 
     44 ll_t *llgetendnode(ll_t *head){
     45 	if (head->next == NULL)
     46 		return head;
     47 	else 
     48 		return llgetendnode(head->next);
     49 }
     50 
     51 void llfreeall(ll_t *head){
     52 	if (head->next != NULL)
     53 		llfreeall(head->next);
     54 	free(head->data);
     55 	free(head);
     56 }
     57