Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.61 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <iostream>
  4. #include <iomanip>
  5. #include <stdlib.h>
  6.  
  7. struct Node
  8. {
  9. struct data
  10. {
  11. int ID;
  12. float ms;
  13. }number;
  14.  
  15. Node * Next;
  16. Node * Previous;
  17. };
  18.  
  19.  
  20. Node * Start;
  21.  
  22. void AddToFront();
  23. void DeleteBeginning();
  24. void PrintListForward();
  25. void PrintListBackward();
  26.  
  27. void main()
  28. {
  29. Start = 0;
  30. int Choice;
  31. Choice = 0;
  32.  
  33. printf("************** MAIN MENU ****************\n");
  34. printf("Please Enter one of the following Choices\n");
  35. printf("************** MAIN MENU ****************\n\n");
  36.  
  37. do
  38. {
  39. printf("======================================================\n");
  40. printf("1. Add a node to the front of the list.\n");
  41. printf("2. Delete a node from the beginning of the list.\n");
  42. printf("3. Print a list Forward.\n");
  43. printf("4. Print a list Backward.\n");
  44. printf("======================================================\n");
  45. printf("5: Exit the program.\n");
  46.  
  47. scanf_s("%d", &Choice);
  48.  
  49. switch (Choice)
  50. {
  51. case 1:
  52. AddToFront();
  53. break;
  54. case 2:
  55. DeleteBeginning();
  56. break;
  57. case 3:
  58. PrintListForward();
  59. break;
  60. case 4:
  61. PrintListBackward();
  62. break;
  63. default:
  64. break;
  65. }
  66.  
  67. } while (Choice != 5); //when the choice is == 5, the program returns
  68. }
  69.  
  70. void AddToFront()
  71. {
  72. Node * NewNode;
  73. NewNode = (Node *)malloc(sizeof(Node));
  74. printf("\n");
  75. printf("Enter ID: ");
  76. scanf("%d", &NewNode->number.ID);
  77. printf("Enter milliseconds: ");
  78. scanf("%f", &NewNode->number.ms);
  79.  
  80. if (Start == 0)
  81. {
  82. NewNode->Previous = 0;
  83. NewNode->Next = 0;
  84. Start = NewNode;
  85. }
  86. else
  87. {
  88. Node *temp = Start;
  89.  
  90. while (temp->Next != 0)
  91. {
  92. temp = temp->Next;
  93. }
  94.  
  95. NewNode->Previous = temp;
  96. temp->Next = NewNode;
  97. NewNode->Next = 0;
  98. }
  99. }
  100.  
  101. void PrintListBackward()
  102. {
  103. if (Start == 0)
  104. {
  105. printf("Nothing to print, insert some elements first.\n\n");
  106. return;
  107. }
  108. Node *temp = Start;
  109. //Go to the first element
  110. while (temp->Next != 0)
  111. {
  112. temp = temp->Next;
  113. }
  114.  
  115. //Print from first to last
  116. while (temp != 0)
  117. {
  118. /*printf("%d", temp->data);
  119. printf("->");
  120. temp = temp->Next;*/
  121.  
  122. printf("%d", temp->number.ID);
  123. printf(" -> ");
  124. printf("ms: %0.1f", temp->number.ms);
  125. printf(" -> ");
  126. temp = temp->Previous;
  127. }
  128. printf("NULL\n");
  129. }
  130.  
  131. void PrintListForward()
  132. {
  133.  
  134. if (Start == 0)
  135. {
  136. printf("Nothing to print, insert some elements first.\n\n");
  137. return;
  138. }
  139. Node *temp = Start;
  140. //Go till the last element
  141. while (temp->Previous != 0)
  142. {
  143. temp = temp->Previous;
  144. }
  145.  
  146. //Print from last to first
  147. while (temp != 0)
  148. {
  149. printf("%d", temp->number.ID);
  150. printf("->");
  151. printf("ms: %0.1f", temp->number.ms);
  152. printf(" -> ");
  153. temp = temp->Next;
  154. }
  155. printf("NULL\n");
  156. }
  157.  
  158. void DeleteBeginning()
  159. {
  160. if (Start == 0)
  161. {
  162. printf("Nothing to delete, insert some elements first.\n\n");
  163. }
  164. else if (Start->Next == 0 && Start->Previous == 0) { //There is only one element and it should be removed.
  165. Start = 0; //this removes the only element
  166. }
  167. else
  168. {
  169. Node *temp = Start;
  170. //Go to the LAST element of the list
  171. while (temp->Next != 0)
  172. {
  173. temp = temp->Next;
  174. }
  175.  
  176. //Loop till first element that has a Previous element
  177. while (temp->Previous != 0)
  178. {
  179. //Verify if the element after the current one is the first
  180. //We want the one before the first to remove its Previous
  181. if (temp->Previous->Previous == 0) {
  182. break;
  183. }
  184. temp = temp->Previous;
  185. }
  186. Node *PreviousOfTemp = temp->Previous;
  187. Node * NewStart = temp;
  188.  
  189. temp->Previous = 0;
  190. free(PreviousOfTemp);
  191. Start = NewStart;
  192.  
  193. PrintListForward();
  194. }
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement