5.1 Stack

A stack can be implemented as a linked list. In fact, we can use the interface in module 0043 for this purpose. Because we always insert items at the beginning of a list, and always remove items at the beginning of a list, the basic interface of a list is already sufficient. As a result, we can define a stack as follows:

struct _Stack
{
  struct List *pList;
};

It is tempting to define a new type Stack as follows:

typedef struct List *_Stack;

However, the proper method is to use its own struct definition. It is better for type checking, and also easier to be altered later on.



Subsections

Copyright © 2006-10-23 by Tak Auyeung