LinkedList

Run Settings
LanguageC
Language Version
Run Command
#include <stdio.h> #include "LinkedList.h" int main() { node_ptr_t head; list_init(head); list_add(head, 22); list_print(head); //list_print(head); //int temp = 2; //list_add(head, 22); //list_add(head, 5); //list_print(*head); //printf("LIST LENGTH: %d", list_length(head)); printf("Hello World!\n"); return 0; }
#ifndef __STACK_H #define __STACK_H typedef struct node node_t; typedef struct node* node_ptr_t; #define list_for_each(pos, head) \ for (pos = (head)->next; pos != NULL; pos = pos->next) struct node { void *data; struct node *next; }; void list_init(node_ptr_t head); void list_add(node_ptr_t head, void* data); void list_add_tail(node_ptr_t head, void* data); node_ptr_t list_del(node_ptr_t head); void list_print(node_ptr_t head); unsigned int list_length(node_ptr_t head); void list_reverse(node_ptr_t *head); #endif
#include <stdio.h> #include <stdlib.h> #include "LinkedList.h" void list_init(node_ptr_t head) { head = (node_ptr_t)malloc(sizeof(node_t)); if (head == NULL) { fprintf(stderr, "Can't allocate memory\n"); exit(1); } head->next = NULL; head->data = NULL; if(head->next == NULL) { printf("HERE IS NULL\n"); } } void list_add(node_ptr_t head, void* data) { if(head->next == NULL) { printf("HERE IS NULL2\n"); } node_ptr_t new_node = (node_ptr_t)malloc(sizeof(node_t)); if (new_node == NULL) { fprintf(stderr, "Can't allocate memory\n"); exit(1); } new_node->data = data; new_node->next = head->next; head->next = new_node; } void list_add_tail(node_ptr_t head, void* data) { node_ptr_t new_node = (node_ptr_t)malloc(sizeof(node_t)); if (new_node == NULL) { fprintf(stderr, "Can't allocate memory\n"); exit(1); } new_node->data = data; new_node->next = NULL; node_ptr_t current = head->next; if (current == NULL) { head->next = new_node; } else { while (current->next != NULL) { current = current->next; } current->next = new_node; } } node_ptr_t list_del(node_ptr_t head) { node_ptr_t temp = NULL; if (head->next != NULL) { temp = head->next; head->next = head->next->next; } return temp; } void list_print(node_ptr_t head) { node_ptr_t current; //list_for_each(current, head) printf("HERE\n"); if(head != NULL) { printf("ONE\n"); if(head->next != NULL) { printf("TWO\n"); current = head->next; printf("%d\n", current->data); } if(current->next != NULL) { printf("THREE\n"); current = current->next; printf("%d\n", current->data); } } /* for (current = head->next; current != NULL; current = current->next) printf("%d\n", *((int*)current->data)); */ } unsigned int list_length(node_ptr_t head) { node_ptr_t current; unsigned int length = 0; list_for_each(current, head) length++; return length; } void list_reverse(node_ptr_t *head) { }
Editor Settings
Theme
Key bindings
Full width
Lines