All pastes #48613 Raw Edit

awukas_forever

public c v1 · immutable
#48613 ·published 2006-04-08 06:36 UTC
rendered paste body
/* Single stack example *//* first steps in classes and data structures */#include <iostream>#include <cstdlib>using namespace std;class stack   {     struct node        {          int data;          node * next;       };       node *p;       public:               stack() { p = NULL;};     /* class methods */               int pop();               void push(int d);               int delsearch(int d);               void print();   };int stack::pop ()   {      node * tmp = p;      int d;      d = p->data;      p = p->next;      free(tmp);      return d;   }void stack::push(int d)   {      node * tmp;      tmp = (node * ) malloc(sizeof(node));      if (tmp)         {           tmp->data = d;           tmp->next = p;           p = tmp;         }   }int stack::delsearch(int d)   {      node *tmp = p; int _tmp;      while (tmp && tmp->data != d) tmp = tmp->next;      if (tmp->data == d)        {          node *t = p;          _tmp = t->data;           t->data = d;          tmp->data = _tmp;          pop();        }   }void  stack::print()   {     while (p)       {         cout << p->data << endl;          p = p->next;       }   }/* main program */int main(int argc, char *argv[]){    stack s;  s.push(10); // just example   s.print();  return EXIT_SUCCESS;}