Advertisement
huutho_96

Kira Ngất

Aug 7th, 2015
383
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<iostream>
  4. #include<fstream>
  5. #include<stdlib.h>
  6. using namespace std;
  7. int k = 0; //Luu so phan tu có trong ds LIFO
  8. int demdc = 0; //Luu so phan tu co trong ds FIFO
  9. struct node
  10. {
  11. int so;
  12. struct node *next;
  13. };
  14. // Ham doc tep so nguyen ra mot danh sach LIFO duoc tro? boi? con tro? First.
  15. struct node *doctepFIFO(struct node *first)
  16. {
  17. int output;
  18. int n;
  19. struct node *tam;
  20. struct node *last = NULL;
  21. ifstream myFile("D:\\input.txt", std::ios::in);
  22. ifstream myFile2("D:\\input.txt", std::ios::in);
  23. if (!myFile)
  24. {
  25. cout << " Khong the mo duoc tep tin !!! Loi!!! ";
  26. }
  27. else
  28. {
  29. cout << "Mo thanh cong tap tin !!!\nHien thi noi dung tep tin :" << "\n";
  30. while (myFile >> output) // kiem tra so so nguyen co trong tep
  31. k++;
  32. myFile.close();
  33. if (k == 0)
  34. cout << "\n" << " Khong gia tri nao trong tep ca ";
  35. else
  36. {
  37. for (int i = 0; i<k; i++)
  38. {
  39. myFile2 >> n; // doc ky tu tu tep tin
  40. cout << n << " ";
  41. tam = (struct node*)malloc(1 * sizeof(struct node));
  42. tam->so = n;
  43. tam->next = NULL;
  44. if (first == NULL)
  45. {
  46. first = last = tam;
  47. }
  48. else
  49. {
  50. last->next = tam;
  51. last = tam;
  52. }
  53. }
  54. }
  55. }
  56. return first;
  57. }
  58. // Ham kiem tra mot so co phai la so duong chan hay khong
  59. int kiemTraDuongChan(int x)
  60. {
  61. if ((x>0) && ((x % 2) == 0))
  62. return 1;
  63. else
  64. return 0;
  65. }
  66. //Ham doc tu danh sach LIFO first thanh mot danh sach FIFO bao gom cac phan tu duong chan
  67. struct node *FIFOsangLIFO(struct node *first)
  68. {
  69. struct node *tam = NULL; //Con tro này tro den danh sách FIFO, no tro den phan tu dau tien cua danh sach
  70. struct node *p = first; //node de duyet danh sach
  71. struct node *head = NULL; //node dau danh sach
  72.  
  73. while (p != NULL)
  74. {
  75. if (p->so % 2 == 0 && p->so > 0)
  76. {
  77. tam = (struct node*)malloc(1 * sizeof(struct node));
  78. tam->next = NULL;
  79. tam->so = p->so;
  80. if (head == NULL)
  81. {
  82. head = tam;
  83. }
  84. else
  85. {
  86. tam->next = head;
  87. head = tam;
  88. }
  89. }
  90.  
  91. p = p->next;
  92. }
  93.  
  94. return head;
  95. }
  96. void xem(struct node *first)
  97. {
  98. struct node *tam = first;
  99. while (tam != NULL)
  100. {
  101. printf("%d ", tam->so);
  102. tam = tam->next;
  103. }
  104. }
  105. int main()
  106. {
  107. struct node *first = NULL;
  108. first = doctepFIFO(first);
  109. printf("\nDanh sach FIFO :\n");
  110. xem(first);
  111. struct node *second1 = NULL;
  112. printf("\nDanh sach LIFO :\n");
  113. second1 = FIFOsangLIFO(first);
  114. xem(second1);
  115. system("pause");
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement