Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <assert.h>
- #include "push.h"
- void
- linked_list_node_initialize(node_t *n)
- {
- assert(n != NULL);
- n->word = '\0';
- n->next = NULL;
- }
- void
- linked_list_node_insert(node_t **sptr,
- char c)
- {
- assert(sptr != NULL);
- node_t *head = *sptr;
- assert(head != NULL && "Please pass a pointer to a list node to me.");
- if(head->word == '\0' && head->next == NULL)
- {
- head->word = c;
- return;
- }
- node_t *cur = head;
- node_t *last = NULL;
- node_t *to_insert = NULL;
- to_insert = (node_t*)malloc(sizeof(node_t));
- linked_list_node_initialize(to_insert);
- to_insert->word = c;
- while(cur != NULL)
- {
- last = cur;
- cur = cur->next;
- }
- printf("original last character is: %c\n", last->word);
- last->next = to_insert;
- }
- void
- linked_list_printlist(node_t *sptr)
- {
- assert(sptr != NULL);
- node_t *cur = sptr;
- printf("linked_list_printlist: ");
- while(cur != NULL)
- {
- printf("%c", cur->word);
- // ternary operator ?:
- printf((cur->next == NULL) ? "\n" : " ");
- cur = cur->next;
- }
- // do some works
- }
- char linked_list_node_all_delete(node_t **sptr)
- {
- node_t *current, *head;
- head = *sptr;
- while( head != NULL)
- {
- current = head;
- head = current->next;
- free(current);
- }
- if(head == NULL)
- {
- printf("node is empty \n");
- }
- }
- int main()
- {
- char a = 'a';
- node_t *s = malloc(sizeof(node_t));
- linked_list_node_insert(&s, a);
- linked_list_node_insert(&s, a);
- //linked_list_node_insert(&s, a);
- linked_list_printlist(s);
- linked_list_node_all_delete(&s);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement