A Review of Stacks and Queues I will first review the operations for stacks and queues. For the stack, these are PUSH, POP, TOP (to see the front of the stack ), SIZE, and EMPTY (boolean, is the stack empty or not ). For queues, it's - POP (usually DELETE, or DEQUEUE) - PUSH (usually INSERT, or ENQUEUE) - SIZE, FRONT, BACK - EMPTY, SIZE (same as for the stack) Stacks and queues are not directly implemented in STl but are implemented through adaptor classes. e.g. stack < vector > stack_one; stack < list > stack_two; stack < dequeue > stack_three; We can uses vectors and lists to implement stacks. Only lists to implement queues and dq's since we have to perform operations that effect both ends. Dequeues (or double ended queues) allow insertion and deletion from both ends. Note that there are many ways to implement the dq. One way presented in the text is to use two vectors. For example, consider the dq Dequeue1: <-> 0 5 9 37 11 <-> Using the two-vector implementation, the internal structure would look something like: Internal Structure of Dequeue1: HVector TVector Index 0 2 5 11 1 9 37 0 in this case HVector and TVector with the elements of the vector stored in HVector and TVector as given by Index. Queues and task scheduling. In LambdaMOO, a task is an execution of a MOO program. It's useful to think of information relevant to a task (e.g. how long it has been running, who initiated it, it's status, the next instruction for it perform etc.) as packaged in a structure called a task. In LambdaMOO, each player has a task queue, to manage the tasks that they are running at the moment. The main loop checks each of these queues to see if they have a task that is ready to run. For example, for each player a "read" task is run. This is equivalent to the scheme read-eval-print loop. The point of one of your exercises is to get a simple REPL going and implement this per-player task queue. So what's the point. Well suppose a player wants to build a "cat robot" every so often wakes up, bounces around the room, purrs, and then falls asleep. The we want some mechanism of suspending the task for a given amount of time and then actually checking it and running it when it's time for it to run. We can do this by deleting the first task from the queue, running it, and then putting it back on the queue: while (1) player:tell(bird, "squak"); suspend(10) endwhile In the above piece of LambdaMOO code, player is object, as is bird. The implementation of scheduling in LambdaMOO is a kind of round robin scheduling: each task runs for a fixed amount of time before suspending. Each task queue has a fixed size. Sets and multisets. Sets best thought of as collections we want to keep around so that we know that a piece of data is present or not. An example. Learning to filter email. You could maintain a collection of acceptable host names: cs.uchicago.edu cicero.uchicago.edu mit.edu etalon.com Assume that another file is an inbox. Let's suppose that you want to mark the hostnames that are not necessarily valid The following program takes as input a file of known valid hostnames, a file that contains hostnames stripped from some mailbox file, and then computes the set difference, giving you a list of hosts that # include # include # include # include void readAddresses(istream & input, set & hosts) { string line; while (getline(input, line)) hosts.insert(line);} void main(void){ ifstream validFile ("validhosts.dat"); if (! validFile){ cerr << "Hey! not able to open input file!\n"; return;} ifstream inboxFile ("inbox"); // create the sets set validHosts, mailHosts, badHosts; // read in the two files. readAddresses(validFile,validHosts); readAddresses(inboxFile,mailHosts); // set difference set_difference(mailHosts.begin(),mailHosts.end(), validHosts.begin(), validHosts.end(), inserter(badHosts,badHosts.begin())); //List the possibly suspicious entries set::iterator start = badHosts.begin(); set::iterator end = badHosts.end(); for( ; start != end; start++ ) cout << "Check host " << *start << "\n"; }