Luv Moderators Angle
Tổng số bài gửi : 34 Points : 78 Reputation : 100 Join date : 27/11/2009 Age : 34 Đến từ : Quận 4
| Tiêu đề: Hàng đợi (QUEUE) dùng cấu trúc DSLK đơn demo trên dãy số nguyên Thu Dec 10, 2009 6:16 am | |
| - Code:
-
/* CHUONG TRINH DEMO HANG DOI DUNG CAU TRUC DSLK DON THAO TAC TREN DAY SO NGUYEN */
#include <stdio.h> #include <conio.h> #include <string.h> #include <alloc.h> struct Queue { int info; struct Queue *next; }; typedef struct Queue *QUEUE;
//Khai Bao Prototype void InitializeQ (QUEUE *pfirst, QUEUE *plast); int EmptyQ (QUEUE *pfirst); QUEUE GetQueue(); void PushQ (QUEUE *pfirst, QUEUE *plast, int x); int PopQ (QUEUE *pfirst, QUEUE *plast); void TopQ (QUEUE *pfirst); //============================== void main() { QUEUE pfirst, plast; int x, y, vt, chon, n, i; char ch; clrscr(); InitializeQ (&pfirst, &plast); do { clrscr(); printf("\n\n\tCHUONG TRINH DEMO HANG DOI THAO TAC TREN DAY NGUYEN !"); printf("\n\t1 : Khoi Tao Hang Doi !"); printf("\n\t2 : Kiem Tra Hang Doi Rong !"); printf("\n\t3 : Tao Hang Doi !"); printf("\n\t4 : Xuat Va Huy Hang Hoi !"); printf("\n\t5 : Them 1 Phan Tu Vao Hang Doi !"); printf("\n\t6 : Xoa 1 PTu Ra Khoi hang Doi !"); printf("\n\t7 : Truy Xuat Noi Dung O Dinh Hang Doi !"); printf("\n\t0 : THOAT KHOI CHUONG TRINH !"); printf("\n\tBan Chon Chuc Nang Nao ? "); scanf ("%d", &chon); switch (chon) { case 1: { InitializeQ (&pfirst, &plast); printf("\n\tHang Doi Da Duoc Khoi Tao !"); getch(); break; } case 2: { printf("\n\tKiem Tra Hang Doi Rong !"); if (EmptyQ (&pfirst)) printf("\n\tHang Doi Rong !"); else printf("\n\tHang Doi Khong Rong !"); getch(); break; } case 3: { printf("\n\tTao Hang Doi !"); InitializeQ (&pfirst, &plast); printf("\n\tNhap SPTu Trong Hang Doi !"); scanf ("%d", &n); printf("\n\tTao Hang Doi !"); for (i = 1; i <= n; i++) { printf("\t\tNhap PTu Thu %d : ", i); scanf("%d", &x); //Neu La Cau Truc : x = Nhap1PTu(); PushQ(&pfirst, &plast, x); } getch(); break; } case 4: { printf("\n\tXuat Va Huy Hang Doi !"); if (EmptyQ (&pfirst)) printf("\n\tHang Doi Rong !"); else { printf("\n\tNoi Dung HDoi Vua Nhap !"); while(EmptyQ (&pfirst) == 0) { TopQ (&pfirst); y = PopQ (&pfirst, &plast); } } getch(); break; } case 5: { printf("\n\tThem 1 PTu Vao Hang Doi !"); printf("\n\tNhap Noi Dung X Can Them : "); scanf ("%d", &x); PushQ(&pfirst, &plast, x); getch(); break; } case 6: { printf("\n\tXoa PTu Dinh !"); y = PopQ (&pfirst, &plast); getch(); break; } case 7: { printf("\n\tTruy Xuat Noi Dung PTu Dinh !"); TopQ (&pfirst); getch(); break; } }//KT Switch }while (chon > 0); getch(); }//KThuc Ham Main //============================== //Cai Dat Cac Prototype //====================================== void InitializeQ (QUEUE *pfirst, QUEUE *plast) { *pfirst = *plast = NULL; } //======================================== int EmptyQ (QUEUE *pfirst) { if (*pfirst == NULL) return 1; //Rong return 0; //Khong Rong } //======================================== QUEUE Getnode()//Cap Phat Vung Nho { QUEUE p; p = (QUEUE) malloc (sizeof (struct Queue)); p -> next = NULL; return p; } //======================================== int PopQ (QUEUE *pfirst, QUEUE *plast) { QUEUE p; int x; if (EmptyQ(pfirst)) printf("\n\tDS Rong, Khong Xoa Duoc !"); else { p = *pfirst; x = p -> info; if (p -> next == NULL) //DS Co 1 Phan Tu *pfirst = *plast = NULL; else *pfirst = p -> next; return x; } } //======================================== void PushQ (QUEUE *pfirst, QUEUE *plast, int x) { QUEUE p; p = Getnode(); p -> info = x; p -> next = NULL; if (EmptyQ (pfirst)) { *pfirst = p; *plast = *pfirst; } else { (*plast) -> next = p; *plast = p; } } //======================================== void TopQ (QUEUE *pfirst) { if (EmptyQ (pfirst)) printf("\n\tHang Doi Rong !"); else { printf("%5d", (*pfirst) -> info); //Neu La Cau Truc In1PTu }
} | |
|