Stack and queue are the most basic data structures. We are very familiar with their features and usage. Today, I want to describe how to customize and implement the data structure of the stack. This helps us to have a deep understanding of the principles of the data structure of the stack and to better study other data structure types.
Overview:
Custom implementation can dynamically adjust the generic stack type and maintain good performance.
Implementation:
1. First, we implement a type of fixed-volume generic stack. After the stack is created, the size is fixed and it is very easy to implement.
public class FixedCapacityStack<T> { private T[] a; private int N; public FixedCapacityStack(int cap) { a = new T[cap]; } public bool isEmpty() { return N == 0; } public int Size() { return N; } public void Push(T item) { a[N++] = item; } public T Pop() { return a[--N]; }
}
This section of C # code successfully implements a generic stack, but its capacity is fixed. When its size is far smaller than or greater than the capacity, it will cause a waste of memory resources and exceptions. This requires a variable and flexible stack type.
2. Next we will implement a variable-volume generic stack type, so that we can achieve dynamic scaling.
public class FixedCapacityStack<T> { private T[] a; private int N; public FixedCapacityStack(int cap) { a = new T[cap]; } public bool isEmpty() { return N == 0; } public void Resize(int max) { T[] temp = new T[max]; for (int i = 0; i < a.Length; i++) temp[i] = a[i]; a = temp; } public int Size() { return N; } public void Push(T item) { if (N == a.Length) Resize(2 * a.Length); a[N++] = item; } public T Pop() { T item= a[--N]; if (N>0&&N == a.Length / 4) Resize(a.Length / 2); return item; } }
3. Further implement the stack type that can be iterated, and implement the stack Type Iteration feature according to the design intent. Public class fixedcapacitystack <t>: ienumerable
{ private T[] a; private int N; public FixedCapacityStack(int cap) { a = new T[cap]; } public bool isEmpty() { return N == 0; } public void Resize(int max) { T[] temp = new T[max]; int min=Min(a.Length,temp.Length); for (int i = 0; i < min; i++) temp[i] = a[i]; a = temp; } public int Min(int x, int y) { if (x < y) return x; else return y; } public int Size() { return N; } public void Push(T item) { if (N == a.Length) Resize(2 * a.Length); a[N++] = item; } public T Pop() { T item= a[--N];
a[N]=null; if (N>0&&N == a.Length / 4) Resize(a.Length / 2); return item; } public IEnumerator GetEnumerator() { for (int i = a.Length-1; i >=0; i--) { yield return a[i]; } } }
Application:
We can test the actual running effect through actual calls. The Code is as follows:
class Program { static void Main(string[] args) { /* string testExpress = "(1+((2+3)*(4*5)))"; Console.WriteLine(Evaluate(testExpress));*/ FixedCapacityStack<string> s = new FixedCapacityStack<string>(100); for (int i = 0; i < 150; i++) s.Push(i.ToString()); Console.WriteLine(s.Size()); for(int j=0;j<120;j++) s.Pop(); foreach (var item in s) { Console.WriteLine(item); } Console.WriteLine(s.Size()); }}
So far, we have implemented an iterative and variable-Tolerant Model stack type, so we can understand and use the stack type more clearly.
class Program { static void Main(string[] args) { /* string testExpress = "(1+((2+3)*(4*5)))"; Console.WriteLine(Evaluate(testExpress));*/ FixedCapacityStack<string> s = new FixedCapacityStack<string>(100); for (int i = 0; i < 150; i++) s.Push(i.ToString()); Console.WriteLine(s.Size()); for(int j=0;j<120;j++) s.Pop(); foreach (var item in s) { Console.WriteLine(item); } Console.WriteLine(s.Size()); }}
Here I have a question: Can I change the value of a fan array to null? In this way, no pop release can be performed. If you have any solutions, please refer to the following comments. Thank you.
Data structure (1) custom implementation of stack