Data Structure stack (C #)

Source: Internet
Author: User

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 ");
}
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.