Queue and Stack in. NET,. NETQueueStack

Source: Internet
Author: User

Queue and Stack in. NET,. NETQueueStack

1. ArrayList class

The ArrayList class is mainly used to process elements in an array. In ArrayList, Add, Remove, RemoveAt, and Insert methods are used to operate stacks. The Add method is used to Add an object to the end of ArrayList. The Remove method is used to Remove the first matching item of a specific object from ArrayList. The RemoveAt method is used to Remove the element at the specified index of ArrayList; the Insert method is used to Insert an element into the specified index of ArrayList.

The example shows how to create an ArrayList, how to add and remove items, and how to traverse the ArrayList. The program code is as follows:

1 using System. collections; // introduce namespace 2 namespace _ 1 3 {4 class ArrayListTest 5 {6 static void Main (string [] args) 7 {8 ArrayList arrlist = new ArrayList (); // instantiate an ArrayList object 9 // use the Add method to Add an element to the ArrayList and Add the element to the end of the ArrayList object 10 arrlist. add ("apple"); 11 arrlist. add ("banana"); 12 arrlist. add ("grape"); 13 foreach (int n in new int [3] {0, 1, 2}) 14 {15 arrlist. add (n); 16} 17 // remove the first element 18 arrlist with a value. remove (0); 19 // Remove the element of the current index, that is, the first element 20 arrlist. removeAt (3); 21 // Add an element 22 arrlist to the specified index. insert (1, "apple"); 23 // traverse the ArrayList and output all elements 24 for (int I = 0; I <arrlist. count; I ++) 25 {26 Console. writeLine (arrlist [I]. toString (); 27} 28} 29} 30}

 

2. Stack

The Stack class mainly implements a LIFO (Last In First Out, Last In First Out) mechanism. The element is inserted from the top of the stack (the inbound stack operation) and also removed from the top of the stack (the outbound stack operation ). Stack operations are performed using the Push, Pop, and Peek methods. The Push method is used to insert an object to the top of the Stack. The Pop method is used to remove and return the object at the top of the Stack. The Peek method is used to return the object at the top of the Stack but not remove it.

The example shows how to create a Stack, add items, and remove items to traverse the Stack. The program code is as follows:

1 using System. collections; // introduce namespace 2 namespace _ 2 3 {4 class StackTest 5 {6 static void Main (string [] args) 7 {8 // instantiate the Stack Class Object 9 Stack stack = new Stack (); 10 // import Stack, use the Pust method to add the element 11 for (int I = 1; I <6; I ++) 12 {13 Stack to the stack peer. push (I); 14 Console. writeLine ("{0} into stack", I); 15} 16 // return stack top element 17 Console. writeLine ("the top element of the current stack is: {0}", stack. peek (). toString (); 18 // 19 Console of the output stack. writeLine ("Remove stack top element: {0}", stack. pop (). ToString (); 20 // return the top element 21 Console of the stack. writeLine ("the top element of the current stack is: {0}", stack. peek (). toString (); 22 // traverse stack 23 Console. writeLine ("traverse stack"); 24 foreach (int I in stack) 25 {26 Console. writeLine (I); 27} 28 // clear stack 29 while (stack. count! = 0) 30 {31 int s = (int) stack. pop (); 32 Console. writeLine ("{0} outbound stack", s); 33} 34} 35} 36}

 

3. Queue class

The Queue class mainly implements a FIFO (First In First Out, First In First Out) mechanism. The element is inserted at the end of the queue (queuing operation) and removed from the queue header (queuing operation ). The Queue mainly uses the Enqueue, Dequeue, and Peek methods to operate the Queue. The Enqueue method is used to add an object to the end of the Queue. The Dequeue method removes the object and returns the object at the beginning of the Queue. The Peek method is used to return the object at the beginning of the Queue but not remove it.

The example shows how to create a Queue, how to add items, and how to remove items to traverse Queue. The program code is as follows:

1 using System. collections; // introduce namespace 2 namespace _ 3 3 {4 class QueueTest 5 {6 static void Main (string [] args) 7 {8 // instantiate the Queue Class Object 9 Queue queue = new Queue (); 10 // into the stack, use the Pust method to add the element 11 for (int I = 1; I <6; I ++) 12 {13 queue to the Stack peer. enqueue (I); 14 Console. writeLine ("{0} join", I); 15} 16 // return element 17 at the beginning of the team Console. writeLine ("the starting element of the current team is: {0}", queue. peek (). toString (); 18 // traverses the 19 Console of the team. writeLine ("Team traversal"); 20 fore Ach (int I in queue) 21 {22 Console. WriteLine (I); 23} 24 // clear stack 25 while (queue. Count! = 0) 26 {27 int q = (int) queue. dequeue (); 28 Console. writeLine ("{0} team-out", q); 29} 30} 31} 32}

 

4. Hashtable class

Hashtable is a set of key/value pairs. These key/value pairs are organized according to the hash code of the key. When a Key/Value pair is inserted in a Hashtable, it automatically maps the Key Value to the value and allows the Value associated with a specified Key to be obtained. In Hashtable, The Add and Remove methods are used to operate the hash table. The Add method is used to Add elements with specified keys and values to Hashtable. The Remove method is used to Remove elements with specified keys from Hashtable.

The example shows how to create a Hashtable, how to add and remove items, and how to traverse Hashtable. The program code is as follows:

1 using System. collections; // introduce namespace 2 3 namespace _ 4 4 {5 class HashtableTest 6 {7 static void Main (string [] args) 8 {9 // instantiate the Hashtable Class Object 10 Hashtable student = new Hashtable (); 11 // Add the element 12 student to Hashtable. add ("maid", "Tom"); 13 student. add ("S1002", "Jim"); 14 student. add ("S1003", "Lily"); 15 student. add ("S1004", "Lucy"); 16 // traverse Hashtable17 foreach (DictionaryEntry element in student) 18 {19 string id = element. key. toString (); 20 string name = element. value. toString (); 21 Console. writeLine ("student ID: {0} student name: {1}", id, name); 22} 23 // remove the 24 student element in Hashtable. remove ("S1003"); 25} 26} 27}

Note: Hashtable cannot contain duplicate keys. If you call the Add method to Add an existing key in the keys array, an exception is thrown. To avoid this problem, you can use the ContainsKey method to test whether a hash table contains a specific Key.

 

5. SortedList class

The SortedList class is also a set of key/value pairs, but unlike the hash table, these key/value pairs are sort by key and can be accessed by key and index. SortedList mainly uses the Add, Remove, and RemoveAt methods to operate SortedList. The Add method is used to Add elements with specified keys and values to SortedList. The Remove method is used to Remove elements with specified keys from SortedList. The RemoveAt method is used to Remove elements in the specified index of SortedList.

The example shows how to create a SortedList, how to add and remove items, and how to traverse SortedList. The program code is as follows:

1 using System. collections; // introduce namespace 2 3 namespace _ 5 4 {5 class SortedListTest 6 {7 static void Main (string [] args) 8 {9 // instantiate the object 10 SortedList student = new SortedList () of the SortedListTest class; 11 // Add the element 12 student to SortedList. add ("maid", "Tom"); 13 student. add ("S1003", "Jim"); 14 student. add ("S1002", "Lily"); 15 student. add ("S1004", "Lucy"); 16 // traverse SortedList17 foreach (DictionaryEntry element in student) 18 {19 string id = element. key. toString (); 20 string name = element. value. toString (); 21 Console. writeLine ("student ID: {0} Student name: {1}", id, name ); 22} 23 // remove the element 24 student whose key is "S1003" in SortedList. remove ("S1003"); 25 // Remove the element whose index is "" In SortedList, that is, the first element 26 student. removeAt (0); 27} 28} 29}

Note: SortedList cannot contain duplicate keys. When a foreach statement is used to traverse a SortedList object, the DictionaryEntry object is returned. This object will be returned in the sorted order based on the Key attribute.

Related Article

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.