akadjoker

linklist2

Mar 29th, 2022
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. struct node
  5. {
  6. int data;
  7. struct node *next;
  8. };
  9.  
  10.  
  11. void addFirst(struct node **head, int val)
  12. {
  13. //create a new node
  14. struct node *newNode = malloc(sizeof(struct node));
  15. newNode->data = val;
  16.  
  17. newNode->next = *head;
  18.  
  19. *head = newNode;
  20. }
  21.  
  22. void printList(struct node *head)
  23. {
  24. struct node *temp = head;
  25.  
  26. //iterate the entire linked list and print the data
  27. while(temp != NULL)
  28. {
  29. printf("%d->", temp->data);
  30. temp = temp->next;
  31. }
  32. printf("NULL\n");
  33. }
  34.  
  35. void addLast(struct node **head, int val)
  36. {
  37. //create a new node
  38. struct node *newNode = malloc(sizeof(struct node));
  39. newNode->data = val;
  40. newNode->next = NULL;
  41.  
  42. //if head is NULL, it is an empty list
  43. if(*head == NULL)
  44. *head = newNode;
  45. //Otherwise, find the last node and add the newNode
  46. else
  47. {
  48. struct node *lastNode = *head;
  49.  
  50. //last node's next address will be NULL.
  51. while(lastNode->next != NULL)
  52. {
  53. lastNode = lastNode->next;
  54. }
  55.  
  56. //add the newNode at the end of the linked list
  57. lastNode->next = newNode;
  58. }
  59.  
  60. }
  61.  
  62. int searchNode(struct node *head,int key)
  63. {
  64. struct node *temp = head;
  65.  
  66. //iterate the entire linked list and print the data
  67. while(temp != NULL)
  68. {
  69. //key found return 1.
  70. if(temp->data == key)
  71. return 1;
  72. temp = temp->next;
  73. }
  74. //key not found
  75. return -1;
  76. }
  77.  
  78. int ft_isspace(char c)
  79. {
  80. return ((c>=9 && c<=13) || c==32);
  81. }
  82.  
  83. void deleteNode(struct node **head, int key)
  84. {
  85. //temp is used to freeing the memory
  86. struct node *temp;
  87.  
  88. //key found on the head node.
  89. //move to head node to the next and free the head.
  90. if((*head)->data == key)
  91. {
  92. temp = *head; //backup to free the memory
  93. *head = (*head)->next;
  94. free(temp);
  95. }
  96. else
  97. {
  98. struct node *current = *head;
  99. while(current->next != NULL)
  100. {
  101. //if yes, we need to delete the current->next node
  102. if(current->next->data == key)
  103. {
  104. temp = current->next;
  105. //node will be disconnected from the linked list.
  106. current->next = current->next->next;
  107. free(temp);
  108. break;
  109. }
  110. //Otherwise, move the current node and proceed
  111. else
  112. current = current->next;
  113. }
  114. }
  115. }
  116.  
  117.  
  118. int main()
  119. {
  120. struct node *head = NULL;
  121. /*
  122. addFirst(&head,10);
  123. addFirst(&head,20);
  124. addFirst(&head,30);
  125.  
  126. addLast(&head,100);
  127. addLast(&head,200);
  128. addLast(&head,300);
  129.  
  130. printList(head);
  131.  
  132. //change the key and try it yourself.
  133. if(searchNode(head,20) == 1)
  134. printf("Search Found\n");
  135. else
  136. printf("Search Not Found\n");
  137. */
  138. addLast(&head,10);
  139. addLast(&head,20);
  140. addLast(&head,30);
  141. printf("Linked List Elements:\n");
  142.  
  143.  
  144. //delete first node
  145. deleteNode(&head,10);
  146. printf("Deleted 10. The New Linked List:\n");
  147.  
  148.  
  149. //delete last node
  150. deleteNode(&head,30);
  151. printf("Deleted 30. The New Linked List:\n");
  152.  
  153.  
  154. printList(head);
  155.  
  156.  
  157.  
  158.  
  159. return 0;
  160. }
Advertisement
Add Comment
Please, Sign In to add comment