You are on page 1of 4

using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lab7
{
internal class ArrayQueue
{
//data member
int[] qArray; //mang 1 chieu so nguyen
int qFront; //vi tri lay 1 phan tu ra (top - first)
int qRear; //vi tri dau 1 gia tri vao cuoi
int qMax; //suc chua toi da
int qCount; //so luong phan tu hien tai

//properties
public int[] QArray { get => qArray; set => qArray = value; }
public int QFront { get => qFront; set => qFront = value; }
public int QRear { get => qRear; set => qRear = value; }
public int QMax { get => qMax; set => qMax = value; }
public int QCount { get => qCount; set => qCount = value; }

//constructor
public ArrayQueue(int max = 0)
{
qArray = new int[max];
qFront = 0;
qRear = -1;
qMax = max;
qCount = 0; //hàng chờ rỗng
}

////Các phương thức


//2. public bool IsEmpty(): Kiểm tra queue có rỗng không? Trả về true nếu rỗng, ngược lại trả về false.
public bool IsEmpty => qCount == 0;
//3. public bool IsFull(): Kiểm tra queue có đầy không? Trả về true nếu đầy, ngược lại trả về false.
public bool IsFull => qCount == qMax;
//4. public bool EnQueue(int x): Thêm phần tử x vào queue theo phương pháp xoay vòng.Nếu thêm thành công thì
trả về true, ngược lại trả về false.
public bool EnQueue(int x)
{
if (IsFull) return false;
qRear++;
if (qRear % qMax == 0) qRear = 0; //xoay vòng
qArray[qRear] = x; //thêm vào ở cuối
qCount++;
return true;
}
//5. public bool DeQueue(out int outItem): Lấy một phần tử ra khỏi queue theo phương pháp xoay vòng và lưu vào
outItem.Nếu lấy ra thành công thì trả về true, ngược lại trả về false.
public bool DeQueue(out int outItem)
{
outItem = 0;
if (IsEmpty) return false;
outItem = qArray[qFront]; //lay ra o dau
qFront++;
if (qFront % qMax == 0) qFront = 0; //xoay vong
qCount--;
return true;
}
//6. public bool Top(out int topItem): Xem phần tử ở đỉnh stack và gán vào topItem.Nếu có giá trị trong stack thì trả
về true, ngược lại trả về false.
public bool Top(out int outItem)
{
outItem = 0;
if (IsEmpty) return false;
outItem = qArray[qFront]; //lay ra o dau
return true;
}
}
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lab7
{
internal class IntNode
{
int data;
IntNode next;

public int Data { get => data; set => data = value; }
internal IntNode Next { get => next; set => next = value; }

public IntNode(int x)
{
data = x;
next = null;
}
}
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lab7
{
internal class ListQueue
{
IntNode front; //first
IntNode rear; //last

internal IntNode Front { get => front; set => front = value; }
internal IntNode Rear { get => rear; set => rear = value; }

public ListQueue()
{
front = rear = null;
}

//2. public bool IsEmpty(): Kiểm tra queue có rỗng không? Trả về true nếu rỗng, ngược lại trả về false.
public bool IsEmpty => front == null;

//3. public bool EnQueue(int x): Thêm phần tử x vào queue theo phương pháp xoay vòng.Nếu thêm thành công thì
trả về true, ngược lại trả về false.
public void EnQueue(int x)
{
IntNode newNode = new IntNode(x);
if (IsEmpty) front = rear = newNode;
else
{
rear.Next = newNode;
rear = newNode;
}
}

//4. public bool DeQueue(out int outItem): Lấy một phần tử ra khỏi queue theo phương pháp xoay vòng và lưu vào
outItem.Nếu lấy ra thành công thì trả về true, ngược lại trả về false.
public bool DeQueue(out int outItem)
{
outItem = 0;
if (IsEmpty) return false;
outItem = front.Data;
IntNode pDel = front;
front = front.Next;
pDel = null;
return true;
}

//5. public bool Top(out int topItem): Xem phần tử ở đỉnh stack và gán vào topItem.Nếu có giá trị trong stack thì trả
về true, ngược lại trả về false.
public bool Top(out int outItem)
{
outItem = 0;
if (IsEmpty) return false;
outItem = front.Data;
return true;
}
}
}
// See https://aka.ms/new-console-template for more information
using Lab7;

#region danh sách liên kết đơn


//void Input(ArrayQueue arrayQueue)
//{
// while (true)
// {
// Console.Write("Mời nhập số nguyên (nhập giá trị âm thì dừng) x: ");
// int x = int.Parse(Console.ReadLine());
// if (x < 0) break;
// if (!arrayQueue.EnQueue(x)) break; //nếu đầy thì dừng
// }
//}
//void Output(ArrayQueue arrayQueue)
//{
// while (!arrayQueue.IsEmpty)
// {
// int x;
// arrayQueue.DeQueue(out x);
// Console.Write($"{x,4}");
// }
//}

//Console.OutputEncoding = System.Text.Encoding.Unicode;
//ArrayQueue arrayQueue = new ArrayQueue(10); //nho la co so 10
//Input(arrayQueue);
////• Cho biết giá trị tại đầu và cuối queue
//Console.WriteLine($"Giá trị đầu : {arrayQueue.QArray[arrayQueue.QFront]}, giá trị cuối :
{arrayQueue.QArray[arrayQueue.QRear]}");
////• Lấy giá trị ra khỏi queue và xuất giá trị tương ứng ra màn hình
//Console.Write("Các giá trị trong Queue: ");
//Output(arrayQueue);
#endregion

#region
void Input(ListQueue listQueue)
{
while (true)
{
Console.Write("Mời nhập số nguyên (nhập giá trị âm thì dừng) x: ");
int x = int.Parse(Console.ReadLine());
if (x < 0) break;
listQueue.EnQueue(x);
}
}
void Output(ListQueue listQueue)
{
while (!listQueue.IsEmpty)
{
int x;
listQueue.DeQueue(out x);
Console.Write($"{x,4}");
}
}

Console.OutputEncoding = System.Text.Encoding.Unicode;
ListQueue listQueue = new ListQueue(); //nho la khong co so 10
Input(listQueue);
//• Cho biết giá trị tại đầu và cuối queue
Console.WriteLine($"Giá trị đầu : {listQueue.Front.Data}, giá trị cuối : {listQueue.Rear.Data}");
//• Lấy giá trị ra khỏi queue và xuất giá trị tương ứng ra màn hình
Console.Write("Các giá trị trong Queue: ");
Output(listQueue);
#endregion
Console.ReadKey();

You might also like