Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<conio.h>
- #include<iostream>
- #include<fstream>
- #include<stdlib.h>
- using namespace std;
- int k = 0; //Luu so phan tu có trong ds LIFO
- int demdc = 0; //Luu so phan tu co trong ds FIFO
- struct node
- {
- int so;
- struct node *next;
- };
- // Ham doc tep so nguyen ra mot danh sach LIFO duoc tro? boi? con tro? First.
- struct node *doctepFIFO(struct node *first)
- {
- int output;
- int n;
- struct node *tam;
- struct node *last = NULL;
- ifstream myFile("D:\\input.txt", std::ios::in);
- ifstream myFile2("D:\\input.txt", std::ios::in);
- if (!myFile)
- {
- cout << " Khong the mo duoc tep tin !!! Loi!!! ";
- }
- else
- {
- cout << "Mo thanh cong tap tin !!!\nHien thi noi dung tep tin :" << "\n";
- while (myFile >> output) // kiem tra so so nguyen co trong tep
- k++;
- myFile.close();
- if (k == 0)
- cout << "\n" << " Khong gia tri nao trong tep ca ";
- else
- {
- for (int i = 0; i<k; i++)
- {
- myFile2 >> n; // doc ky tu tu tep tin
- cout << n << " ";
- tam = (struct node*)malloc(1 * sizeof(struct node));
- tam->so = n;
- tam->next = NULL;
- if (first == NULL)
- {
- first = last = tam;
- }
- else
- {
- last->next = tam;
- last = tam;
- }
- }
- }
- }
- return first;
- }
- // Ham kiem tra mot so co phai la so duong chan hay khong
- int kiemTraDuongChan(int x)
- {
- if ((x>0) && ((x % 2) == 0))
- return 1;
- else
- return 0;
- }
- //Ham doc tu danh sach LIFO first thanh mot danh sach FIFO bao gom cac phan tu duong chan
- struct node *FIFOsangLIFO(struct node *first)
- {
- struct node *tam = NULL; //Con tro này tro den danh sách FIFO, no tro den phan tu dau tien cua danh sach
- struct node *p = first; //node de duyet danh sach
- struct node *head = NULL; //node dau danh sach
- while (p != NULL)
- {
- if (p->so % 2 == 0 && p->so > 0)
- {
- tam = (struct node*)malloc(1 * sizeof(struct node));
- tam->next = NULL;
- tam->so = p->so;
- if (head == NULL)
- {
- head = tam;
- }
- else
- {
- tam->next = head;
- head = tam;
- }
- }
- p = p->next;
- }
- return head;
- }
- void xem(struct node *first)
- {
- struct node *tam = first;
- while (tam != NULL)
- {
- printf("%d ", tam->so);
- tam = tam->next;
- }
- }
- int main()
- {
- struct node *first = NULL;
- first = doctepFIFO(first);
- printf("\nDanh sach FIFO :\n");
- xem(first);
- struct node *second1 = NULL;
- printf("\nDanh sach LIFO :\n");
- second1 = FIFOsangLIFO(first);
- xem(second1);
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement