Advertisement
huutho_96

Code 2

Aug 7th, 2015
449
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.25 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4. //dinh nghia 1 node. trong c++ chung ta khong can tu struct nua
  5. struct Node
  6. {
  7.     int x;
  8.     Node *pNext;
  9. };
  10.  
  11. //1 list Lifo hay Fifo thi ta deu co the quan ly bang 2 con tro co ban nay. Nhu vay thao tac se de hon so voi 1 con tro first
  12. struct List
  13. {
  14.     Node *pHead;
  15.     Node *pTail;
  16. };
  17.  
  18. //Khoi tao 1 List trong, khong co phan tu nao
  19. void CreateList(List &L)
  20. {
  21.     L.pHead = L.pTail = NULL;
  22. }
  23.  
  24. //Khoi tao 1 node co gia tri x
  25. Node *CreateNode(int x)
  26. {
  27.     Node *p = new Node;
  28.     //Kiem tra neu khong du bo nho de cap phat thi thoat luon
  29.     if (p == NULL)
  30.         exit(1);
  31.     p->pNext = NULL;
  32.     p->x = x;
  33.     return p;
  34. }
  35.  
  36. //Them 1 Node vao dau danh sach. cai nay danh cho LIFO
  37. void AddHead(List &L, Node *pAdd)
  38. {
  39.     if (L.pHead == NULL)
  40.     {
  41.         L.pHead = L.pTail = pAdd;
  42.     }
  43.     else
  44.     {
  45.         pAdd->pNext = L.pHead;
  46.         L.pHead = pAdd;
  47.     }
  48. }
  49.  
  50. //Them 1 Node vao cuoi danh sach. cai nay danh cho FIFO
  51. void AddTail(List &L, Node *pAdd)
  52. {
  53.     if (L.pHead == NULL)
  54.     {
  55.         L.pHead = L.pTail = pAdd;
  56.     }
  57.     else
  58.     {
  59.         L.pTail->pNext = pAdd;
  60.         L.pTail = pAdd;
  61.     }
  62. }
  63.  
  64. //Duyet cac phan tu trong List ra de xem
  65. void Output(List L)
  66. {
  67.     Node *p = L.pHead;
  68.     while (p != NULL)
  69.     {
  70.         cout << " " << p->x;
  71.         p = p->pNext;
  72.     }
  73. }
  74.  
  75. //Doc tep vao danh sach LIFO
  76. void ReadFile(List &L)
  77. {
  78.     //thay vi dung ifstream de doc minh dung lop tong quat hon la fstream va dung mode la ca in va out
  79.     fstream f;
  80.     int x;
  81.     f.open("D:\\input.txt", ios::in | ios::out);
  82.     if (!f)
  83.         cout << "tep khong ton tai\n";
  84.     else
  85.     {
  86.         while (!f.eof())
  87.         {
  88.             f >> x;
  89.             //Tao node co gia tri x va them vao dau danh sach fifo
  90.             AddHead(L, CreateNode(x));
  91.         }
  92.     }
  93.     f.close();
  94. }
  95.  
  96. //Lay phan tu chan duong trong lifo chuyen sang fifo
  97. void Lifo_Fifo(List Fifo, List &Lifo)
  98. {
  99.     Node *p = Fifo.pHead;
  100.     while (p != NULL)
  101.     {
  102.         if (p->x > 0 && p->x % 2 == 0)
  103.         {
  104.             //Tao node co gia tri p->x va them vao danh sach lifo
  105.             AddTail(Lifo, CreateNode(p->x));
  106.         }
  107.         p = p->pNext;
  108.     }
  109. }
  110.  
  111. void main()
  112. {
  113.     List Fifo, Lifo;
  114.     //Khoi tao 2 list
  115.     CreateList(Fifo);
  116.     CreateList(Lifo);
  117.  
  118.     //Doc du lieu vao Lifo va xuat ra man hinh
  119.     ReadFile(Lifo);
  120.     Output(Lifo);
  121.  
  122.     cout << endl;
  123.  
  124.     //Doc du lieu vao Fifo va xuat ra man hinh
  125.     Lifo_Fifo(Lifo, Fifo);
  126.     Output(Fifo);
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement