Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct node {
  5. int data;
  6. struct node *next;
  7. } node_t;
  8.  
  9.  
  10. node_t *addNewNode(node_t *list, int newdat);
  11.  
  12. node_t *addNode(node_t *list, node_t *node);
  13.  
  14. node_t *pop(node_t *list);
  15.  
  16. node_t *at(node_t *list, int index);
  17.  
  18. node_t *peek(node_t *list);
  19.  
  20. int getLength(node_t *list);
  21.  
  22. node_t *addNewNode(node_t *list, int newdat) {
  23. if (list == NULL) {
  24. list = malloc(sizeof(node_t));
  25. list->data = newdat;
  26. list->next = NULL;
  27. } else {
  28. node_t *current = list;
  29. while (current->next == NULL) {
  30. current = current->next;
  31. }
  32. current->next = malloc(sizeof(node_t));
  33. current = current->next;
  34. current->data = newdat;
  35. current->next = NULL;
  36. }
  37. return list;
  38. }
  39.  
  40. node_t *addNode(node_t *list, node_t *node) {
  41. if (list == NULL) {
  42. list = node;
  43. } else {
  44. node_t *current = list;
  45. while (current->next == NULL) {
  46. current = current->next;
  47. }
  48. node->next = NULL;
  49. current->next = node;
  50. }
  51. return list;
  52. }
  53.  
  54. node_t *pop(node_t *list) {
  55. node_t *current = list;
  56. if (current == NULL)
  57. return NULL;
  58. else if (current->next == NULL) {
  59. list = NULL;
  60. return current;
  61. } else {
  62. node_t *current = list;
  63. while (current->next->next == NULL) {
  64. current = current->next;
  65. }
  66. node_t *takeout = current->next;
  67. current->next = NULL;
  68. return takeout;
  69. }
  70. }
  71.  
  72. node_t *at(node_t *list, int index) {
  73. int i;
  74. if (getLength(list) - 1 < index) {
  75. return NULL;
  76. } else {
  77. node_t *current = list;
  78. for (int i = 0; i < index; i++) {
  79. current = current->next;
  80. }
  81. return current;
  82. }
  83. }
  84.  
  85. node_t *peek(node_t *list) {
  86. return at(list, getLength(list));
  87. }
  88.  
  89. int getLength(node_t *list) {
  90. int i = 0;
  91. if (list == NULL) {
  92. return 0;
  93. } else {
  94. node_t *current = list;
  95. while (current->next == NULL) {
  96. current = current->next;
  97. }
  98.  
  99. }
  100. }
  101.  
  102.  
  103. int main(int argc, char *argv[]) {
  104.  
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement