Finally discovered the problem of the program

xiaoxiao2021-04-04  290

Today, I have been busy, under my heart, I finally discovered the problem of the program.

The problem is in the class of an analog stack, the program is as follows:

Using system.collections; using system.text;

Namespace Arithmetic {public class stack {///

///// private arraylist mystack; /// /// initialization /// Public Stack () {Mystack = new arraylist ();

///

/// 入 栈 /// /// into the stack object public void push (Object O) {mystack.add (o) }

///

/// out of the stack /// /// Return Object public object pop () {int count = MyStack.count - 1; if (count <0 ) {Throw new exception;} Object o = mystack [count]; MyStack.remove (o); // The problem is Return O;}

///

/// Judgment stack empty /// /// BOOL value public bool iSempty () {return (MyStack.count == 0);}

///

/// Return Stack Top Object /// /// Return Stack Top Object Public Object TopItem () {INT Count = MyStack.count - 1; IF (count <0) {throw new exception ("stack empty");} Object o = mystack [count]; returno}}}

When I use this class analog stack, there is no mistake at other places, it is removed in the stack, which is remove (O), this method is to delete the first matching item, so when there is the same item in the stack, The stack will be wrong and change it to:

MyStack.removeat (count) is OK.

转载请注明原文地址:https://www.9cbs.com/read-131910.html

New Post(0)