Assignment: Queue

Assigned
  • February 13, 2019
Due
  • February 20, 2019 by 10:30pm
Submitting
Upload queue.c to gradescope to submit your assignment. Don’t forget that code style will count for 20% of your grade on this assignment.

Overview

For this week’s assignment you will implement a basic queue datastructure. Your queue will hold integers, and is accessed through a simple command line interface provided with the starter code. You are free to implement your queue however you like, as long as you do not write code with memory errors, and correctly free any allocated memory during shutdown.

To begin, download queue.tar.gz. Review the comments next to each queue_* function, which describes the queue API in detail. You should implement your queue in queue.c, but do not modify the main function. You are welcome to add structs or helper functions to this file if you like.

Command Line Interface

The command line interface for this queue understands two commands: put N where N is some integer, and take. The command put 3 will add a 3 to the queue. The command take removes a value from the queue, or prints "The queue is empty." if the queue does not contain any values.

The following examples show simple interactions with the queue interface. You can run these yourself, but you will need to press Control+D when you are finished providing input and would like to exit. This command sends the EOF character to stdin, which will cause the program to exit.

$ ./queue
put 1
put 2
put 3
put 4
take
1
take
2
take
3
take
4
take
The queue is empty.
put 5
put 6
take
5
take
6
take
The queue is empty.
$ ./queue
put 10
put 15
take
10
put 2
put -5
take
15
put 2
take
2
take
-5
take
2
$ ./queue
put 9
put 8
put 7
take
9
put 6
take
8
put 5
put 4
put 3
take
7

Questions & Answers

What should happen if the input ends before the queue is empty?
Nothing special. Just make sure you clean up any allocated memory in queue_destroy.
Can our queues hold duplicate values?
Yes, they should work for duplicate values.