Advertisement
huutho_96

Code Nhập giới hạn phần tử

Aug 7th, 2015
432
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.60 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 Input(List &L, int Max)
  112. {
  113.     int x;
  114.     cout << "Viec nhap se ket thuc khi nhap gia tri x = 0\nNhap danh sach LIFO\n";
  115.     while (Max > 0)
  116.     {
  117.         cout << "x = ";
  118.         cin >> x;
  119.         if (x == 0) break;
  120.         AddHead(L, CreateNode(x));
  121.         Max--;
  122.     }
  123. }
  124.  
  125.  
  126. void main()
  127. {
  128.     List Fifo, Lifo;
  129.     //Khoi tao 2 list
  130.     CreateList(Fifo);
  131.     CreateList(Lifo);
  132.  
  133.     //Nhap du lieu vao Lifo va xuat ra man hinh
  134.     //Tam thoi khuc nay minh gioi han 5 phan tu. ban co the cin >> n va truyen n vao de nhap n phan tu
  135.     Input(Lifo, 5);
  136.     Output(Lifo);
  137.  
  138.     cout << endl;
  139.  
  140.     //Doc du lieu vao Fifo va xuat ra man hinh
  141.     Lifo_Fifo(Lifo, Fifo);
  142.     Output(Fifo);
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement