- #include <stdlib.h>
- #include <stdio.h>
- #include <stdbool.h>
- // some help func
- void
- debug(char* func, char* string)
- {
- printf("in -> [%s] :: %s",func,string);
- }
- // define Linked list
- typedef struct linked_list llist;
- typedef struct linked_list_node llnode;
- struct
- linked_list
- {
- int size;
- struct linked_list_node* first;
- struct linked_list_node* last;
- };
- struct
- linked_list_node
- {
- void* value;
- struct linked_list_node* next;
- };
- // Linked list functions
- // init new linked list
- llist*
- ll_init(void)
- {
- llist* new = malloc(sizeof(llist));
- new -> size = 0;
- new -> last = 0;
- new -> first = 0;
- debug((char*)__func__,"llist created\n");
- return new;
- }
- // append new node to linked list
- llnode*
- ll_append(llist* base, char* string)
- {
- /*
- if last : last->next = new
- */
- // allocate new list node
- llnode* new = malloc(sizeof(llnode));
- // if last node NULL and llist.size is zero
- // list is clear
- if ( base -> last == 0 && base -> size == 0)
- {
- base -> first = new;
- base -> last = new;
- base -> size += 1;
- new -> value = (char*) string;
- debug((char*)__func__,"list node created in empty list\n");
- return new;
- }
- else
- {
- // if last exists
- if ( base -> last != NULL )
- {
- // check for error
- if ( base -> size == 0 )
- {
- printf("trying to append in end but llist size is zero\n");
- free(new);
- return 0;
- }
- base -> last -> next = new;
- base -> last = new;
- base -> size += 1;
- new -> value = (char*)string;
- debug((char*)__func__,"list allocated in list with other childs\n");
- return new;
- }
- // if we have first, but don't have last
- if ( base -> first && base -> last == NULL)
- {
- printf("you have first element, but doesnt have last\n");
- llnode* current = base -> first;
- bool finded = false;
- while (finded == false)
- {
- if (current -> next == NULL)
- {
- current -> next = new;
- base -> last = new;
- base -> size += 1;
- new -> value = (char*)string;
- printf("llnode appended to llist with some errors\n");
- finded = true;
- return new;
- }
- else
- {
- current = current -> next;
- }
- }
- }
- return 0;
- }
- }
- void
- ll_print_all(base)
- llist* base;
- {
- if (base -> first == NULL)
- {
- printf("sorry, we cant find first element in your list\n");
- }
- else
- {
- llnode* current = base -> first;
- int i = 0;
- for(i; i < (base -> size); i++)
- {
- printf("-> %s\n",(char*)current -> value);
- current = current -> next;
- }
- printf("size is %d\n",i);
- }
- }
- bool
- ll_lookup(llist* base, char* keyword)
- {
- if (base -> first == 0) {
- printf("Sorry, this llist is empty\n");
- return false;
- }
- llnode* current = base -> first;
- bool finded = false;
- int i = 0;
- while (finded == false)
- {
- if (current -> value == keyword)
- {
- printf("value %s finded at position %d\n",keyword,i);
- return true;
- }
- else
- {
- i++;
- if (current -> next == 0)
- {
- printf("Sorry, we cant find this keyword in list\n");
- return false;
- }
- else
- {
- current = current -> next;
- }
- }
- }
- return false;
- }
- void
- ll_full_destroy(llist* base)
- {
- if (base -> first == NULL)
- {
- free(base);
- return;
- }
- llnode* current = base -> first;
- bool has_next = true;
- while (has_next == true)
- {
- if (current -> next)
- {
- llnode *next_current = current -> next;
- free(current);
- current = next_current;
- }
- else
- {
- free(current);
- has_next = false;
- }
- }
- free(base);
- }
- char*
- ll_get_at(llist* base,int position)
- {
- int i = 0;
- if (base -> size < position)
- {
- printf("Wrong index\n");
- return 0;
- }
- if (base -> first)
- {
- llnode* current = base -> first;
- if_equal:
- if ( i == position) {
- return (char*) current -> value;
- }
- else
- {
- current = current -> next;
- i++;
- goto if_equal;
- }
- }
- else
- {
- printf("list is nulled\n");
- return 0;
- }
- }
- int main(void) {
- llist* ll_base = ll_init();
- llnode* a = ll_append(ll_base,"First");
- llnode* b = ll_append(ll_base,"Second");
- llnode* c = ll_append(ll_base,"Third");
- ll_print_all(ll_base);
- ll_lookup(ll_base,"ddd");
- // ll_full_destroy(ll_base);
- printf("%s at position %d",ll_get_at(ll_base,4),4);
- }