Defines a stack class for storing data in the stack and operations on the data!
Class SeqStact <T>
{
Private int maxsize;
Private T [] data;
Private int top;
Public int MaxSize
{
Get {return maxsize ;}
Set {maxsize = value ;}
}
Public int Top
{
Get {return top ;}
Set {top = value ;}
}
Public T this [int index]
{
Get {return data [index];}
Set {data [index] = value ;}
}
Public SeqStact (int size)
{
Maxsize = size;
Top =-1;
Data = new T [size];
}
Public int GetLength ()
{
Return top + 1;
}
Public void Clear ()
{
Top =-1;
}
Public bool IsEmpty ()
{
If (top =-1)
{
Return true;
}
Return false;
}
Public bool IsFull ()
{
If (top + 1 = maxsize)
{
Return true;
}
Return false;
}
Public void Push (T itme)
{
If (IsFull ())
{
Console. WriteLine ("Full ");
Return;
}
Data [++ top] = itme;
}
Public T Pop ()
{
If (IsEmpty ())
{
Console. WriteLine ("Empty ");
Return default (T );
}
T tmp = data [top];
-- Top;
Return tmp;
}
Public T GetTop ()
{
If (IsEmpty ())
{
Console. WriteLine ("Empty ");
Return default (T );
}
Return data [top];
}
Public void List ()
{
If (IsEmpty ())
{
Console. WriteLine ("Empty ");
Return;
}
Console. WriteLine ("This is ");
For (int I = 0; I <= top; I ++)
{
Console. Write (data [I] + "");
}
}
}
Use the stack instance to determine whether the brackets match.
Public static void Match (char [] charlist)
{
If (charlist. Length = 0)
{
Return;
}
SeqStact <char> myStact = new SeqStact <char> (charlist. Length );
For (int I = 0; I <charlist. Length; I ++)
{
If (myStact. IsEmpty ())
{
MyStact. Push (charlist [I]);
}
Else if (myStact. getTop () = '(' & charlist [I] = ') | (myStact. getTop () = '[' & charlist [I] = ']')
{
MyStact. Pop ();
}
Else
{
MyStact. Push (charlist [I]);
}
}
If (myStact. IsEmpty ())
{
Console. WriteLine ("OK ");
}
Else
{
Console. WriteLine ("Error ");
}
}