Advertisement
Guest User

Untitled

a guest
Jul 7th, 2015
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct Node {
  6.   char data;
  7.   Node* next;
  8. };
  9.  
  10. class Stack {
  11.   public:
  12.     Node* head;
  13.     void add(char value);
  14.     void output();
  15.     Stack();
  16.     ~Stack();
  17. };
  18.  
  19. int main() {
  20.   Stack stack1;
  21.   stack1.add('h');
  22.   stack1.add('e');
  23.   stack1.add('l');
  24.   stack1.add('l');
  25.   stack1.add('o');
  26.   stack1.output();
  27.  
  28.   return 0;
  29. }
  30.  
  31. Stack::Stack()
  32.   :head(NULL) {}
  33.  
  34. Stack::~Stack() {
  35.   Node* cursor = head;
  36.   while (cursor != NULL) {
  37.     Node* next = cursor->next;
  38.     delete cursor;
  39.     cursor = NULL;
  40.     cursor = next;
  41.   }
  42. }
  43.  
  44. void Stack::add(char value) {
  45.   Node* node = new Node;
  46.   node->data = value;
  47.   node->next = head;
  48.   head = node;
  49. }
  50.  
  51. void Stack::output() {
  52.   Node* cursor = head;
  53.   if (cursor) {
  54.     while (cursor->next) {
  55.       cout << cursor->data << endl;
  56.       cursor = cursor->next;
  57.     }
  58.     cout << cursor->data << endl;
  59.   }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement