online compiler and debugger for c/c++

code. compile. run. debug. share.
Source Code    Language
#include <stdbool.h> #include <stdlib.h> #include <stdio.h> typedef struct node_tag { int value; struct node_tag *next; } node_t; // write functions to encapsulate the data and provide a stable interface: node_t* node_create_value(int value) { node_t *new_node = calloc(1, sizeof *new_node); if(new_node) new_node->value = value; return new_node; } node_t* node_advance(node_t const *node) { return node->next; } typedef struct list_tag { // a list usually consists of node_t *head; // a pointer to the first and node_t *tail; // a pointer to the last element // size_t size; // one might want to add that. } list_t; list_t list_create(void) { list_t list = { NULL, NULL }; return list; } // make code based on these functions "speak" for itself: node_t* list_begin(list_t const *list) { return list->head; } node_t* list_end (list_t const *list) { return list->tail; } bool list_is_empty(list_t const *list) { return !list_begin(list); } // common operations for lists: node_t* list_push_front(list_t *list, int value) { node_t *new_node = node_create_value(value); if (!new_node) return NULL; new_node->next = list->head; return list->head = new_node; } node_t* list_push_back(list_t *list, int value) { // push_back on an empty list is push_front: if (list_is_empty(list)) return list->tail = list_push_front(list, value); node_t *new_node = node_create_value(value); if (!new_node) return NULL; list->tail->next = new_node; return list->tail = new_node; } node_t* list_insert_after(list_t *list, node_t *node, int value) { if (list_end(list) == node) return list_push_back(list, value); node_t *new_node = node_create_value(value); if (!new_node) return NULL; new_node->next = node->next; return node->next = new_node; } node_t* list_insert_sorted(list_t *list, int value) { // first handle the special cases that don't require iterating the whole list: if (list_is_empty(list) || value < list_begin(list)->value) return list_push_front(list, value); if (value > list_end(list)->value) return list_push_back(list, value); // the general (worst) case: for (node_t *current_node = list_begin(list); node_advance(current_node); current_node = node_advance(current_node)) if (value < node_advance(current_node)->value) return list_insert_after(list, current_node, value); return NULL; // should never happen } void list_print(list_t const *list) { for (node_t *current_node = list_begin(list); current_node; current_node = node_advance(current_node)) printf("%d\n", current_node->value); } void list_free(list_t *list) { for(node_t *current_node = list_begin(list), *next_node; current_node; current_node = next_node) { next_node = current_node->next; free(current_node); } } // user code should not be required to know anything about the inner workings // of our list: int main(void) { list_t list = list_create(); for (int i = 1; i < 10; i += 2) { if (!list_push_back(&list, i)) { list_free(&list); fputs("Not enough memory :(\n\n", stderr); return EXIT_FAILURE; } } list_print(&list); putchar('\n'); for (int i = 0; i < 11; i += 2) { if (!list_insert_sorted(&list, i)) { list_free(&list); fputs("Not enough memory :(\n\n", stderr); return EXIT_FAILURE; } } list_print(&list); list_free(&list); }

Compiling Program...

Command line arguments:
Standard Input: Interactive Console Text
×

                

                

Program is not being debugged. Click "Debug" button to start program in debug mode.

#FunctionFile:Line
VariableValue
RegisterValue
ExpressionValue