- Forward


Linked Stacks
With Examples in C++


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Motivation
Back SMYC Forward
  • So far, our implementations of stacks have been limited to a pre-specified number of elements
  • Dynamic memory allocation makes it possible for us to develop much more flexible implementations
  • We could use dynamic memory allocation with contiguous implementations or linked implementations
  • We'll discuss a linked implementation here
A Node in a Linked Stack of int
Back SMYC Forward
cppexamples/linkedstack/int/Node.h
 
cppexamples/linkedstack/int/Node.cpp
 
A Linked Stack of int
Back SMYC Forward
cppexamples/linkedstack/int/Stack.h
 
cppexamples/linkedstack/int/Stack.cpp
 
A Limitation of this Implementation
Back SMYC Forward
  • The current implementation can only contain int values
  • We would need to create a different Stack class for every different type of element
A Templated Node
Back SMYC Forward
cppexamples/linkedstack/templated/Node.h
 
cppexamples/linkedstack/templated/Node.cpp
 
A Templated Stack
Back SMYC Forward
cppexamples/linkedstack/templated/Stack.h
 
cppexamples/linkedstack/templated/Stack.cpp
 
There's Always More to Learn
Back -