Advertisement
Guest User

Stack.cpp

a guest
Apr 15th, 2013
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.63 KB | None | 0 0
  1. #include "Stack.h"
  2. #include "require.h"
  3. using namespace std;
  4.  
  5. void
  6. Stack::Link::initialize(void* dat, Link* nxt) {
  7.   data = dat;
  8.   next = nxt;
  9. }
  10.  
  11. void Stack::initialize() { head = 0; }
  12.  
  13. void Stack::push(void* dat) {
  14.   Link* newLink = new Link;
  15.   newLink->initialize(dat, head);
  16.   head = newLink;
  17. }
  18.  
  19. void* Stack::peek() {
  20.   require(head != 0, "Stack empty");
  21.   return head->data;
  22. }
  23.  
  24. void* Stack::pop() {
  25.   if(head == 0) return 0;
  26.   void* result = head->data;
  27.   Link* oldHead = head;
  28.   head = head->next;
  29.   delete oldHead;
  30.   return result;
  31. }
  32.  
  33. void Stack::cleanup() {
  34.   require(head == 0, "Stack not empty");
  35. } ///:~
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement