C # brief introduction to stack set

Source: Internet
Author: User
  1. Using system;
  2. Using system. Collections. Generic;
  3. Using system. LINQ;
  4. Using system. text;
  5. Namespace set
  6. {
  7. Class Stack
  8. {
  9. Public static void main ()
  10. {
  11. // The stack and queue are very similar containers.
  12. // The difference is that the queue is FIFO, while the stack is lifo.
  13. // Stack and stack <>. Like a queue, stacks also provide generic and non-generic versions.
  14. // The stack method is basically the same as that of the queue
  15. // POP () reads the stack from the top of the stack and deletes the element push () to store data. The top of the stack PEEK () reads the data from the top of the stack but does not delete the data.
  16. // Write an example below to use the stack
  17. Stack <string> stack = new stack <string> ();
  18. Stack. Push ("");
  19. Stack. Push ("B ");
  20. Stack. Push ("C ");
  21. Foreach (string s in stack)
  22. {
  23. Console. writeline (s );
  24. }
  25. // The result is CBA because the stack is backward-in-first-out.
  26. // If we use the enumerator above, the elements will not be changed and cannot be deleted. Now we use Pop to read the output.
  27. Console. writeline ("Use Pop to read -------------------------");
  28. While (stack. Count! = 0)
  29. {
  30. Console. writeline (stack. Pop ());
  31. }
  32. Console. writeline ("stack size after pop: {0}", stack. Count );
  33. Console. Readline ();
  34. }
  35. }
  36. }

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.