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 đề: Bài tập chuỗi đảo "STACK" Thu Dec 10, 2009 6:13 am | |
| - Code:
-
/* CHUONG TRINH DEMO NGAN XEP : NHAP 1 CHUOI IN CHUOI DAO */
#include <stdio.h> #include <conio.h> #include <string.h> #include <alloc.h> struct Stack { char info; struct Stack *next; }; typedef struct Stack *STACK;
//Khai Bao Prototype void InitializeS (STACK *ps); int EmptyS (STACK *ps); STACK GetStack(); void PushS (STACK *ps, char ch); char PopS (STACK *ps); void TopS (STACK *ps); //============================== void main() { STACK ps; int x, y, vt, chon, n, i; char ch, st[50]; clrscr(); InitializeS (&ps); do { clrscr(); printf("\n\n\tCHUONG TRINH NHAP VAO 1 CHUOI IN CHUOI DAO DUNG STACK !"); printf("\n\t1 : Khoi Tao Ngan Xep !"); printf("\n\t2 : Kiem Tra Ngan Xep Rong !"); printf("\n\t3 : Tao Ngan Xep !"); printf("\n\t4 : Xuat Va Huy Ngan Xep !"); printf("\n\t5 : Them 1 Phan Tu Vao Ngan Xep !"); printf("\n\t6 : Xoa 1 PTu Ra Khoi Ngan Xep !"); printf("\n\t7 : Truy Xuat Noi Dung O Dinh Ngan Xep !"); printf("\n\t0 : THOAT KHOI CHUONG TRINH !"); printf("\n\tBan Chon Chuc Nang Nao ? "); scanf ("%d", &chon); switch (chon) { case 1: { InitializeS (&ps); printf("\n\tNgan Xep Da Duoc Khoi Tao !"); getch(); break; } case 2: { printf("\n\tKiem Tra Ngan Xep Rong !"); if (EmptyS (&ps)) printf("\n\tNgan Xep Rong !"); else printf("\n\tNgan Xep Khong Rong !"); getch(); break; } case 3: { printf("\n\tTao Ngan Xep !"); InitializeS (&ps); printf("\n\tNhap Chuoi ST : "); fflush(stdin); gets(st); for (i = 0; i < strlen(st); i++) { ch = st[i]; PushS(&ps, ch); } getch(); break; } case 4: { printf("\n\tIn Chuoi Dao !"); printf("\n\tXuat Va Huy Ngan Xep !"); if (EmptyS (&ps)) printf("\n\tNgan Xep Rong !"); else { printf("\n\tNoi Ngan Xep Vua Nhap !"); while(EmptyS (&ps) == 0) { TopS (&ps); y = PopS (&ps); } } getch(); break; } case 5: { printf("\n\tThem 1 PTu Vao Ngan Xep !"); printf("\n\tNhap Noi Dung Ky Tu Can Them : "); ch = getch(); PushS(&ps, ch); getch(); break; } case 6: { printf("\n\tXoa PTu Dinh !"); printf("\n\tPTu Dinh O Dinh La ! "); TopS (&ps); y = PopS (&ps); getch(); break; } case 7: { printf("\n\tTruy Xuat Noi Dung PTu Dinh !"); TopS (&ps); getch(); break; } }//KT Switch }while (chon > 0); getch(); }//KThuc Ham Main //============================== //Cai Dat Cac Prototype //====================================== void InitializeS (STACK *ps) { *ps = NULL; } //======================================== int EmptyS (STACK *ps) { if (*ps == NULL) return 1; //Rong return 0; //Khong Rong } //======================================== STACK Getnode()//Cap Phat Vung Nho { STACK p; p = (STACK) malloc (sizeof (struct Stack)); p -> next = NULL; return p; } //======================================== char PopS (STACK *ps) { STACK p; char x; if (EmptyS(ps)) printf("\n\tDS Rong, Khong Xoa Duoc !"); else { p = *ps; x = p -> info; *ps = p -> next; free (p); return x; } }
//======================================== void PushS (STACK *ps, char x) { STACK p; p = Getnode(); p -> info = x; p -> next = *ps; *ps = p; } //======================================== void TopS (STACK *ps) { if (EmptyS (ps)) printf("\n\tNgan Xep Rong !"); else { printf("%5c", (*ps) -> info); //Neu La Cau Truc In1PTu }
} | |
|