Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- // Defining the list structure
- typedef struct element {
- int value;
- struct element *next;
- } element;
- // Creating the list head
- element* init_list() {
- element *head = malloc(sizeof(element));
- head->value = 0;
- head->next = NULL;
- return head;
- }
- // Creates a new element and pointing it's next to the old list
- element* append_on_top(element* list, int item_value) {
- element *head = malloc(sizeof(element));
- head->value = item_value;
- head->next = list;
- return head;
- }
- // Iterates the list printing each and every value
- void print_list (const element* list) {
- element *loop_list = list;
- while(loop_list->next != NULL) {
- printf("%d | ", loop_list->value);
- loop_list = loop_list->next;
- }
- }
- int main()
- {
- element* list = init_list();
- list = append_on_top(list, 5);
- list = append_on_top(list, 1);
- list = append_on_top(list, 7);
- list = append_on_top(list, 4);
- print_list(list);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement