Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #define ITERATIVE 20
- #define RAND_MAX 100
- typedef struct ls {
- int data;
- struct ls* next;
- } ls;
- ls* createNode(int data);
- void append(ls* list, int data);
- void printList(ls* list);
- void freeListRecursive(ls** head);
- int main(void)
- {
- ls* mainList = createNode(1);
- // Write Here
- freeListRecursive(&mainList);
- getchar();
- return 0;
- }
- /*
- Function creates a node with data
- input: integer data
- output: address of new struct
- */
- ls* createNode(int data)
- {
- ls* list = (ls*)malloc(sizeof(ls));
- list->data = data;
- list->next = NULL;
- return list;
- }
- /*
- Function prints a linked list
- input: head list
- output: none
- */
- void printList(ls* lst)
- {
- ls* curr = lst;
- do {
- printf("%d -> ", lst->data);
- lst = lst->next;
- } while (lst);
- printf("NULL\n");
- }
- /*
- Function takes a list and data and appends a new
- node to the end of the list inputed with the data given.
- input: list head and data
- output: none
- */
- void append(ls* list, int data)
- {
- ls* curr = list;
- while (curr->next)
- {
- curr = curr->next;
- }
- curr->next = createNode(data);
- }
- /*
- Function frees all memory of a linked list
- input: a linked list
- output: none
- */
- void freeListRecursive(ls** head)
- {
- if (*head != NULL) // if list not empty
- {
- if ((*head)->next != NULL) // end condition
- {
- freeListRecursive(&((*head)->next));
- }
- free(*head);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement