1. Knowledge points involved
1. Stack definition
A stack is an important data structure. From the perspective of data structure, the stack is also a linear table. Its speciality is that the basic operations of the stack are subsets of linear table operations. They are linear tables with limited operations, so they can be called limited data structures.
A stack is a linear table that limits insertion or deletion operations only at the end of the table. Therefore, for the stack, the end of the table has its own special meaning, which is called "top" and correspondingly, the end of the table is called "bottom" and an empty table without elements is called an empty stack. The modification of the stack is carried out according to the principle of back-in-first-out. Therefore, the stack is also called a linear table with last in first out. This feature must be paid attention to when implementing the data structure of the stack. In addition to inserting or deleting it at the top of the stack, the basic operations of the stack also include initialization of the stack, determining whether it is empty and taking elements on the top of the stack.
<T>Class
The stack in C# is defined by the Stack class in the namespace. The Stack class provides properties and methods that simulate the stack, such as Push (add elements to the top of the stack), Pop (removing elements from the top of the stack), and Peek (see elements at the top of the stack).
2. Use Stack<T> class for stack design
To create a stack using Stack class, you first need to create a new Stack instance using the following syntax:
1. Create a new Stack<T> instance
Stack<T> stack = new Stack<T>(); //T is the type of element you want to store in the stack.//For example, if you want to store integers in the stack, you can use the following code:Stack<int> stack = new Stack<int>();
2. Then, you can add elements to the stack using the Push method
(10); (20); (30); //This will add three integers at the top of the stack10、20and30。
To remove an element from the stack, you can use the Pop method, which deletes an element from the top of the stack and returns it.
3. Use the Pop method to delete an element from the top of the stack
int item = (); //The last added element (30) will be removed from the stack and stored in the variable item.
To view the element at the top of the stack without removing it, you can use the Peek method.
4. Use the Peek method to view elements at the top of the stack
int item = (); //Returns the element at the top of the stack(20),But it will not be removed from the stack。
The Stack class also provides other methods and properties, such as the Count property (returns the number of elements in the stack) and the Clear method (clears all elements in the stack).
III. Example
// Use CThe Stack class implementation stack in #namespace _133_1{ class Program { static void Main(string[] args) { (args); // Create a new integer stack Stack<int> stack = new(); // Add some elements to the stack (10); (20); (30); // View elements at the top of the stack int item2 = (); ("The element at the top of the stack is: " + item2); // Output: The element at the top of the stack is: 30 // Get the number of elements in the stack int count = ; ("The number of elements in the stack is: " + count); // Pop up elements from the stack and print them while ( > 0) { int item1 = (); (item1); } // Get the number of elements in the stack count = ; ("The number of elements in the stack is: " + count); (40); (50); (60); // Get the number of elements in the stack count = ; ("The number of elements in the stack is: " + count); // Clear all elements in the stack (); // Check whether the stack is empty if ( == 0) { ("The stack is empty."); } } } } //Run result:/* The elements at the top of the stack are: 30 The number of elements in the stack is: 3 30 20 10 The number of elements in the stack is: 0 The number of elements in the stack is: 3 The stack is empty. */
This is the end of this article about C# using Stack<T> class for stack design. For more related C# Stack<T> stack content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!