Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- struct Node {
- char data;
- Node* next;
- };
- class Stack {
- public:
- Node* head;
- int size;
- void add(char value);
- void output();
- Stack();
- ~Stack();
- };
- int main() {
- Stack stack1;
- stack1.add('h');
- stack1.add('e');
- stack1.add('l');
- stack1.add('l');
- stack1.add('o');
- stack1.output();
- return 0;
- }
- Stack::Stack()
- :size(0) {}
- Stack::~Stack() {
- Node* cursor = head;
- Node* next;
- while (cursor != NULL)
- {
- next = cursor->next;
- delete cursor;
- cursor = NULL;
- cursor = next;
- }
- }
- void Stack::add(char value) {
- if (size == 0) {
- head = new Node;
- head->data = value;
- head->next = NULL;
- } else {
- Node* ptr = new Node;
- ptr->data = value;
- ptr->next = head;
- head = ptr;
- }
- ++size;
- }
- void Stack::output()
- {
- Node* cursor = head;
- while (cursor && cursor->next) {
- cout << cursor->data << endl;
- cursor = cursor->next;
- }
- if (cursor) { cout << cursor->data << endl; }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement