All pastes #48555 Raw Edit

Unnamed

public text v1 · immutable
#48555 ·published 2006-04-07 19:16 UTC
rendered paste body
/* Single stack example */
/* first steps in classes and data structures */

#include <iostream>
#include <cstdlib>


using namespace std;

struct node 
        {
          int data;
          node * next;
        };

class stack
   {
       public:
               stack() {};
               int pop(node **p);
               void push(node **p, int d);
   };
int stack::pop (node **p)
   {
      node * tmp = *p;
      int d;
      d = (*p)->data;
      *p = (*p)->next;
      free(tmp);
      return d;
   }

void stack::push(node **p, int d)
   {
      node * tmp;
      tmp = (node * ) malloc(sizeof(node));
      if (tmp)
         {
           tmp->data = d;
           tmp->next = *p;
           * p = tmp;
         }
   }
int main(int argc, char *argv[])
{
  node *p;
  stack s;
  s.push(&p, 10);     // for test
  cout << s.pop(&p) << endl;
  return EXIT_SUCCESS;
}