Data structure (C #): Stack

Source: Internet
Author: User
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 ));

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.