Lecture 28: A stack data type
Overview
Burton Ma explains how a stack follows last-in, first-out access and implements it in C with dynamically allocated, singly linked nodes. The lecture builds a stack API around `push`, `pop`, size and emptiness checks, printing, and cleanup, emphasizing pointer updates and safe memory management.
Key takeaways
- A linked stack can implement `push` and `pop` in O(1) time by changing the top pointer rather than shifting elements.
- A stack's `top` pointer is `NULL` when empty, and storing a separate `size_t` count makes size and emptiness checks constant time.
- In C, every dynamically allocated node removed by `pop` must be freed, while its element value must be saved first if it still needs to be returned.
- Linked-list cleanup must save each node's `next` pointer before calling `free`, because freed memory cannot safely be dereferenced.
- An opaque stack type can hide its structure definition in the source file while exposing a `typedef` and function declarations through a header.
Chapters
- A stack provides access only to its top element, with `push` adding an item and `pop` removing and returning one.
- The implementation uses a singly linked sequence: each node stores an integer and a pointer to the next node.
- C pointers replace Java references; dynamically allocated nodes can persist across function calls, unlike local nodes with automatic storage duration.
- The stack structure tracks a pointer named `top` and a size; an empty stack has `top == NULL` and size zero.
- Each new node points to the previous top, so pushing requires only a fixed number of pointer operations.
- A singly linked stack supports traversal toward older elements but not backward movement; a stack does not need doubly linked nodes.
- `typedef` creates an alternate name for a type rather than a new type, allowing code to use `qlstack` instead of `struct qlstack_tag`.
- A header can expose the stack type and function declarations without revealing the structure's fields, an opaque-type design.
- The header uses a guard to prevent repeated inclusion and includes `<stdbool.h>` for Boolean results.
- The C source file defines a node with an integer element and a `next` pointer, then defines the stack with `top` and `size` fields.
- The implementation includes `<stdlib.h>` for allocation and deallocation and `<stdio.h>` for printing.
- Using `size_t` for the stack size avoids a negative count and lets the size function return a stored value without traversing nodes.
- The stack constructor allocates space for the stack structure with `malloc` and returns `NULL` if allocation fails.
- Because `malloc` does not initialize fields, initialization explicitly sets `top` to `NULL` and `size` to zero.
- The `size` and emptiness functions read the stored size directly, making both checks constant time.
- `push` allocates a node, stores the supplied integer, and sets the node's `next` pointer to the current top.
- The stack's `top` pointer is updated to the new node and its size is incremented.
- The operation takes O(1) time; returning the stack pointer also allows calls to be chained.
- `pop` saves the current top node and its integer, advances `top` to the next node, and decrements the size.
- The removed node is freed after its value is saved; the integer is written through a caller-provided `int` pointer when that pointer is not `NULL`.
- Unlike Java, C does not automatically reclaim the removed node, so the implementation must call `free`.
- Cleanup traverses from `top`, saves each node's `next` pointer before freeing the node, and then frees the stack structure itself.
- Freeing a node before retrieving its `next` pointer would make that memory invalid to access.
- The print function walks the same linked sequence, prints integers with separators, and can add optional prefix and suffix text.
- The GCC demo pushes `1`, `2`, and `3`, showing the top-first order `3, 2, 1`, then pops items until the stacks are empty.
- A second demo stack stores the corresponding negative values, and both stacks are freed after use.
- A generic stack cannot declare its node element as `int` when callers may need values such as `double`; choosing a general representation is the next lecture's topic.
Transcript excerpt
0:00 your memory on what a stack is. So stack's one of the fundamental data structures. Well, it's one of the fundamental simple data structures in computing science. Um, conceptually a stack models a stack of stuff, right? So here we got a stack of folders, we got a stack of books, we got a stack of pancakes. Um now the difference between real life and computing science uh is that in computing science with a stack traditionally you can only access the top of the stack right so in this stack of books I'm only able to access that top book right whereas in real life if you wanted to you could go into the middle of the stack and pull out the middle book right so a data structure stack you can only access the top element so everything happens at the top of the stack right if
0:49 I wanted to get to the bottom of this book stack of books, I'd have to remove all of the individual books and then get to the bottom book. Right? So, classically stacks only implement uh two operations, right? There's an operation called push. Push adds an element to the top of the stack. Pop removes the top element of the stack and typically it returns the element back to the caller. So, a stack is a linear data structure. It's a linear sequence of uh of stuff. And so you can implement these things in many different ways. So you could use a list if you already had a list. This is C, so we don't have a list. Um, today we're going to use an array to hold the elements of the stack. You should have done that in 124. Um, and then I Oh, sorry. Today we're not using an array.
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
In this course
Related notes
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Burton Ma.