1 Introduction

A collection is a container that holds data while it is being manipulated by a computer program. More specifically, it is a data structure that consists of zero or more items, where each item contains its own value or values. Examples of collections include stacks, queues, linked lists, and sorted lists. The total number of items in a collection is its count. Unlike an array, a collection is dynamically allocated in memory. That is, its capacity is automatically increased (through memory reallocation) as additional items are added to it. Like an array, a collection is considered an internal data structure because it resides in RAM and only remains there until the program that utilizes it terminates. Thus, the data in a collection is said to be nonpersistent. This is in contrast to a database table, which is considered an external data structure because it resides on a peripheral device (e.g., a magnetic disk) and remains there even after the program that utilizes it terminates. Thus, the data in a database table is said to be persistent.

In this chapter, we will begin by looking at the Stack class. The Stack class permits us to create and manipulate a one-dimensional last-in-first-out (LIFO) data structure analogous to a stack of books that we want to read in order of priority from top to bottom. Next, we will discuss the Queue class. The Queue class enables us to create and manipulate a one-dimensional first-in-first-out (FIFO) data structure analogous to a line of people waiting to be checked out at a grocery store. After that, we will consider the LinkedList class. The LinkedList class permits us to create and manipulate a one-dimensional linear data structure analogous to a group of alphabetized folders in a file cabinet. And finally, we will look at the SortedList class. The SortedList class enables us to create and manipulate a two-dimensional key-based data structure analogous to a dictionary of alphabetized terms and their respective definitions.

2 Stack Class

A stack is a one-dimensional last-in-first-out (LIFO) data structure that contains zero or more objects. The first object on a stack is at the bottom of the stack, whereas the last object on a stack is at the top of the stack. Operations on a stack can only occur at the top of the stack. Conceptually, a stack is like a stack of books that we want to read in order of priority from top to bottom. First, we place the book of least priority on a table. Then, we place the book of next least priority on top of that, and so on. Once we have a stack of books with the most important book on top, the process is complete. Now we can simply pull the books off the top of the stack one at a time and read them in order of importance.

In C#, objects are pushed onto a stack and popped off a stack. Retrieving an object that is not on top of the stack requires popping one or more objects off the stack. This is because direct access to the objects under the object on the top of the stack is not possible. Table 13-1 shows some of the properties, methods, and events of the Stack class.

Table 13-1 Some of the properties, methods, and events of the Stack class

Figure 13-1 shows an example of the Stack class.

Notice at 01 that we are declaring a stack of String types. Keep in mind, however, that we can declare stacks of any type. Notice as well that we are not specifying the size of the stack in the declaration. This is because a stack is dynamically allocated in memory. That is, its capacity is automatically increased (through memory reallocation) as additional items are added to it.

Figure 13-1
figure 1

Example of the Stack class

3 Queue Class

A queue is a one-dimensional first-in-first-out (FIFO) data structure that contains zero or more objects. The first object in a queue is at the beginning of the queue, whereas the last object in a queue is at the end of the queue. Operations on a queue can only occur at the beginning of the queue. Conceptually, a queue is like a line of people waiting to be checked out at a grocery store. The person at the beginning of the line is checked out first, the people in the middle of the line are checked out next, and the person at the end of the line is checked out last.

In C#, objects are enqueued onto a queue and dequeued from a queue. Retrieving an object that is not at the beginning of the queue requires dequeueing one or more objects from the queue since direct access to the objects after the object at the beginning of the queue is not possible. Table 13-2 shows some of the properties, methods, and events of the Queue class.

Table 13-2 Some of the properties, methods, and events of the Queue class

Figure 13-2 shows an example of the Queue class.

Notice at 01 that we are declaring a queue of String types. Keep in mind, however, that we can declare queues of any type. Notice as well that we are not specifying the size of the queue in the declaration. This is because a queue is dynamically allocated in memory. That is, its capacity is automatically increased (through memory reallocation) as additional items are added to it.

Figure 13-2
figure 2

Example of the Queue class

4 LinkedList Class

A linked list is a one-dimensional linear data structure that contains zero or more nodes. The first node in a linked list is at the start of the linked list, whereas the last node in a linked list is at the end of the linked list. Operations on a linked list can occur anywhere in the linked list. Conceptually, a linked list is like a group of alphabetized folders in a file cabinet. When we want to add a new folder to the group of folders, we scan the set of ordered folders, locate the point at which the folder should be added, and then add the folder. Conversely, when we want to remove an existing folder from the group of folders, we scan the set of ordered folders, locate the folder that we wish to remove, and then remove the folder.

In C#, nodes are added to a linked list, found in a linked list, and removed from a linked list. A node can be added to the start of a linked list, added immediately before a specified node in the linked list, added immediately after a specified node in the linked list, and added to the end of the linked list. In addition, a linked list can be searched in an effort to find a given node and/or retrieve its associated data. And finally, a node can be removed from the start of the linked list, removed from anywhere in the middle of the linked list, and removed from the end of the linked list. Table 13-3 shows some of the properties, methods, and events of the LinkedList class.

Table 13-3 Some of the properties, methods, and events of the LinkedList class

Figure 13-3 shows an example of the LinkedList class.

Notice at 01 that we are declaring a linked list of String types. Keep in mind, however, that we can declare linked lists of any type. Notice as well that we are not specifying the size of the linked list in the declaration. This is because a linked list is dynamically allocated in memory. That is, its capacity is automatically increased (through memory reallocation) as additional items are added to it.

Figure 13-3
figure 3figure 3figure 3

Example of the LinkedList class

5 SortedList Class

A sorted list is a two-dimensional key-based data structure that contains zero or more elements, each of which contains a key/value pair.Footnote 4 The first element in a sorted list is at the start of the sorted list, whereas the last element in a sorted list is at the end of the sorted list. Operations on a sorted list can occur anywhere in the sorted list. Conceptually, a sorted list is like a dictionary of alphabetized terms and their respective definitions. When we want to add a new term (i.e., key) and definition (i.e., value) to the dictionary, we scan the set of ordered terms, locate the point at which the term and definition should be added, and then add the term and definition. Conversely, when we want to remove an existing term and definition from the dictionary, we scan the set of ordered terms, locate the term and definition that we wish to remove, and then remove the term and definition.

In C#, elements are added to a sorted list, looked up in a sorted list, and removed from a sorted list. When an element is added to a sorted list, the sorted list is automatically adjusted so that its keys remain in the proper sort sequence. Duplicate keys are not permitted in a sorted list since a given key must uniquely identify an element in the sorted list. When an element is looked up in a sorted list, it is looked up by its key.Footnote 5 And when an element is removed from a sorted list, the sorted list is again automatically adjusted so that its keys remain in the proper sort sequence. Table 13-4 shows some of the properties, methods, and events of the SortedList class.

Table 13-4 Some of the properties, methods, and events of the SortedList class

Figure 13-4 shows an example of the SortedList class.

Notice at 01 that we are declaring the key of the key/value pair as a String type and the value of the key/value pair as a String type. Keep in mind, however, that we can declare keys and values of any type. Notice as well that we are not specifying the size of the sorted list in the declaration. This is because a sorted list is dynamically allocated in memory. That is, its capacity is automatically increased (through memory reallocation) as additional items are added to it.

Notice at 02 that although the elements are not added to the sorted list in alphabetical order, they are placed in alphabetical order automatically as they are added. Notice as well that United States Postal Service is misspelled.

Notice at 03 that United States Postal Service is no longer misspelled.

Figure 13-4
figure 4figure 4

Example of the SortedList class