CITS2002 Systems Programming  
prev
next CITS2002 CITS2002 schedule  

A simple dynamic data structure - a stack

We'll commence with a simple stack - a data structure that maintains a simple list of items by adding new items, and removing existing items, from the head of the list.

Such a data structure is also termed a first-in-last-out data structure, a FILO, because the first item added to the stack is the last item removed from it (not the sort of sequence you want while queueing for a bank's ATM!).

Let's consider the appropriate type definition in C11:


typedef struct _s {
    int         value;
    struct _s   *next;
}  STACKITEM;

STACKITEM    *stack = NULL;

Of note:
  • we haven't really defined a stack datatype, but a single item that will "go into" the stack.
  • the datatype STACKITEM contains a pointer field, named next, that will point to another item in the stack.
  • we've defined a new type, a structure named _s, so that the pointer field next can be of a type that already exists.
  • we've defined a single pointer variable, named stack, that will point to a stack of items.

 


CITS2002 Systems Programming, Lecture 19, p2, 3rd October 2023.