Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- struct sinhvien
- {
- int MSSV;
- string HoTen;
- string lop;
- float DTB;
- };
- struct Node
- {
- sinhvien Data;
- Node *next;
- };
- struct Queue// dinh nghia cau truc cua queue
- {
- Node *dau, *cuoi;// Node dau va node cuoi
- int count;// dem so phan tu
- };
- Node *CreateNode(sinhvien x);
- void taoQueue(Queue &q);
- bool ktQueueRong(Queue q);
- void pushQueue(Queue &q, sinhvien x);
- int peakQueue(Queue &q, sinhvien &x);
- int popQueue(Queue &q, sinhvien &x);
- void input(Queue &q, sinhvien &x, int &n);
- void output(Queue q, sinhvien x, int n);
- Node *CreateNode(sinhvien x)
- {
- Node *p = new Node;
- if (p == NULL)
- {
- return NULL;
- }
- else
- {
- p->Data = x;
- p->next = NULL;
- }
- return p;
- }
- void taoQueue(Queue &q)// khoi tao queue
- {
- q.dau = q.cuoi = NULL;
- q.count = 0;
- }
- bool ktQueueRong(Queue q)// kiem tra rong
- {
- if (q.count == 0)
- return true;
- else
- return false;
- }
- void pushQueue(Queue &q, sinhvien x)// them phan tu vao queue
- {
- Node *p = CreateNode(x);
- if (ktQueueRong(q))
- {
- q.dau = q.cuoi = p;
- }
- else
- {
- q.cuoi->next = p;
- q.cuoi = p;
- }
- q.count++;
- }
- int peakQueue(Queue &q,sinhvien &x)// lay phan tu nhung khong xoa
- {
- if (ktQueueRong(q))
- {
- cout << "hang doi rong";
- return 0;
- }
- else
- {
- sinhvien x = q.dau->Data;
- cout << "MSSV la: " << x.MSSV << endl;
- cout << "ten sinh vien la: " << x.HoTen<< endl;
- cout << "lop sinh vien la: " << x.lop << endl;
- cout << "DTB cua sinh vien la: " << x.DTB<< endl;
- return 1;
- }
- }
- int popQueue(Queue &q,sinhvien &x)// loai bo phan tu dau khoi hang doi
- {
- if (ktQueueRong(q))
- {
- cout << "hang doi rong";
- return 0;
- }
- else
- {
- sinhvien x = q.dau->Data;
- if (q.count == 1)//
- taoQueue(q);
- else
- q.dau = q.dau->next;
- q.count--;
- return 1;
- }
- }
- void input(Queue &q, sinhvien &x, int &n)
- {
- cout << "nhap so luong sinh vien ma ban muon: ";
- cin >> n;
- for (int i = 0; i < n; i++)
- {
- cout << "__________________nhap sinh vien thu " << i + 1 << ": " << endl;
- rewind(stdin);
- cout << "nhap MSSV: ";
- cin >> x.MSSV;
- rewind(stdin);
- cout << "nhap ten sinh vien: ";
- getline(cin, x.HoTen);
- rewind(stdin);
- cout << "nhap lop sinhvien: ";
- getline(cin, x.lop);
- rewind(stdin);
- cout << "nhap DTB cua sinh vien: ";
- cin >> x.DTB;
- pushQueue(q, x);
- }
- }
- void output(Queue q,sinhvien x,int n)
- {
- Node *p = q.dau;
- while (p != NULL)
- {
- cout << "MSSV la: " << p->Data.MSSV << endl;
- cout << "ten sinh vien la: " << p->Data.HoTen << endl;
- cout << "lop sinh vien la: " << p->Data.lop << endl;
- cout << "DTB cua sinh vien la: " << p->Data.DTB << endl;
- p = p->next;
- }
- cout << "\n";
- }
- //void sapxeptangdan(Queue &q)// chua fix duoc :(((
- {
- Node *a = q.dau;
- Node *b = q.dau->next;
- do
- {
- do
- {
- if (a->Data.MSSV > b->Data.MSSV)
- {
- int MSSV;
- string HoTen;
- string lop;
- float DTB;
- MSSV = a->Data.MSSV;
- b->Data.MSSV = MSSV;
- HoTen = a->Data.HoTen;
- b->Data.HoTen = HoTen;
- MSSV = a->Data.MSSV;
- b->Data.MSSV = MSSV;
- }
- } while (b->next == NULL);
- } while (a->next->next == NULL);
- }//
- int main()
- {
- sinhvien x, y;
- int n;
- Queue q;
- taoQueue(q);
- input(q, x, n);
- output(q, x,n);
- int lua_chon;
- cout << "Moi Ban Chon Phep Toan Voi DS LKD";
- cout << "\n1: Kiem Tra Queue Rong";
- cout << "\n2: Them Phan Tu Vao Queue";
- cout << "\n3: Lay Gia Tri Dau Queue";
- cout << "\n4: lay va xoa sinh vien";
- cout << "\n5: Xuat Queue";
- cout << "\n6: Thoat";
- do
- {
- cout << "\nBan Chon: ";
- cin >> lua_chon;
- switch (lua_chon)
- {
- case 1:
- {
- if (ktQueueRong(q))
- cout << "Queue rong";
- else
- cout << "Queue Khong Rong";
- break;
- }
- case 2:
- {
- cout << "nhap phan tu can chen vao Queue: ";
- rewind(stdin);
- cout << "nhap MSSV: ";
- cin >> y.MSSV;
- rewind(stdin);
- cout << "nhap ten sinh vien: ";
- getline(cin, y.HoTen);
- rewind(stdin);
- cout << "nhap lop sinhvien: ";
- getline(cin, y.lop);
- rewind(stdin);
- cout << "nhap DTB cua sinh vien: ";
- cin >> y.DTB;
- pushQueue(q, y);
- break;
- }
- case 3:
- {
- peakQueue(q, x);
- break;
- }
- case 4:
- {
- popQueue(q, x);
- break;
- }
- case 5:
- {
- output(q, x, n);
- break;
- }
- }
- } while (lua_chon != 6);
- return 0;
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement