Mastering Algorithms With C - Kyle Loudon [53]
A: Using queue operations, we dequeue req 1 for processing, dequeue req 2 and re-enqueue it, dequeue req 3 for processing, dequeue req 4 and re-enqueue it, and dequeue req 5 for processing. Because we re-enqueued req 2 and req 4, the queue now contains only these requests in order. Removing requests out of sequence is more intuitive when we treat the queue as a linked list and apply linked list operations to it. In this case, we simply call list_next to traverse the requests one at a time and list_rem_next to remove the appropriate requests.
Related Topics
Polymorphism
A principle that allows an object (a variable) of one type to be used in place of another provided the two share some common characteristics. Polymorphism is an important part of object-oriented languages. However, even in languages that do not support it inherently, we can apply certain techniques to provide polymorphic behavior to some degree.
Double-ended queues
Often called deques (pronounced "decks") for short. A deque is a more flexible queue that allows insertions and deletions at both its head and tail.
Circular queues
Queues akin to circular lists. As with circular lists, circular queues do not have a tail. Instead, the last element in the queue is linked back to the first element so that the queue can be traversed in a circular fashion.
Chapter 7. Sets
Sets are collections of distinguishable objects, called members, grouped together because they are in some way related. Two important characteristics of sets are that their members are unordered and that no members occur more than once. Sets are an important part of discrete mathematics, an area of mathematics particularly relevant to computing. In computing, we use sets to group data, especially when we plan to correlate it with other data in the future. Some languages, such as Pascal, support sets intrinsically, but C does not. Therefore, this chapter presents a set abstract datatype.
This chapter covers:
Set principles
The fundamental mathematics describing sets. Like other mathematical objects, sets can be described in terms of some definitions, basic operations, and properties.
Sets
Abstract datatypes based on the mathematical concept of a set. Sets are unordered collections of related members in which no members occur more than once.
Some applications of sets are:
Data correlation
Determining interesting relationships between sets of data. For example, the intersection of two sets tells which members are present in both sets. The difference of two sets tells which members of the first set do not appear in the second set.
Set covering (illustrated in this chapter)
An optimization problem that nicely models many problems of combinatorics and resource selection. For example, imagine trying to form a team from a large set of candidate players, each with a certain set of skills. We might use the set-covering abstraction to form the smallest team possible possessing a certain set of skills overall. That is, for any skill required by the team as a whole, at least one player on the team should possess the skill.
Mathematics with sets
Specifically, combinatorics and probability. Sets have their own principles and rules that computers help apply. Computers are especially useful when working with large sets, which may contain many thousands of members. Operations with sets of this size, like operations in mathematics with large numbers, are very tedious to carry out by hand.
Graphs
Data structures typically used to model problems defined in terms of relationships or connections between objects (see Chapter 11). The most common way