Sample Video Frame

Created by Zed A. Shaw Updated 2024-02-17 04:54:36

Exercise 42: Stacks and Queues

At this point in the book, you should know most of the data structures that are used to build all of the other data structures. If you have some kind of List, DArray, Hashmap, and Tree, then you can build almost anything else out there. Everything else you run into either uses these or some variant of these. If it's not, then it's most likely an exotic data structure that you probably won't need.

Stacks and Queues are very simple data structures that are really variants of the List data structure. All they do is use a List with a discipline or convention, that says you always place elements on one end of the List. For a Stack, you always push and pop from one end. For a Queue, you always shift to the front, but pop from the end.

I can implement both data structures using nothing but the CPP and two header files. My header files are 21 lines long and do all of the Stack and Queue operations without any fancy defines.

To see if you've been paying attention, I'm going to show you the unit tests, and then have you implement the header files needed to make them work. To pass this exercise, you can't create any stack.c or queue.c implementation files. Use only the stack.h and queue.h files to make the tests runs.

Previous Lesson Next Lesson

Register for Learn C the Hard Way

Register today for the course and get the all currently available videos and lessons, plus all future modules for no extra charge.