Advertisement
Guest User

Untitled

a guest
Mar 5th, 2015
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <conio.h>
  4. #include <math.h>
  5. #include <alloc.h>
  6.  
  7. FILE *fin, *fout;
  8.  
  9. typedef struct list
  10. {
  11. int data;
  12. list *next; // âêàç³âíèê íà íàñòóïíèé åëåìåíò ÷åðãè
  13. } queue;
  14.  
  15. void add(queue* &head, queue *&pt, int a) // ôóíêö³ÿ ðîçì³ùåííÿ íîâîãî åëåìåíòó â ÷åðç³
  16. {
  17. queue *temp = new queue;
  18. //temp = (queue*)calloc(1, sizeof(queue));
  19. temp -> data = a;
  20. temp -> next = NULL;
  21. if(head == NULL) head = temp;
  22. else pt -> next = temp;
  23. pt = temp;
  24. }
  25.  
  26. void input(queue *&head) // ôóíêö³ÿ ââåäåííÿ ÷åðãè
  27. {
  28. int M, data;
  29. queue *pt;
  30. fscanf(fin, "%d", &M);
  31. for(int i = 0; i < M; i++)
  32. {
  33. fscanf(fin, "%d", &data);
  34. add(head, pt, data);
  35. }
  36. }
  37.  
  38. void print(queue *head) // ôóíêö³ÿ âèâåäåííÿ ÷åðãè
  39. {
  40. queue *temp = head -> next;
  41. while(temp)
  42. {
  43. fprintf(fout, "%d ", temp -> data);
  44. temp = temp -> next;
  45. }
  46. }
  47.  
  48. void parse(queue *&head, queue *&head1, queue *&head2)
  49. {
  50. queue *pt, *pt1, *pt2;
  51. pt = head;
  52. while(pt)
  53. {
  54. if(pt -> data >= 0) add(head1, pt1, pt -> data);
  55. else add(head2, pt2, pt -> data);
  56. }
  57. }
  58.  
  59. int main()
  60. {
  61. queue *head = NULL, *head1 = NULL, *head2 = NULL;
  62. fin = fopen("input.txt" , "r");
  63. fout = fopen("output.txt" , "w");
  64. input(head);
  65. parse(head, head1, head2);
  66. fprintf(fout, "ïàðí³ ÷èñëà: \n"); print(head1);
  67. fprintf(fout, "íåïàðí³ ÷èñëà: \n"); print(head2);
  68. fclose(fin);
  69. fclose(fout);
  70. return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement