A Stack is a linear table that is limited to insert or delete operations at the end of the table. System. Stack indicates a simple post-first-out non-generic set of objects. Major Stack operations include: Element stack entry, element stack exit, stack clearing, determining whether the stack is empty, and obtaining the number of stack elements.
The ShowStackOperation () function in the following program code demonstrates the five common stack operations described above.
Private void ShowStackOperation ()
{
Stack s = new Stack (); // create a Stack s
S. Push ("Hello word."); // element "Hello word." Incoming Stack
S. Push (1); // element into Stack
S. Pop (); // element output Stack
Int stackcount = S. Count; // gets the number of elements in the stack.
Bool isempty = S. Count> 0? False: true; // determines whether the stack is empty.
S. Clear (); // clear all elements of the stack
}
In the following program code, the author creates a class stackrealizequeue. This class uses two stacks to implement a queue (a first-in-first-out linear table ). Common queue operations are also implemented, such as element queuing, element queuing, clearing the queue, determining whether the queue is empty, and obtaining the number of queue elements.
The constructor of the class stackrealizequeue initializes two stacks. The fstack stack stores the queue value, and the sstack is the standby stack, which is used when the queue leaves the queue. It is easy to clear the queue, determine whether the queue is empty, and obtain the number of queue elements. The program code is as follows:
/// <Summary>
/// Use two stacks to implement a queue
/// </Summary>
Public class stackrealizequeue
{
/// <Summary>
/// Value of the fstack storage queue
/// </Summary>
Stack fstack;
/// <Summary>
/// Backup stack, used when the queue leaves the queue
/// </Summary>
Stack sStack;
/// <Summary>
/// Constructor, initializing fStack and sStack
/// </Summary>
Public StackRealizeQueue ()
{
FStack = new Stack ();
SStack = new Stack ();
}
/// <Summary>
/// Clear the queue
/// </Summary>
Public void Crear ()
{
FStack. Clear ();
}
/// <Summary>
/// Obtain the queue length
/// </Summary>
Public int Length
{
Get {return fStack. Count ;}
}
/// <Summary>
/// Determine whether the queue is empty
/// </Summary>
Public bool IsEmpty ()
{
Return (fstack. Count <= 0 & sstack. Count <= 0 );
}
The following analyzes the principles of two stacks to implement one queue. The elements of the queue are set to "1, 2, 3, 4, 5, 6, 7, 8, 9, 10" in sequence, so the order of the elements is "1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ".
When
When the elements "," are all written into the fstack, the order of the elements is the opposite of that of the sorting order, that is,
4, 3, 2, 1 ". The sstack stack is empty, as shown in Figure 2.26. In order to let the element "1" out of the queue, you can order the elements "10, 9, 6, 5, 4, 3, 2,
1 "pop up from the fstack stack and press it into the stack sstack, as shown in 2.27. Then the element "1" is displayed. At this time, the elements "10, 9, 8, 7, 6, 5,
"4, 3, 2" pops up from the sstack stack and is pushed into the stack fstack, as shown in Figure 2.28.
Figure 2.26 element team-up figure 2.27 element "1" Prepare team-out figure 2.28 element "1" after team-out
Based on the above analysis, you can define the queuing and queuing operations. When an element enters the queue, as the element of the queue is only stored in the fstack of the stack, you only need to add the element to the fstack of the stack. The program code for queue operations is as follows:
/// <Summary>
/// Element
/// </Summary>
Public void EnQueue (object oValue)
{
FStack. Push (oValue );
}
When
When an element leaves the queue, the queue elements are only stored in the stack fStack. First, all the elements in stack fStack are popped up and pushed to stack sStack. Then, stack sStack
The top element outputs the stack and outputs this element. Finally, all the elements of the sStack Stack are re-pushed to the fStack stack to complete the queuing operation. Queue queue
The Code is as follows:
/// <Summary>
/// Element
/// </Summary>
Public object DeQueue ()
{
If (IsEmpty () = true) {return null ;}
If (sStack. Count> 0) {return null ;}
/// Press all elements of the fStack stack into the sStack.
While (fStack. Count> 0)
{
SStack. Push (fStack. Pop ());
}
Object oTemp = sStack. Pop (); // obtain the top element of the sStack stack.
/// Re-press all elements of the sStack stack into the fStack.
While (sStack. Count> 0)
{
FStack. Push (sStack. Pop ());
}
Return oTemp;
}
}
Ying
Use the Page_Load (object sender, EventArgs
E) First, call the TestStackRealizeQueue () function to create a queue and add four elements to the queue. These elements are "true", "$ ",
"34567" and "Hello Word", and then print all elements in the queue. Function Page_Load (object sender, EventArgs
E) The program code is as follows:
Protected void Page_Load (object sender, EventArgs e)
{
TestStackRealizeQueue (); Response. End ();
}
Private void TestStackRealizeQueue ()
{// Create a queue
StackRealizeQueue queue = new StackRealizeQueue ();
Queue. Crear ();
Bool B = true;
Char c = '$ ';
Int I = 34567;
String s = "Hello Word ";
/// Enter the team
Queue. EnQueue (B );
Queue. EnQueue (c );
Queue. EnQueue (I );
Queue. EnQueue (s );
/// Print all elements of the queue
While (queue. IsEmpty () = false)
{
Response. Write (queue. DeQueue (). ToString () + "<br> ");
}
}
Set StackPage. aspx to the start page of the application Sample_02_06 and run the application, as shown in StackPage. aspx page 2.29.
Figure 2.29 StackPage. aspx the double stack operation result is displayed.