minhlap Admin - Quản trị viên
Tổng số bài gửi : 129 Points : 374 Reputation : 5 Join date : 22/07/2009 Age : 34 Đến từ : TP Hồ Chí Minh
| Tiêu đề: Bài Queue Tue Sep 01, 2009 3:22 pm | |
| - Code:
-
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading;
namespace Queue { class Queue { int kichthuoc; int[] a; int front,rear; public Queue(int n) { kichthuoc = n; a = new int[n]; front = rear = 0; } public bool EmptyQueue() { if (front == rear) return true; else return false; } public bool FullQueue() { int nr = (rear + 1) % kichthuoc; if (nr == front) return true; else return false; } public void AddQueue(int x) { if (FullQueue() == false) a[++rear] = x; else Console.WriteLine("Queue day"); } public void RemoveQueue(ref int x) { if (EmptyQueue() ==false) x = a[++front]; else Console.WriteLine("Queue rong"); } public void NhapQueue(Queue a) { string s; int x; Console.WriteLine("Nhap Queue"); for (int i = 0; ; i++) { Console.Write("Nhap phan tu thu {0} :", i); s = Console.ReadLine(); x = int.Parse(s); if (x <= 0) break; a.AddQueue(x); } } public void DuyetQueue(Queue a) { int x = 0; int i = 0; Console.WriteLine("Duyet Stack"); while (a.EmptyQueue() == false) { a.RemoveQueue(ref x); Console.WriteLine("Phan tu thu i la :{0}", x); } } } class Program { static void Main(string[] args) { int x = 0; ; Queue a = new Queue(5); a.NhapQueue(a); a.DuyetQueue(a); } } } | |
|