Skip to content

Instantly share code, notes, and snippets.

@kdelwat
Created July 25, 2017 06:14
Show Gist options
  • Save kdelwat/bf50b7ac269ed0d0f182263e60169543 to your computer and use it in GitHub Desktop.
Save kdelwat/bf50b7ac269ed0d0f182263e60169543 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
struct node *new_node(int data, struct node *next);
void print_list(struct node *head);
int main(void) {
struct node *head = new_node(4, NULL);
head = new_node(3, head);
head = new_node(2, head);
head = new_node(1, head);
print_list(head);
}
struct node *new_node(int data, struct node *next) {
struct node *new = malloc(sizeof (struct node));
new->data = data;
new->next = next;
return new;
}
void print_list(struct node *head) {
printf("Your list: ");
while(head != NULL) {
printf("%d ", head->data);
head = head->next;
}
printf("\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment