Stack is
Back-in-first-out In some Program It is often used in design. A stack can only perform operations such as top element, pressure stack, outbound stack, number of elements in the stack, and whether the stack is empty.
The stack can be implemented using sequence tables and linked lists. With the previous sequence tables and linked lists, the stack implementation is very simple. Here we use single-chain tables.
1. ImplementationCode
/*
* File: Stack. CS
* Author: Zhenxing Zhou
* Date: 2008-12-07
* Blog: Http://www.xianfen.net/
*/
Namespace Xianfen. net. Datastructure
{
Public Class Stack < T >
{
Protected Singlelinkedlist < T > M_list;
Public IntCount
{
Get{ReturnM_list.count ;}
}
Public bool isempty
{< br> Get { return m_list.isempty ;}
}
Public stack ()
{< br> m_list = New singlelinkedlist T > ();
}
Public stack (T)
{< br> m_list = New singlelinkedlist T > (t );
}
PublicT POP ()
{
T=M_list.gettail ();
M_list.removetail ();
ReturnT;
}
PublicT top ()
{
ReturnM_list.gettail ();
}
Public VoidPush (T)
{
M_list.addtail (t );
}
}
}
2. Application Example
is a boring string flip program. char [] ch = " Hello world! " . tochararray ();
stack char > S = New stack char > ();
Foreach(CharCInCh)
{
S. Push (C );
}
IntIdx= 0;
While(!S. isempty)
{
Ch [idx++]=S. Pop ();
}
Console. writeline (New String(CH ));