Advertisement
Guest User

Untitled

a guest
May 4th, 2016
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. typedef struct Item
  5. {
  6. int num;
  7. float price;
  8. struct Item* next;
  9. }*PItem;
  10.  
  11. void Error_Msg(char*);
  12. void AddNewItem(PItem *, PItem *, int, int, float);
  13. void CreateList(PItem *, PItem *, FILE *);
  14. void PrintItem(PItem);
  15. void PrintList(PItem, char*);
  16. void WriteListToFile(PItem, FILE *);
  17. void DeleteList(PItem *);
  18.  
  19. int main()
  20. {
  21. int a, n;
  22. float b;
  23. PItem head = NULL, tail = NULL;
  24. FILE *in = fopen("input_price.txt", "rt");
  25. if (in == NULL)
  26. {
  27. Error_Msg("input file is wrong");
  28. }
  29. CreateList(&head, &tail, in);
  30. PrintList(head, "\nThe Old List:\n");
  31. printf("\n\nEnter a number and the price\n");
  32. scanf("%d%f", &a, &b);
  33. printf("\nEnter a place for the new item:");
  34. scanf("%d", &n);
  35. AddNewItem(&head, &tail, n, a, b);
  36. PrintList(head, "\nThe New List:\n");
  37. fclose(in);
  38.  
  39.  
  40.  
  41. //WriteListToFile(head, in);
  42. //DeleteList(&head);
  43. tail = NULL;
  44. return 0;
  45. }
  46. //-------------------------------------------------------------------
  47. void Error_Msg(char*msg)
  48. {
  49. printf("\n%s", msg);
  50. exit(1);
  51. }
  52. //-------------------------------------------------------------------
  53. void CreateList(PItem *head, PItem *tail, FILE *f)
  54. {
  55. int a;
  56. float b;
  57. PItem temp;
  58. while (fscanf(f, "%d %f", &a, &b) == 2)
  59. {
  60. temp = (PItem)malloc(sizeof(struct Item));
  61. if (temp == NULL)
  62. {
  63. DeleteList(head);
  64. Error_Msg("Memmory!");
  65. }
  66. temp->num = a;
  67. temp->price = b;
  68. temp->next = NULL;
  69. if (*head == NULL)
  70. *head = temp;
  71. else
  72. (*tail)->next = temp;
  73. *tail = temp;
  74. }
  75. }
  76. //-------------------------------------------------------------------
  77. void PrintItem(PItem node)
  78. {
  79. printf("%d,%.1f-->", node->num, node->price);
  80. }
  81. //-------------------------------------------------------------------
  82. void PrintList(PItem head, char* title)
  83. {
  84. printf("%s", title);
  85. while (head)
  86. {
  87. PrintItem(head);
  88. head = head->next;
  89. }
  90. }
  91. //-------------------------------------------------------------------
  92. void DeleteList(PItem *head)
  93. {
  94. PItem tmp = *head;
  95. while (*head)
  96. {
  97. tmp = *head;
  98. *head = (*head)->next;
  99. free(tmp);
  100. }
  101. }
  102. //-------------------------------------------------------------------
  103. void AddNewItem(PItem *head, PItem *tail, int n, int a, float b)
  104. {
  105. PItem* prev,next;
  106. PItem item;
  107. prev=NULL; next = NULL; item = NULL;
  108. int i;
  109. item = (PItem)malloc(sizeof(struct Item));
  110. item->num = a;
  111. item->price = b;
  112. prev = head;
  113. for (i = 0; i < n; i++)
  114. {
  115. prev = (**prev).next;
  116. if (i == n-1)
  117. {
  118. next = prev->next->next;
  119. break;
  120. }
  121. }
  122. prev->next = item;
  123. item->next = next;
  124. return;
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement