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 Stack 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: 177607" data-attributes="member: 165510"><p><a href="https://vnkienthuc.com/forums/code.377/" target="_blank"><span style="font-size: 18px">Lớp Stack</span></a><span style="font-size: 18px"> trong C# biểu diễn một tập hợp Last-in, First-out của các đối tượng.</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 Last-in, First-out. Khi bạn thêm một item vào trong danh sách, nó được gọi là pushing và khi bạn gỡ bỏ một item, nó được gọi làpopping.</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px">Ghi chú: Stack: ngăn xếp, push: thêm nút mới vào đỉnh stack, pop: thao tác lấy 1 phần tử từ đỉnh stack.</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><span style="color: #0000ff"><strong>Phương thức và Thuộc tính của lớp Stack trong C#</strong></span></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><strong>*Thuộc tính được sử dụng phổ biến của lớp Stack trong C#:</strong></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><a href="https://vnkienthuc.com/threads/tim-hieu-ve-lop-queue-trong-c.77383/" target="_blank"><em><strong>Count:</strong></em></a> Lấy số phần tử được chứa trong Stack</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><strong>*Bảng dưới liệt kê các phương thức được sử dụng phổ biến của lớp Stack trong C#:</strong></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><em><strong>1. public virtual void Clear();</strong></em></span></p><p><span style="font-size: 18px">Gỡ bỏ tất cả phần tử từ Stack</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><strong><em>2. public virtual bool Contains(object obj);</em></strong></span></p><p><span style="font-size: 18px">Gỡ bỏ tất cả phần tử từ Stack</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><em><strong>3. public virtual object Peek();</strong></em></span></p><p><span style="font-size: 18px">Trả về một đối tượng tại trên cùng của Stack mà không gỡ bỏ nó</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><strong><em>4. public virtual object Pop();</em></strong></span></p><p><span style="font-size: 18px">Gỡ bỏ và trả về đối tượng tại trên cùng của Stack</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><strong><em>5. public virtual void Push(object obj);</em></strong></span></p><p><span style="font-size: 18px">Chèn một object tại trên cùng của Stack</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><strong><em>6. public virtual object[] ToArray();</em></strong></span></p><p><span style="font-size: 18px">Sao chép Stack tới một mảng mới</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">Dưới đây là ví dụ minh họa các thuộc tính và<a href="https://vnkienthuc.com/threads/tim-hieu-ve-phuong-thuc-nac-danh-trong-c.77386/" target="_blank"> phương thức của lớp Stack trong C#</a>:</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 Stack 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"> Stack st = new Stack();</span></em></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> st.Push('A');</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> st.Push('M');</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> st.Push('G');</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> st.Push('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 Stack hien tai: ");</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> foreach (char c in st)</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.Write(c + " ");</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"></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"></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> st.Push('V');</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> st.Push('H');</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> Console.WriteLine("Gia tri co the pop tiep theo trong Stack la: {0}", st.Peek());</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> Console.WriteLine("2 - Hien thi Stack hien tai: ");</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> foreach (char c in st)</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.Write(c + " ");</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"></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"></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> Console.WriteLine("Xoa cac gia tri!!!");</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> st.Pop();</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> st.Pop();</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> st.Pop();</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("3 - Hien thi Stack hien tai: ");</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> foreach (char c in st)</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.Write(c + " ");</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.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"><span style="color: #5900b3"><em>}</em></span></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/stack.PNG" alt="" class="fr-fic fr-dii fr-draggable " data-size="" style="" /></span></p></blockquote><p></p>
[QUOTE="uocmo_kchodoi, post: 177607, member: 165510"] [URL='https://vnkienthuc.com/forums/code.377/'][SIZE=5]Lớp Stack[/SIZE][/URL][SIZE=5] trong C# biểu diễn một tập hợp Last-in, First-out của các đối tượng. Nó được sử dụng khi bạn cần truy cập các item theo dạng Last-in, First-out. Khi bạn thêm một item vào trong danh sách, nó được gọi là pushing và khi bạn gỡ bỏ một item, nó được gọi làpopping. Ghi chú: Stack: ngăn xếp, push: thêm nút mới vào đỉnh stack, pop: thao tác lấy 1 phần tử từ đỉnh stack. [COLOR=#0000ff][B]Phương thức và Thuộc tính của lớp Stack trong C#[/B][/COLOR] [B]*Thuộc tính được sử dụng phổ biến của lớp Stack trong C#:[/B] [URL='https://vnkienthuc.com/threads/tim-hieu-ve-lop-queue-trong-c.77383/'][I][B]Count:[/B][/I][/URL] Lấy số phần tử được chứa trong Stack [B]*Bảng dưới liệt kê các phương thức được sử dụng phổ biến của lớp Stack trong C#:[/B] [I][B]1. public virtual void Clear();[/B][/I] Gỡ bỏ tất cả phần tử từ Stack [B][I]2. public virtual bool Contains(object obj);[/I][/B] Gỡ bỏ tất cả phần tử từ Stack [I][B]3. public virtual object Peek();[/B][/I] Trả về một đối tượng tại trên cùng của Stack mà không gỡ bỏ nó [B][I]4. public virtual object Pop();[/I][/B] Gỡ bỏ và trả về đối tượng tại trên cùng của Stack [B][I]5. public virtual void Push(object obj);[/I][/B] Chèn một object tại trên cùng của Stack [B][I]6. public virtual object[] ToArray();[/I][/B] Sao chép Stack tới một mảng mới [B]Ví dụ[/B] Dưới đây là ví dụ minh họa các thuộc tính và[URL='https://vnkienthuc.com/threads/tim-hieu-ve-phuong-thuc-nac-danh-trong-c.77386/'] phương thức của lớp Stack trong C#[/URL]: [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 Stack trong C#");[/COLOR][/I] [I][COLOR=#5900b3] Console.WriteLine("-------------------------------------");[/COLOR][/I] [I][COLOR=#5900b3] SortedList ht = new SortedList();[/COLOR][/I] [I][COLOR=#5900b3] Stack st = new Stack();[/COLOR][/I] [I][COLOR=#5900b3] st.Push('A');[/COLOR][/I] [I][COLOR=#5900b3] st.Push('M');[/COLOR][/I] [I][COLOR=#5900b3] st.Push('G');[/COLOR][/I] [I][COLOR=#5900b3] st.Push('W');[/COLOR][/I] [I][COLOR=#5900b3] Console.WriteLine("1 - Hien thi Stack hien tai: ");[/COLOR][/I] [I][COLOR=#5900b3] foreach (char c in st)[/COLOR][/I] [I][COLOR=#5900b3] {[/COLOR][/I] [I][COLOR=#5900b3] Console.Write(c + " ");[/COLOR][/I] [I][COLOR=#5900b3] }[/COLOR][/I] [I][COLOR=#5900b3] Console.WriteLine();[/COLOR][/I] [I][COLOR=#5900b3] st.Push('V');[/COLOR][/I] [I][COLOR=#5900b3] st.Push('H');[/COLOR][/I] [I][COLOR=#5900b3] Console.WriteLine("Gia tri co the pop tiep theo trong Stack la: {0}", st.Peek());[/COLOR][/I] [I][COLOR=#5900b3] Console.WriteLine("2 - Hien thi Stack hien tai: ");[/COLOR][/I] [I][COLOR=#5900b3] foreach (char c in st)[/COLOR][/I] [I][COLOR=#5900b3] {[/COLOR][/I] [I][COLOR=#5900b3] Console.Write(c + " ");[/COLOR][/I] [I][COLOR=#5900b3] }[/COLOR][/I] [I][COLOR=#5900b3] Console.WriteLine();[/COLOR][/I] [I][COLOR=#5900b3] Console.WriteLine("Xoa cac gia tri!!!");[/COLOR][/I] [I][COLOR=#5900b3] st.Pop();[/COLOR][/I] [I][COLOR=#5900b3] st.Pop();[/COLOR][/I] [I][COLOR=#5900b3] st.Pop();[/COLOR][/I] [I][COLOR=#5900b3] Console.WriteLine("3 - Hien thi Stack hien tai: ");[/COLOR][/I] [I][COLOR=#5900b3] foreach (char c in st)[/COLOR][/I] [I][COLOR=#5900b3] {[/COLOR][/I] [I][COLOR=#5900b3] Console.Write(c + " ");[/COLOR][/I] [I][COLOR=#5900b3] }[/COLOR][/I] [I][COLOR=#5900b3] Console.ReadKey();[/COLOR][/I] [I][COLOR=#5900b3] }[/COLOR][/I] [I][COLOR=#5900b3] }[/COLOR][/I] [COLOR=#5900b3][I]}[/I][/COLOR] 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/stack.PNG[/IMG][/SIZE][/CENTER] [/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 Stack trong C#
Top