A stack and a queue are two table-oriented data structures that provide easy-to-understand abstraction. The data in the stack can be added and deleted only at one end of the table. The data in the queue is added at one end of the table and deleted at the other end of the table. Stacks are widely used in the implementation of any programming language from expression calculation to function calling. Queues are used to process and simulate real-world events in a prioritized operating system, such as the bank cashier's counter queue and elevator operations in buildings. C # provides two types of data structures: Stack and queue.
Class.
1. Stack. Stack is one of the most frequently used data structures. The stack is defined as a list of data items, and these data items can only be accessed from the end of the table. This end that can be accessed is called the top of the stack. The standard model of the stack is the plate heap of the cafeteria. People always need to take the plate from the top, and put it on the top of the plate heap when the picker or handyman puts the plate back into the plate heap. Stack is a famous post-first-out (LIFO) data structure. The two most basic operations on a stack are to add data items to the stack and delete data items from the stack. The push operation adds data items to the stack. The pop (Out stack) operation is used to remove data items from the stack. Another basic operation of the stack is to view the data items at the top of the stack. Pop
The operation returns the data item at the top of the stack, but this operation also removes the data item from the stack. If you only want to view the data item at the top of the stack rather than removing it, there is an operation named peek in the C # language. Of course, this operation may use other names (such as top) in other languages and implementations ). Stack entry, stack exit, and fetch are all basic operations performed when the stack is used. However, there are other operations to be performed and attributes to be checked. It is very useful to remove all data items from the stack. Call the clear operation to clear all stacks. In addition, it is useful to know the number of data items in the stack at any time. This can be achieved by calling the Count attribute. Many implementations have stackempty
Method. This method returns a true value or a false value based on the stack status, but the Count attribute can also be used for the same purpose .. The Stack class of the net framework implements all these operations and attributes, and even more. But before discussing how to use them, let's take a look at how to implement a stack without a stack class.
(1) stack implementation requires a potential structure to store data. Since you do not need to worry about adjusting the table size when adding new data items to the stack, select arraylist. Because the C # language has such powerful Object-Oriented Programming features, the stack will be implemented as a class here. This type is called cstack. A constructor method and a method for the operations mentioned above are also included. To illustrate the implementation process in C #, the Count attribute is implemented as an attribute. The most important variable is the arraylist object used to store stack data items. In addition, the data that needs to be concerned is the top of the stack. Here we will use a simple integer variable for processing to provide a function similar to the index. When a new cstack
When an object is instantiated, the initial value of this variable is set to-1. Each time a new data item is pushed into the stack, the variable is automatically added with 1. The constructor method only initializes the index variable to-1. The first method is push. The program calls the add method of arraylsit and adds the value passed to it to the arraylist. The pop method does three things: Call the removeat method to remove the data items at the top of the stack (from the arraylist), perform the index variable auto-minus 1 operation, and finally return the stack objects. The peek method is implemented by calling the item method containing index variables as parameters. The clear method simply calls the arraylist
Class. Since you do not need to change the number of data items on the stack, write the Count attribute as a read-only attribute here. The Code is as follows:
class CStack{ private int p_index; private ArrayList list; public CStack() { list = new ArrayList(); p_index = -1; } public int count { get { return list.Count; } } public void push(object item) { list.Add(item); p_index++; } public object pop() { object obj = list[p_index]; list.RemoveAt(p_index); p_index--; return obj; } public void clear() { list.Clear(); p_index = -1; } public object peek() { return list[p_index]; }}
The following is a text-back test:
static void Main(string[] args){ CStack alist = new CStack(); string ch; string word = "sees"; bool isPalindrome = true; for (int x = 0; x < word.Length; x++) alist.push(word.Substring(x, 1)); int pos = 0; while (alist.count > 0) { ch = alist.pop().ToString(); if (ch != word.Substring(pos, 1)) { isPalindrome = false; break; } pos++; } if (isPalindrome) Console.WriteLine(word + " is a palindrome."); else Console.WriteLine(word + " is not a palindrome."); Console.Read();}
(2) The most important operations on the stack are push and pop. Use the push method to add data to the stack. Use Pop to remove data from the stack. The following example uses a stack to calculate a simple arithmetic expression to understand these methods.
using System;using System.Collections;using System.Text.RegularExpressions;namespace csstack{ class Class1 { static void Main(string[] args) { Stack nums = new Stack(); Stack ops = new Stack(); string expression = "5 + 10 + 15 + 20"; Calculate(nums, ops, expression); Console.WriteLine(nums.Pop()); Console.Read(); } // IsNumeric isn't built into C# so we must define it static bool IsNumeric(string input) { bool flag = true; string pattern = (@"^\d+{1}quot;); Regex validate = new Regex(pattern); if (!validate.IsMatch(input)) { flag = false; } return flag; } static void Calculate(Stack N, Stack O, string exp) { string ch, token = ""; for (int p = 0; p < exp.Length; p++) { ch = exp.Substring(p, 1); if (IsNumeric(ch)) token += ch; //+= if (ch == " " || p == (exp.Length - 1)) { if (IsNumeric(token)) { N.Push(token); token = ""; } } else if (ch == "+" || ch == "-" || ch == "*" || ch == "/") O.Push(ch); if (N.Count == 2) Compute(N, O); } } static void Compute(Stack N, Stack O) { int oper1, oper2; string oper; oper1 = Convert.ToInt32(N.Pop()); oper2 = Convert.ToInt32(N.Pop()); oper = Convert.ToString(O.Pop()); switch (oper) { case "+": N.Push(oper1 + oper2); break; case "-": N.Push(oper1 - oper2); break; case "*": N.Push(oper1 * oper2); break; case "/": N.Push(oper1 / oper2); break; } } }}
(3). Common stack methods.
Peek method. The peek method allows people to see the value of the top data item of the stack without removing the data item from the stack.
Clear method. The clear method removes all data items from the stack and sets the data item counter to zero.
Contains method. The contains method is used to determine whether the specified element is in the stack. If this element is found, true is returned for this method; otherwise, false is returned.
Copyto method. The copyto method copies the content in the stack to an array. The array must be of the object type, because this is the data type of all stack objects. This method removes two parameters: An array and the starting index of the array that begins to place the stack element. The elements in the stack are copied in the order of lifo, as if they were operated out of the stack.
Toarray method. The working principle of the toarray method is similar to that of the copyto method. However, you cannot specify the starting index position of an array. Instead, you must create an array in the value assignment statement.