Trang chủ
Bài viết mới
Diễn đàn
Bài mới trên hồ sơ
Hoạt động mới nhất
VIDEO
Mùa Tết
Văn Học Trẻ
Văn Học News
Media
New media
New comments
Search media
Đại Học
Đại cương
Chuyên ngành
Triết học
Kinh tế
KHXH & NV
Công nghệ thông tin
Khoa học kĩ thuật
Luận văn, tiểu luận
Phổ Thông
Lớp 12
Ngữ văn 12
Lớp 11
Ngữ văn 11
Lớp 10
Ngữ văn 10
LỚP 9
Ngữ văn 9
Lớp 8
Ngữ văn 8
Lớp 7
Ngữ văn 7
Lớp 6
Ngữ văn 6
Tiểu học
Thành viên
Thành viên trực tuyến
Bài mới trên hồ sơ
Tìm trong hồ sơ cá nhân
Credits
Transactions
Xu: 0
Đăng nhập
Đăng ký
Có gì mới?
Tìm kiếm
Tìm kiếm
Chỉ tìm trong tiêu đề
Bởi:
Hoạt động mới nhất
Đăng ký
Menu
Đăng nhập
Đăng ký
Install the app
Cài đặt
Chào mừng Bạn tham gia Diễn Đàn VNKienThuc.com -
Định hướng Forum
Kiến Thức
- HÃY TẠO CHỦ ĐỀ KIẾN THỨC HỮU ÍCH VÀ CÙNG NHAU THẢO LUẬN Kết nối:
VNK X
-
VNK groups
| Nhà Tài Trợ:
BhnongFood X
-
Bhnong groups
-
Đặt mua Bánh Bhnong
CÔNG NGHỆ
Công Nghệ Thông Tin
Code
Tìm hiểu về lớp Queue trong C#
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Trả lời chủ đề
Nội dung
<blockquote data-quote="uocmo_kchodoi" data-source="post: 177635" data-attributes="member: 165510"><p><a href="https://vnkienthuc.com/forums/code.377/" target="_blank"> <span style="font-size: 18px">Queue </span></a><span style="font-size: 18px">(hay còn gọi là hàng đợi) là một cấu trúc dữ liệu hoạt động theo nguyên lý FIFO(First In First Out). Người ta hay gọi nó là hàng đợi bởi vì nó hoạt động giống như việc xếp hàng đợi chờ gì đó trong thực tế vậy.</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px">Nó được sử dụng khi bạn cần truy cập các item theo dạng First-in, First-out. Khi bạn thêm một item vào trong danh sách, nó được gọi là enqueue và khi bạn gỡ bỏ một item, nó được gọi làdeque.</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><span style="color: #0000ff"><strong>1. Đặc điểm của Queue</strong></span></span></p><ul> <li data-xf-list-type="ul"><span style="font-size: 18px">Là một <a href="https://vnkienthuc.com/threads/tim-hieu-ve-lop-bitarray-trong-c.77384/" target="_blank">danh sách lưu trữ </a>các đối tượng nhưng không thể truy cập các phần tử thông qua chỉ số phần tử được.</span></li> <li data-xf-list-type="ul"><span style="font-size: 18px">Hành động thêm phần tử vào Queue được gọi là Enqueue (xếp hàng).</span></li> <li data-xf-list-type="ul"><span style="font-size: 18px">Hành động lấy phần tử ra khỏi Queue được gọi là Dequeue (ra khỏi hàng). Và luôn luôn lấy ra phần tử được thêm vào đầu tiên.</span></li> </ul><p><span style="font-size: 18px">Queue rất giống Stack chỉ khác ở nguyên lý hoạt động thôi nên Stack có gì Queue sẽ có cái tương tự như vậy.</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px">Do Queue cũng là 1 Collections nên để sử dụng ta cần thêm thư viện System.Collectionsbằng câu lệnh:</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px">using System.Collections;</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px">Trước khi sử dụng ta cần khởi tạo vùng nhớ bằng toán tử new:</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">Queue MyQueue = new Queue(); // khởi tạo 1 Queue rỗng</span></em></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px">Bạn cũng có chỉ định sức chứa (Capacity) ngay lúc khởi tạo bằng cách thông qua constructor được hỗ trợ sẵn:</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">Queue MyQueue2 = new Queue(5); // khởi tạo 1 Queue và chỉ định sức chứa ban đầu là 5</span></em></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px">Bạn cũng có thể<a href="https://vnkienthuc.com/threads/tim-hieu-ve-generic-trong-c.77387/" target="_blank"> khởi tạo 1 Queue </a>chứa các phần tử được sao chép từ một danh sách khác:</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px">// khởi tạo 1 mảng bất kỳ</span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">ArrayList MyArray = new ArrayList();</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">MyArray.Add(5);</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">MyArray.Add(9);</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">MyArray.Add(10);</span></em></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px">// Khởi tạo 1 Queue và sao chép giá trị của các phần tử từ MyArray vào Queue.</span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">Queue MyQueue3 = new Queue(MyArray);</span></em></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><span style="color: #0000ff"><strong>2. Phương thức và Thuộc tính của lớp Queue trong C#</strong></span></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><em><strong>Dưới đây liệt kê các thuộc tính được sử dụng phổ biến của lớp Queue trong C#:</strong></em></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><span style="color: #ff8000">Count</span>: Lấy số phần tử được chứa trong Queue</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><strong><em>Dưới đây liệt kê các phương thức được sử dụng phổ biến của lớp Queue trong C#:</em></strong></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><span style="color: #ff8000">1 public virtual void Clear();</span></span></p><p><span style="font-size: 18px">Gỡ bỏ tất cả phần tử từ Queue</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><span style="color: #ff8000">2 public virtual bool Contains(object obj);</span></span></p><p><span style="font-size: 18px">Xác định có hay không một phần tử là trong Queue</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><span style="color: #ff8000">3 public virtual object Dequeue();</span></span></p><p><span style="font-size: 18px">Gỡ bỏ và trả về đối tượng tại phần bắt đầu của Queue</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><span style="color: #ff8000">4 public virtual void Enqueue(object obj);</span></span></p><p><span style="font-size: 18px">Thêm một phần tử vào phần cuối của Queue</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><span style="color: #ff8000">5 public virtual object[] ToArray();</span></span></p><p><span style="font-size: 18px">Sao chép Queue tới một mảng mới</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><span style="color: #ff8000">6 public virtual void TrimToSize();</span></span></p><p><span style="font-size: 18px">Thiết lập capacity tới số phần tử thực sự trong Queue</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><strong>Ví dụ</strong></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">using System;</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">using System.Collections;</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">namespace VietJackCsharp</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">{</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> class TestCsharp</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> {</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> static void Main(string[] args)</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> {</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> Console.WriteLine("Vi du minh hoa Queue trong C#");</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> Console.WriteLine("-------------------------------------");</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> SortedList ht = new SortedList();</span></em></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> Queue q = new Queue();</span></em></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> q.Enqueue('A');</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> q.Enqueue('M');</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> q.Enqueue('G');</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> q.Enqueue('W');</span></em></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> Console.WriteLine("1 - Hien thi Queue hien tai: ");</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> foreach (char c in q) Console.Write(c + " ");</span></em></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> Console.WriteLine();</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> q.Enqueue('V');</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> q.Enqueue('H');</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> Console.WriteLine("2 - Hien thi Queue hien tai: ");</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> foreach (char c in q) Console.Write(c + " ");</span></em></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> Console.WriteLine();</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> Console.WriteLine("Xoa mot so gia tri!!!");</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> char ch = (char)q.Dequeue();</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> Console.WriteLine("Gia tri bi xoa la: {0}", ch);</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> ch = (char)q.Dequeue();</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> Console.WriteLine("Gia tri bi xoa la: {0}", ch);</span></em></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> Console.ReadKey();</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> }</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> }</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">}</span></em></span></p><p><span style="font-size: 18px">Nếu bạn không sử dụng lệnh Console.ReadKey(); thì chương trình sẽ chạy và kết thúc luôn (nhanh quá đến nỗi bạn không kịp nhìn kết quả). Lệnh này cho phép chúng ta nhìn kết quả một cách rõ ràng hơn.</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px">Biên dịch và chạy chương trình C# trên sẽ cho kết quả sau:</span></p><p><span style="font-size: 18px"></span></p><p style="text-align: center"><span style="font-size: 18px"><img src="https://vietjack.com/csharp/images/queue.PNG" alt="" class="fr-fic fr-dii fr-draggable " data-size="" style="" /></span></p><p></p><p style="text-align: right"><span style="font-size: 18px">Nguồn: Sưu tầm</span></p></blockquote><p></p>
[QUOTE="uocmo_kchodoi, post: 177635, member: 165510"] [URL='https://vnkienthuc.com/forums/code.377/'][B][SIZE=5] [/SIZE][/B][SIZE=5]Queue [/SIZE][/URL][SIZE=5](hay còn gọi là hàng đợi) là một cấu trúc dữ liệu hoạt động theo nguyên lý FIFO(First In First Out). Người ta hay gọi nó là hàng đợi bởi vì nó hoạt động giống như việc xếp hàng đợi chờ gì đó trong thực tế vậy. Nó được sử dụng khi bạn cần truy cập các item theo dạng First-in, First-out. Khi bạn thêm một item vào trong danh sách, nó được gọi là enqueue và khi bạn gỡ bỏ một item, nó được gọi làdeque. [COLOR=#0000ff][B]1. Đặc điểm của Queue[/B][/COLOR][/SIZE] [LIST] [*][SIZE=5]Là một [URL='https://vnkienthuc.com/threads/tim-hieu-ve-lop-bitarray-trong-c.77384/']danh sách lưu trữ [/URL]các đối tượng nhưng không thể truy cập các phần tử thông qua chỉ số phần tử được.[/SIZE] [*][SIZE=5]Hành động thêm phần tử vào Queue được gọi là Enqueue (xếp hàng).[/SIZE] [*][SIZE=5]Hành động lấy phần tử ra khỏi Queue được gọi là Dequeue (ra khỏi hàng). Và luôn luôn lấy ra phần tử được thêm vào đầu tiên.[/SIZE] [/LIST] [SIZE=5]Queue rất giống Stack chỉ khác ở nguyên lý hoạt động thôi nên Stack có gì Queue sẽ có cái tương tự như vậy. Do Queue cũng là 1 Collections nên để sử dụng ta cần thêm thư viện System.Collectionsbằng câu lệnh: using System.Collections; Trước khi sử dụng ta cần khởi tạo vùng nhớ bằng toán tử new: [I][COLOR=#5900b3]Queue MyQueue = new Queue(); // khởi tạo 1 Queue rỗng[/COLOR][/I] Bạn cũng có chỉ định sức chứa (Capacity) ngay lúc khởi tạo bằng cách thông qua constructor được hỗ trợ sẵn: [I][COLOR=#5900b3]Queue MyQueue2 = new Queue(5); // khởi tạo 1 Queue và chỉ định sức chứa ban đầu là 5[/COLOR][/I] Bạn cũng có thể[URL='https://vnkienthuc.com/threads/tim-hieu-ve-generic-trong-c.77387/'] khởi tạo 1 Queue [/URL]chứa các phần tử được sao chép từ một danh sách khác: // khởi tạo 1 mảng bất kỳ [I][COLOR=#5900b3]ArrayList MyArray = new ArrayList();[/COLOR][/I] [I][COLOR=#5900b3]MyArray.Add(5);[/COLOR][/I] [I][COLOR=#5900b3]MyArray.Add(9);[/COLOR][/I] [I][COLOR=#5900b3]MyArray.Add(10);[/COLOR][/I] // Khởi tạo 1 Queue và sao chép giá trị của các phần tử từ MyArray vào Queue. [I][COLOR=#5900b3]Queue MyQueue3 = new Queue(MyArray);[/COLOR][/I] [COLOR=#0000ff][B]2. Phương thức và Thuộc tính của lớp Queue trong C#[/B][/COLOR] [I][B]Dưới đây liệt kê các thuộc tính được sử dụng phổ biến của lớp Queue trong C#:[/B][/I] [COLOR=#ff8000]Count[/COLOR]: Lấy số phần tử được chứa trong Queue [B][I]Dưới đây liệt kê các phương thức được sử dụng phổ biến của lớp Queue trong C#:[/I][/B] [COLOR=#ff8000]1 public virtual void Clear();[/COLOR] Gỡ bỏ tất cả phần tử từ Queue [COLOR=#ff8000]2 public virtual bool Contains(object obj);[/COLOR] Xác định có hay không một phần tử là trong Queue [COLOR=#ff8000]3 public virtual object Dequeue();[/COLOR] Gỡ bỏ và trả về đối tượng tại phần bắt đầu của Queue [COLOR=#ff8000]4 public virtual void Enqueue(object obj);[/COLOR] Thêm một phần tử vào phần cuối của Queue [COLOR=#ff8000]5 public virtual object[] ToArray();[/COLOR] Sao chép Queue tới một mảng mới [COLOR=#ff8000]6 public virtual void TrimToSize();[/COLOR] Thiết lập capacity tới số phần tử thực sự trong Queue [B]Ví dụ[/B] [I][COLOR=#5900b3]using System;[/COLOR][/I] [I][COLOR=#5900b3]using System.Collections;[/COLOR][/I] [I][COLOR=#5900b3]namespace VietJackCsharp[/COLOR][/I] [I][COLOR=#5900b3]{[/COLOR][/I] [I][COLOR=#5900b3] class TestCsharp[/COLOR][/I] [I][COLOR=#5900b3] {[/COLOR][/I] [I][COLOR=#5900b3] static void Main(string[] args)[/COLOR][/I] [I][COLOR=#5900b3] {[/COLOR][/I] [I][COLOR=#5900b3] Console.WriteLine("Vi du minh hoa Queue trong C#");[/COLOR][/I] [I][COLOR=#5900b3] Console.WriteLine("-------------------------------------");[/COLOR][/I] [I][COLOR=#5900b3] SortedList ht = new SortedList();[/COLOR][/I] [I][COLOR=#5900b3] Queue q = new Queue();[/COLOR][/I] [I][COLOR=#5900b3] q.Enqueue('A');[/COLOR][/I] [I][COLOR=#5900b3] q.Enqueue('M');[/COLOR][/I] [I][COLOR=#5900b3] q.Enqueue('G');[/COLOR][/I] [I][COLOR=#5900b3] q.Enqueue('W');[/COLOR][/I] [I][COLOR=#5900b3] Console.WriteLine("1 - Hien thi Queue hien tai: ");[/COLOR][/I] [I][COLOR=#5900b3] foreach (char c in q) Console.Write(c + " ");[/COLOR][/I] [I][COLOR=#5900b3] Console.WriteLine();[/COLOR][/I] [I][COLOR=#5900b3] q.Enqueue('V');[/COLOR][/I] [I][COLOR=#5900b3] q.Enqueue('H');[/COLOR][/I] [I][COLOR=#5900b3] Console.WriteLine("2 - Hien thi Queue hien tai: ");[/COLOR][/I] [I][COLOR=#5900b3] foreach (char c in q) Console.Write(c + " ");[/COLOR][/I] [I][COLOR=#5900b3] Console.WriteLine();[/COLOR][/I] [I][COLOR=#5900b3] Console.WriteLine("Xoa mot so gia tri!!!");[/COLOR][/I] [I][COLOR=#5900b3] char ch = (char)q.Dequeue();[/COLOR][/I] [I][COLOR=#5900b3] Console.WriteLine("Gia tri bi xoa la: {0}", ch);[/COLOR][/I] [I][COLOR=#5900b3] ch = (char)q.Dequeue();[/COLOR][/I] [I][COLOR=#5900b3] Console.WriteLine("Gia tri bi xoa la: {0}", ch);[/COLOR][/I] [I][COLOR=#5900b3] Console.ReadKey();[/COLOR][/I] [I][COLOR=#5900b3] }[/COLOR][/I] [I][COLOR=#5900b3] }[/COLOR][/I] [I][COLOR=#5900b3]}[/COLOR][/I] Nếu bạn không sử dụng lệnh Console.ReadKey(); thì chương trình sẽ chạy và kết thúc luôn (nhanh quá đến nỗi bạn không kịp nhìn kết quả). Lệnh này cho phép chúng ta nhìn kết quả một cách rõ ràng hơn. Biên dịch và chạy chương trình C# trên sẽ cho kết quả sau: [/SIZE] [CENTER][SIZE=5][IMG]https://vietjack.com/csharp/images/queue.PNG[/IMG][/SIZE][/CENTER] [SIZE=5][/SIZE] [RIGHT][SIZE=5]Nguồn: Sưu tầm[/SIZE][/RIGHT] [/QUOTE]
Tên
Mã xác nhận
Gửi trả lời
CÔNG NGHỆ
Công Nghệ Thông Tin
Code
Tìm hiểu về lớp Queue trong C#
Top