Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Linked List demo in C
#include <stdio.h> #include <string.h> #include <stdlib.h> #define TRUE 1 #define FALSE 0 typedef int BOOL; typedef struct _thing { char *item; struct _thing *next; }THING; THING *NewElement(char *text) { THING *newp; newp = (THING *) malloc(sizeof(THING)); newp -> item = (char *) malloc(strlen(text) + 1); strcpy(newp -> item, text); newp -> next = NULL; return newp; } void InsertThing(THING **head, THING *newp) { THING **tracer = head; while((*tracer) && strcmp((*tracer) -> item , newp -> item) < 1) { tracer = &(*tracer) -> next; } newp -> next = *tracer; *tracer = newp; } void RemoveThing(THING **head, char *text) { BOOL present = FALSE; THING *old; THING **tracer = head; while((*tracer) && !(present = (strcmp(text, (*tracer) -> item) == 0) )) tracer = &(*tracer) -> next; if(present) { old = *tracer; *tracer = (*tracer) -> next; free(old -> item); free(old); } } void PrintList(THING **head) { THING **tracer = head; while(*tracer) { printf("%s \n", (*tracer) -> item); tracer = &(*tracer) -> next; } } THING *start = NULL; int main(int argc, char **argv) { InsertThing(&start, NewElement("chips")); InsertThing(&start, NewElement("onion")); InsertThing(&start, NewElement("beer")); InsertThing(&start, NewElement("turkey")); InsertThing(&start, NewElement("zucchini")); InsertThing(&start, NewElement("burgers")); InsertThing(&start, NewElement("slaw")); printf("\nINITIAL LIST\n"); PrintList (&start); RemoveThing(&start, "pizza"); RemoveThing(&start, "zucchini"); RemoveThing(&start, "burgers"); printf("\nALTERED LIST\n"); PrintList(&start); return 0; } // Actual program taken from Computerphile: https://www.youtube.com/watch?v=0ZEX_l0DFK0
run
|
edit
|
history
|
help
1
String Manipulation
poinres1
main.c
2.1.1 Prime numbers in different threads with thread ID
Greeting
NETFLIX TERMINADO
MÓDULO 3, PREGUNTA 1, GUÍA 4
Array Sum
A_141121_factorial01
Ahora si?