EXAMPLES
PIC 10B, Summer 2002
- The very first pair of example, traditional "Hello World!" program in
C and in C++:
- Example that demonstrates use of pointers as parameters to functions
in order to implement call-by-reference (as opposed to the
default call-by-value):
- Example that demonstrates three approaches to computation of factorials:
iterative, recursive, and tail recursive:
- An example that demonstrates use of
void
pointers and of
function pointers (i.e. pointers to functions). This is a nice
implementation of insertion sort algoritm, capable of sorting an
array of arbitrary type of mutually comparable objects:
- A sample implementation of a very simple Linked List of characters
sorted in alphabetical order. The example includes an interactive
driver program.
- An interface and implementation of a general Linked List.
- An interface and implementation of a general Stack. Stack can
be implemented either as an array, or as a Linked List. We decided
to implement Stack as a Linked List, therefore class
Stack
is derived from class List
of
Example # 6 above.
- An interface and implementation of a general Queue. Queue can
be implemented either as an array, or as a Linked List. We decided
to implement Queue as a Linked List, therefore class
Queue
is derived from class List
of
Example #6 above.
- An interface and implementation of a nice hash function which
works particularly well with C-style strings as keys.
- An interface and implementation of a general Chained Hash Table.
- An interface and implementation of a general Binary Tree.
- An interface and implementation of 3 standard traversal methods for
Binary Trees: preorder, inorder, postorder.
- An interface and implementation of a Binary Search Tree. We
have chosen to balance the tree using AVL Rotations, thus
our version of Binary Search Tree is implemented as an AVL Tree.
Please keep in mind that these two files are WORK IN PROGRESS !,
they will be updated appropriately while we will be working on
Binary Search Trees.
- Illustration of various sorting algorithms. Interface and implementation
of each are provided in files
sort.h
and sort.cpp
respectively. Sorting algorithms are organized as static methods of
Sort
class. Each of the sorting algorithms works on objects
of type Sortable
. The latter is an abstract interface,
which declares methods necessary for any kind of sortable objects.
If we want to sort objects of a particular data type using the methods
from Sort
class, this data type must, implement the
Sortable
interface, in other words it should be publically
derived from the class Sortable
.
As a proof that all this really works, here is a very simple class
Data
derived from the Sortable
ADT. Its
objects hold integral values. The class provides implementation of
methods required by Sortable
. Finally, a driver program
allows us to compile all this into an executable, and test our ideas.
Last modified on
by fedandr@math.ucla.edu.