C # queue

Source: Internet
Author: User
Address: http://blog.csdn.net/loveandangle/article/details/6733773
  1. /* Generic class: queue
  2. * Namespace: system. Collections. Generic
  3. * Description: the object's first-in-first-out set.
  4. * Type parameter: t -- specifies the type of the element in the queue.
  5. * Note:
  6. * Items in the queue container can only be deleted from the beginning, but cannot be deleted without rules. For example, you cannot directly Delete 2nd or the last item,
  7. * To delete 2nd items, you can only Delete the first item before deleting the first item. (only the first item can be deleted !!!)
  8. * Queue is very swimming in receiving ordered storage messages for sequential processing. objects stored in the queue are inserted at one end and removed from the other end.
  9. * The capacity of the queue is the number of elements that can be stored by the queue. The default initial capacity is 8.
  10. * When an element is added to the queue, the internal array is reassigned and the capacity is automatically increased as needed. You can use trimexcess to reduce the capacity.
  11. * Queue accepts null references as the valid values of the reference type and allows repeated elements.
  12. */
  13. // Example:
  14. // Example 1
  15. // Create a string queue with the default capacity and use the enqueue method to queue the string.
  16. // Enumerate queue elements, which does not change the queue status.
  17. // Use the dequeue method to column the first string, use the peek method to find the next item in the queue, and then use the dequeue method to column it.
  18. // Use the toarray method to create an array object and copy the queue elements to the array.
  19. // Create an array that is twice the queue size and use the copyto method to copy array elements from the array.
  20. // Use the contains method to display the string "four" in the first queue copy, then use the clear method to clear the copy, and the count attribute shows that the queue is empty.
  21. Using system;
  22. Using system. Collections. Generic;
  23. Class Program
  24. {
  25. Static void main (string [] ARGs)
  26. {
  27. // (1) create a queue container object and queue it to the container
  28. Queue <string> numbers = new queue <string> ();
  29. Numbers. enqueue ("one ");
  30. Numbers. enqueue ("two ");
  31. Numbers. enqueue ("three ");
  32. Numbers. enqueue ("four ");
  33. Numbers. enqueue ("five ");
  34. // (2) enumerate queue Elements
  35. Foreach (string s in numbers)
  36. {
  37. Console. writeline (s );
  38. }
  39. // (3) use the dequeue method/return the queue header object and remove it. Use the peek method to return the queue header object but do not remove it. Then use the dequeue method to make it out of the column.
  40. Console. writeline ("\ ndequeuing '{0}'", numbers. dequeue (); // return the queue header object and remove it
  41. Console. writeline ("Peek at next item to dequene: {0}", numbers. Peek (); // returns the queue header object but does not remove it
  42. Console. writeline ("dequeuing '{0}'", numbers. dequeue ());
  43. // (4) use the toarray method to create an array object and copy the queue elements to the array.
  44. String [] strarray = numbers. toarray ();
  45. Console. writeline ("\ n Array object :");
  46. Foreach (string s in strarray)
  47. {
  48. Console. writeline (s );
  49. }
  50. // (5) create a queue container from an array object
  51. Queue <string> queuecopy = new queue <string> (strarray );
  52. Console. writeline ("\ n queue container created by array object \" strarray :");
  53. Foreach (string s in queuecopy)
  54. {
  55. Console. writeline (s );
  56. }
  57. // (5) Create an array that is twice the queue size and use the copyto method to copy array elements from the array.
  58. String [] array2 = new string [numbers. Count * 2];
  59. Numbers. copyto (array2, 0); // copy all contents to the array object starting from the index 0 of the queue container
  60. Queue <string> queuecopy2 = new queue <string> (array2 );
  61. Console. writeline ("\ n queue container created by array object \" array2 :");
  62. Foreach (string s in queuecopy2)
  63. {
  64. Console. writeline (s );
  65. }
  66. // (6) use the contains method to display the string "four" in the first queue copy, then clear the copy using the clear method, and the queue is empty by the count attribute.
  67. Console. writeline ("\ nqueuecopy. contains (\ "four \") = {0} ", queuecopy. contains ("four"); // determines whether an element is in the queue
  68. Console. writeline ("number of elements before queuecopy is cleared:" + queuecopy. Count );
  69. Queuecopy. Clear ();
  70. Console. writeline ("number of elements after queuecopy is cleared:" + queuecopy. Count );
  71. Console. Read ();
  72. }
  73. }

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.