--- files: [stack.c] url: https://cdn.cs50.net/2022/fall/labs/5/stack/README.md window: [terminal] --- # Jack's Stack ## Learning Goals * See a larger variety of data structures * Learn more about stacks ![Jack's Clothes](clothes.png) ## Background Recall Jack from lecture, in "Jack Learns the Facts About Queues and Stacks". Jack unwisely stores his clothing in a stack, and as a result, wears the same clothing day after day. Let's see how Jack's stack really works! There are various ways to represent a stack data structure: in C, we'll nearly always need some kind of `struct`! Open `stack.c` and notice how the `struct` we'll use is based on an array of `char *`s, or `strings`. This array holds each addition to the stack (in this case, Jack's clothing). The `struct`'s other members include `top`, which is an `int` representing the array index at which we should find the top of our stack. `capacity` represents the size of the array. When the stack is first created, `stack->top` is set to -1, meaning the array is empty. As elements are added, or "pushed", to the array, `stack->top` is incremented, and as elements are removed, or "popped" from the array, `stack->top`, is decremented. ![Stack Diagram](stack.png) In this practice problem, most of the code to create Jack's stack is complete. Your job will be to complete the `push` and `pop` functions, so Jack can add and retrieve, respectively, the clothes from his stack! ## Demo ## Implementation Details You must implement the functions `push` and `pop` to allow Jack to use his stack! ### `push` * Note that the function `push` has two parameters, a pointer to a stack and a `char *`, `item`, which is to be "pushed" to the stack. * Since the stack uses an array which has a fixed size, it is possible that the stack can be full. Do check (note the function `is_full`) and print a message such as "Sorry, stack is full", followed by a return, if the stack is full. * Once we confirm that there is space to `push` an item to the stack, increment `stack->top`, allocate memory for the corresponding array element, copy the contents of `item` to the array and print a confirmation message that the item was pushed to the stack. ### `pop` * To implement `pop`, you must first check if the stack is empty, and if so, return `NULL`. There is a completed function `is_empty` that might come in useful here. * If the stack contains data, you will return the `top` element of the stack and decrement `stack->top`. + Hints * If you use ```n--``` as an index to an array, it will first access the nth element and then decrement `n` by one, in that order. ## Thought Question * What other real world applications can you think of that would benefit from using a stack? * Is this the *best* way to create a stack? What other kind(s) of `struct` could you create to more dynamically scale the size of your stack up and down, even eliminating the need for `is_full`? ## How to Test Your Code Your program should behave per the examples below. ``` stack/ $ ./stack Blue Shirt pushed to stack Green Shirt pushed to stack Red Shirt pushed to stack White Shirt pushed to stack Enter an item of clothing to add to the stack: Khaki Pants Khaki Pants pushed to stack Enter an item of clothing to add to the stack: Khaki Pants popped from stack ``` No `check50` for this one! To evaluate that the style of your code, type in the following at the `$` prompt. ``` style50 stacks.c ``` ## How to Submit No need to submit! This is a practice problem.