03.01.2015 Views

C# 5.0 Programmer's Reference

Visual Studio 2013 C# 5.0 Programmer's Reference

Visual Studio 2013 C# 5.0 Programmer's Reference

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Stacks and Queues ❘ 331<br />

}<br />

}<br />

jobsListBox.Items.Add(Jobs.GetKey(i) + ": " + Jobs.GetByIndex(i));<br />

Enter a job name, select a priority, and click Add. The program adds the new job to the SortedList<br />

and calls the ListJobs method to list the jobs in their sorted order.<br />

The ListJobs method loops through the indices of the items in the list and displays their keys<br />

and values.<br />

A SortedList is more complicated (and hence slower) than a Hashtable or an array, so you should<br />

use it only if you need its special properties.<br />

CollectionsUtil<br />

Normally, Hashtables and SortedLists are case-sensitive. The CollectionsUtil class provides two<br />

shared methods, CreateCaseInsensitiveHashtable and CreateCaseInsensitiveSortedList,<br />

which create Hashtables and SortedLists that are case-insensitive.<br />

If you can use case-insensitive Hashtables and SortedLists, you may be better off using them<br />

because they will prevent the program from accidentally adding the same item twice with different<br />

capitalization.<br />

Stacks and Queues<br />

Stacks and queues are specialized data structures that are useful in many programming applications<br />

that need to add and remove items in a particular order. The .NET Framework Stack and Queue<br />

classes implement stacks and queues.<br />

The difference between a stack and a queue is the order in<br />

which they return the items stored in them. The following<br />

two sections describe stacks and queues and explain the<br />

ways in which they return items.<br />

Stack<br />

A stack returns items in last-in-first-out (LIFO, pronounced<br />

life-o) order. Because of its LIFO behavior, a<br />

stack is sometimes called a LIFO list or simply a LIFO.<br />

Adding an item to the stack is called pushing the item<br />

onto the stack and removing an item is called popping<br />

the item off the stack. These operations have the names<br />

push and pop because a stack is like a spring-loaded stack<br />

of plates in a cafeteria or buffet. You push new plates<br />

down onto the top of the stack and the plates sink into the<br />

counter. You pop the top plate off and the stack rises to<br />

give you the next plate. Figure 14-4 illustrates this kind<br />

of stack.<br />

Push<br />

Pop<br />

Figure 14-4: A Stack lets you remove<br />

items in last-in-first-out (LIFO) order.<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!