Guest User

Untitled

a guest
Oct 17th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct node_t{
  5. int x;
  6. struct node_t *next;
  7. } node_t;
  8.  
  9. node_t *list_create(int x,node_t *next) {
  10. node_t *new_node = (node_t *)malloc(sizeof(node_t));
  11. if (new_node == NULL) {
  12. return (node_t*)1;
  13. }
  14. new_node->x=x;
  15. new_node->next=next;
  16. return new_node;
  17. }
  18.  
  19. void list_append(node_t *head,int x) {
  20. node_t *cursor = head;
  21. while (cursor->next != NULL) {
  22. cursor=cursor->next;
  23. }
  24. cursor->next=list_create(x,NULL);
  25. return;
  26. }
  27.  
  28. void list_destroy(node_t *head) {
  29. node_t *cursor = head->next, *temp = NULL;
  30. free(head);
  31. if (cursor != NULL) {
  32. while (cursor->next != NULL) {
  33. temp=cursor->next;
  34. free(cursor);
  35. cursor=temp;
  36. }
  37. free(cursor);
  38. }
  39. return;
  40. }
  41.  
  42. int main() {
  43. node_t *head = list_create(0,NULL), *cursor = NULL;
  44. for (int i=1;i<32;i++) {
  45. list_append(head,i);
  46. }
  47. cursor=head;
  48. if (cursor->next == NULL) printf("%d",cursor->x);
  49. while (cursor->next != NULL) {
  50. printf("%d\n",cursor->x);
  51. cursor=cursor->next;
  52. }
  53. printf("%d\n",cursor->x);
  54. list_destroy(head);
  55. }
Add Comment
Please, Sign In to add comment