Advertisement
Guest User

Untitled

a guest
Jan 18th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. //
  2. // Created by elvis on 19.1.14.
  3. //
  4.  
  5. #include "include/list_public.h"
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8.  
  9. typedef struct node
  10. {
  11. int data;
  12. struct node * next;
  13. }node;
  14.  
  15.  
  16. node * createLinkedList(int n);
  17. void displayList(node * head);
  18. void freeList(struct node* head);
  19.  
  20. int main( ) {
  21. struct linked_list list;
  22.  
  23. int n = 0;
  24. node * HEAD = NULL;
  25. printf("\nHow many nodes:");
  26. scanf("%d",&n);
  27. HEAD = createLinkedList(n);
  28. displayList(HEAD);
  29.  
  30. //char *pStr = (char*) malloc(512); //memory leaks fix? nope this is another memory leak wtf?
  31. freeList(HEAD);
  32. return 0;
  33.  
  34. }
  35.  
  36. node * createLinkedList(int n)
  37. {
  38. //int i = 0;
  39. node * head = NULL;
  40. node * temp = NULL;
  41. node * current = head;//node * current = NULL;
  42.  
  43. for (int i = 0; i < n; i++) {
  44.  
  45.  
  46. temp = (node*)malloc(sizeof(node));
  47.  
  48. printf("\nEnter the data for node number: %d", i + 1); //printf("\nEnter the data for node number: ", i + 1);
  49. scanf("%d",&temp->data);//number you store in nodes &(temp->data)
  50.  
  51. temp->next = NULL;
  52.  
  53. if (head == NULL)
  54. {
  55. head = temp;//no **
  56.  
  57. current = head->next;
  58.  
  59. temp = NULL;
  60. printf("\n IF loop");
  61. }
  62. else
  63. {
  64.  
  65. current = temp;
  66. current = current->next;
  67. temp = NULL;
  68.  
  69. }
  70.  
  71. printf("\n ending loop");
  72. free(temp);
  73. }
  74.  
  75. return head;
  76. }
  77.  
  78. void displayList(node * head)
  79. {
  80. node * current = head;
  81. while(current !=NULL)
  82. {
  83. printf("\t%d->",current->data);
  84. current = current->next;
  85. }
  86.  
  87. }
  88. void freeList(struct node* head)
  89. {
  90. struct node* tmp;
  91.  
  92. while (head != NULL)
  93. {
  94. tmp = head;
  95. head = head->next;
  96. free(tmp);
  97. }
  98.  
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement