Write a C # data structure yourself: use list<t> to implement a simple stack

Source: Internet
Author: User

Using list<t> to implement a simple stack in C #

What you need to do: stack, stack, view the top of the stack, view the number of elements, see if the socket is empty, determine if the element is in the socket, empty the socket, and copy it to a new list in the same order as the socket is advanced.

Here's the code:

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 5 6 //using list to implement a simple stack7  Public classMystack<t>8 {9     PrivateList<t>SL;Ten  One     //constructor to create a list A      PublicMystack () -     { -SL =NewList<t>(); the     } -  -     //clears all elements in the stack -      Public voidClear () +     { - SL. Clear (); +     } A  at     //perform a push stack operation -      Public voidPush (T Item) -     { - SL. ADD (item); -     } -  in     //perform a stack operation -      Public voidPop () to     { +         //determines if the stack is not allowed if the current stack is empty, throws an exception -         if(IsEmpty ()) the         { *             Throw NewIndexOutOfRangeException (); $         }Panax Notoginseng         Else -         { theSl. RemoveAt (Length ()-1); +         } A     } the  +     //gets the top element of the stack but does not eject -      PublicT Peek () $     { $         //determines if the stack top element cannot be fetched if the current stack is empty, throwing an exception -         if(IsEmpty ()) -         { the             Throw NewIndexOutOfRangeException (); -         }Wuyi         Else the         { -             returnSl[length ()-1]; Wu         } -     } About  $     //Copy the stack to a new list -      PublicList<t>ToList () -     { -list<t> NewList =NewList<t>() { }; A          for(inti = Length ()-1; I >=0; i--) +         { the Newlist.add (Sl[i]); -         } $         returnNewList; the     } the  the     //determine if the element is in the stack the      Public BOOLContains (T Item) -     { in         return(SL. Contains (item)); the     } the  About  the     //determines whether the current stack is empty the      Public BOOLIsEmpty () the     { +         return(Length () = =0); -     } the Bayi     //gets the current stack length the      Public intLength () the     { -         returnSL. Count (); -     } the  the}

Test the well-written socket:

1mystack<int> mystack =Newmystack<int>();2Mystack.push (Ten);3Mystack.push ( One);4Mystack.push ( A);5Mystack.push ( -);6 7Console.WriteLine ("top elements of the stack:"+Mystack.peek ());8Console.WriteLine ("number of stack elements:"+mystack.length ());9Console.WriteLine ("whether the stack is empty:"+mystack.isempty ());Ten  OneConsole.WriteLine ("====== Stack ======"); A  - Mystack.pop (); -  theConsole.WriteLine ("top elements of the stack:"+Mystack.peek ()); -Console.WriteLine ("number of stack elements:"+mystack.length ()); -Console.WriteLine ("whether the stack is empty:"+mystack.isempty ()); -  +Console.WriteLine ("====== to see if the element is ====== in the stack"); -  intA =Ten; +  intb = the; A Console.WriteLine (Mystack.contains (a)); at Console.WriteLine (Mystack.contains (b)); -  -  -Console.WriteLine ("====== copied to the new list ======"); -  -list<int> NewList =mystack.tolist (); in  -   for(inti =0; i < Newlist.count; i++) to  { + Console.WriteLine (Newlist[i]); -  } the  *Console.WriteLine ("====== empty Stack ======"); $ Panax Notoginseng mystack.clear (); -Console.WriteLine ("number of stack elements:"+mystack.length ()); theConsole.WriteLine ("whether the stack is empty:"+ Mystack.isempty ());

Results from the console output:

  4 stack is empty: False ====== stack ====== 3 stack is empty:  False====== See if the element is ======TrueFalse====== copied to the new list in theStack ====== ===== emptying stack ======0 stack is empty: True

Write a C # data structure yourself: use list<t> to implement a simple stack

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.