Advertisement
eric11144

linked list

Jan 4th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <assert.h>
  4.  
  5. #include "push.h"
  6.  
  7. void
  8. linked_list_node_insert(node_t *sptr,
  9. char c)
  10. {
  11. sptr = malloc(sizeof(node_t));
  12. node_t *newNode ;
  13. newNode = (node_t*)malloc(sizeof(node_t));
  14. newNode->word = c;
  15. newNode->next = NULL;
  16. if (sptr->next == NULL)
  17. {
  18. sptr->next = newNode;
  19. }
  20. node_t *cur = sptr;
  21. /*while (cur->next == NULL)
  22. {
  23. last = cur;
  24. cur = cur->next;
  25. }*/
  26. printf("original last character is: %c", last->word);
  27. printf("\n");
  28. last->next = newNode;
  29. return;
  30. }
  31.  
  32. void
  33. linked_list_printlist(node_t *sptr)
  34. {
  35. node_t *cur ;
  36. printf("%c\n", cur->word);
  37. printf("linked_list_printlist: ");
  38. if(cur != NULL)
  39. {
  40. printf("%c", cur->word);
  41. printf((cur->next != NULL) ? "\n" : " ");
  42. cur = cur->next;
  43. }
  44. if(cur == NULL)
  45. {
  46. printf("is no data\n");
  47. return;
  48. }
  49. }
  50.  
  51. char linked_list_node_all_delete(node_t *sptr)
  52. {
  53. node_t *current, *head;
  54. head = sptr;
  55. while( head != NULL)
  56. {
  57. current = head;
  58. head = current->next;
  59. free(current);
  60. }
  61. if(head == NULL)
  62. {
  63. printf("node is empty \n");
  64. }
  65. return 0;
  66. }
  67.  
  68. int main()
  69. {
  70. node_t s ;
  71. linked_list_node_insert(&s, 'a');
  72. linked_list_node_insert(&s, 'b');
  73. linked_list_node_insert(&s, 'c');
  74.  
  75. linked_list_printlist(&s);
  76. ///linked_list_node_all_delete(s);
  77.  
  78. return 0;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement