Skip to content

Instantly share code, notes, and snippets.

@prail
Created October 16, 2017 21:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prail/a463a0e9329979abdb3f2478c01dd519 to your computer and use it in GitHub Desktop.
Save prail/a463a0e9329979abdb3f2478c01dd519 to your computer and use it in GitHub Desktop.
Just a linked list program I made for future reference.
#include <stdio.h>
#include <stdlib.h>
typedef struct node_t{
int x;
struct node_t *next;
} node_t;
node_t *list_create(int x,node_t *next) {
node_t *new_node = (node_t *)malloc(sizeof(node_t));
if (new_node == NULL) {
return (node_t*)1;
}
new_node->x=x;
new_node->next=next;
return new_node;
}
void list_append(node_t *head,int x) {
node_t *cursor = head;
while (cursor->next != NULL) {
cursor=cursor->next;
}
cursor->next=list_create(x,NULL);
return;
}
void list_destroy(node_t *head) {
node_t *cursor = head->next, *temp = NULL;
free(head);
if (cursor != NULL) {
while (cursor->next != NULL) {
temp=cursor->next;
free(cursor);
cursor=temp;
}
free(cursor);
}
return;
}
int main() {
node_t *head = list_create(0,NULL), *cursor = NULL;
for (int i=1;i<32;i++) {
list_append(head,i);
}
cursor=head;
if (cursor->next == NULL) printf("%d",cursor->x);
while (cursor->next != NULL) {
printf("%d\n",cursor->x);
cursor=cursor->next;
}
printf("%d\n",cursor->x);
list_destroy(head);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment