figure 2.4
output of listing 2.1.4 when viewed through a browser.
adding elements to a queue
in listing 2.1.4, we begin by creating an instance of the queue class, qtasks (line 5). in line 7 through 14, we add eight new elements to qtasks using the enqueue method. recall that a queue supports first in, first out ordering, so when we get ready to remove these elements, the first element to be removed will be "wake up", which was the first element added.
to quickly check if a particular element is an element of the queue, you can use the contains method. line 18 demonstrates usage of the contains method. note that it takes a single parameter, the element to search for, and returns true if the element is found in the queue and false otherwise.
removing elements from a queue
with a queue, you can only remove the element at the head. with such a constraint, it's no wonder that the queue class only has a single member to remove an element: dequeue. dequeue not only removes the element at the head of the queue, but it also returns the element just removed.
if you attempt to remove an element from an empty queue, the invalidoperationexception exception will be thrown and you will receive an error. therefore, to prevent producing a runtime error in your asp.net page, be sure to either place the dequeue statement in a try ... catch ... finally block or ensure that the count property is greater than zero (0) before using dequeue. (for more information on try ... catch ... finally blocks, refer to chapter 9, "asp.net error handling." for an example of checking the count property prior to using dequeue, see lines 28 through 32 in listing 2.1.4.) as with all the other collection types, you can remove all the queue elements with a single call to the clear method (line 36).
there might be times when you want to access the element at the head of the queue without removing it from the queue. this is possible via the peek method, which returns the element at the head of the queue without removing it. as with the dequeue method, if you try to peek an empty queue, an invalidoperationexception exception will be thrown.
iterating through the elements of a queue
one way to iterate through the elements of a queue is to simply use dequeue to successively grab each item off the head. this approach can be seen in lines 27 through 32 in listing 2.1.4. the major disadvantage of this approach is that, after iteration is complete, the queue is empty!
as with every other collection type, the queue can be iterated via a for each ... next loop or through the use of an enumerator. the following code snippet illustrates using the c# foreach statement to iterate through all the elements of a queue without affecting the structure:
queue qmyqueue = new queue(); // create a queue
qmyqueue.enqueue(5);
qmyqueue.enqueue(62); // add some elements to the queue
qmyqueue.enqueue(-7);
// iterate through each element of the queue, displaying it
foreach (int i in qmyqueue)
response.write("visiting queue element with value: " + i + "<br>");
working with the stack class
a stack is a data structure similar to a queue in that it supports only sequential access. however, a stack does bear one major difference from a queue: rather than storing elements with a first in, first out (fifo) semantic, a stack uses last in, first out (lifo). a crowded elevator behaves similar to a stack: the first person who enters the crowded elevator is the last person to leave, whereas the last person to board the elevator is the first out when it reaches its destination.
adding, removing, and accessing elements in a stack
the .net framework provides an implementation of the stack data type with the stack class. a stack has two basic operations: adding an element to the top of the stack, which is accomplished with the push method, and removing an element from the top of the stack, accomplished via the pop method. similar to the queue class, the stack class also contains a peek method to permit developers to access the top of the stack without removing the element.
up until this point, the code provided in the previous listings has just given you a feel for the syntax of the various collections. listing 2.1.5, however, contains a handy little piece of reusable code that can be placed on each page of your web site to provide a set of navigation history links for your visitors.
the code in listing 2.1.5 uses a session-level stack class instance that is used to store the links that a web visitor has traversed on your site since the start of his session. each time a user visits a web page, the stack is displayed in a history label and the page's url is pushed onto the stack. as the user visits various pages on your web site, his navigation history stack will continue to grow and he will be able to quickly jump back to previous pages on your site. basically, this is mimicking the functionality of a browser's back button. the output is shown in figure 2.5.
listing 2.1.5 a stack is ideal for keeping track of a user's navigation history
1: <script language="c#" runat="server">
2:
3: void page_load(object sender, eventargs e)
4: {
5: // see if we have a stack created or not:
6: if (session["history"] == null)
7: {
8: // the history stack has not been created, so create it now.
9: session["history"] = new stack();
10: } else {
11: // we already have a history stack. display the history:
12: ienumerator enumhistory =
13: ((stack) session["history"]).getenumerator();
14: while (enumhistory.movenext())
15: lblstackhistory.text += "<a href=/"" + enumhistory.current +
16: "/">" + enumhistory.current +
17: "</a><br>";
18: }
19:
20: // push current url onto stack if it is not already on the top
21: if (((stack) session["history"]).count > 0)
22: {
23: if(((stack) session["history"]).peek().tostring() !=
24: request.url.pathandquery.tostring())
25: ((stack) session["history"]).push(request.url.pathandquery);
26: } else
27: ((stack) session["history"]).push(request.url.pathandquery);
28: }
29:
30: </script>
31:
32: <html>
33: <body>
34: <b>session history</b><br>
35: <asp:label runat=server id="lblstackhistory" /><br>
36:
37: <a href="clearstackhistory.csharp.aspx">clear stack history</a><br>
38: <a href="back.csharp.aspx">back</a>
39:
40: <p>
41: <b>links:</b><br>
42: <li><a href="listing2.1.5.aspx">listing2.1.5.aspx</a><br>
43: <li><a href="listing2.1.5.b.aspx">listing2.1.5.b.aspx</a><br>
44: </body>
45: </html>
新闻热点
疑难解答
图片精选