Notes 7b:
Addendum B to Essentials of Data Structures, Cpt 7

Math 61 -- D. C. Smolarski, S.J.
Santa Clara University, Department of Mathematics and Computer Science

[Return to Math 61 Homepage | Return to Notes Listing Page]

Contents


Pointer Implementation of a Stack in C++

(DCS 7.3.3, pg 75-76) (Also see Notes M3 for Math 10.)

Declaration of Data Structure

    class stack
    { public:
        int       info;
	next  *   stack;
    }
To make best use of the capability of C++, one should include member functions as part of the class stack as in the array implementation. (This is the approach should in Math 10 notes M3.)

Push

     void push(int x, stack* & s)
     {   stack p;
         p = new stack;
	 p->info = x;
	 p->next = s;
	 s = p;
     }

Pop

     int pop(stack* & s)
     {   stack p;
         int temp;
	 if (s == NULL){
	    cout << "stack underflow error\n");
	    exit(1);}
	 else {
	    temp = s->info;
	    p = s;
	    s = s->next;
	    delete p; }
	 return temp;
      }

This page is maintained by Dennis C. Smolarski, S.J.
© Copyright 1998, 1999 Dennis C. Smolarski, SJ, All rights reserved.
Last changed: 7 April 1999.