SHOW:
|
|
- or go back to the newest paste.
1 | #include <iostream> | |
2 | ||
3 | using namespace std; | |
4 | ||
5 | struct Node { | |
6 | char data; | |
7 | Node* next; | |
8 | }; | |
9 | - | struct Node |
9 | + | |
10 | class Stack { | |
11 | - | char data; |
11 | + | public: |
12 | - | Node* next; |
12 | + | Node* head; |
13 | int size; | |
14 | void add(char value); | |
15 | - | class Stack |
15 | + | void output(); |
16 | Stack(); | |
17 | - | public: |
17 | + | ~Stack(); |
18 | - | Node* head; |
18 | + | |
19 | - | int size; |
19 | + | |
20 | - | void add(char value); |
20 | + | int main() { |
21 | - | char pop(); |
21 | + | Stack stack1; |
22 | - | void output(); |
22 | + | stack1.add('h'); |
23 | - | Stack(); |
23 | + | stack1.add('e'); |
24 | - | ~Stack(); |
24 | + | stack1.add('l'); |
25 | stack1.add('l'); | |
26 | stack1.add('o'); | |
27 | stack1.output(); | |
28 | - | int main() |
28 | + | |
29 | return 0; | |
30 | - | Stack stack1; |
30 | + | |
31 | - | stack1.add('h'); |
31 | + | |
32 | - | stack1.add('e'); |
32 | + | |
33 | - | stack1.add('l'); |
33 | + | :size(0) {} |
34 | - | stack1.add('l'); |
34 | + | |
35 | - | stack1.add('o'); |
35 | + | Stack::~Stack() { |
36 | - | stack1.output(); |
36 | + | Node* cursor = head; |
37 | - | |
37 | + | Node* next; |
38 | - | |
38 | + | |
39 | - | return 0; |
39 | + | while (cursor != NULL) |
40 | { | |
41 | next = cursor->next; | |
42 | delete cursor; | |
43 | cursor = NULL; | |
44 | cursor = next; | |
45 | - | :size(0){} |
45 | + | } |
46 | - | |
46 | + | |
47 | ||
48 | - | Stack::~Stack() |
48 | + | void Stack::add(char value) { |
49 | if (size == 0) { | |
50 | head = new Node; | |
51 | - | Node* cursor; |
51 | + | head->data = value; |
52 | - | Node* next; |
52 | + | head->next = NULL; |
53 | - | |
53 | + | } else { |
54 | - | /* |
54 | + | Node* ptr = new Node; |
55 | - | cout << endl << endl << "random shit" << endl << endl ; |
55 | + | ptr->data = value; |
56 | - | if (size > 0) |
56 | + | ptr->next = head; |
57 | - | { |
57 | + | head = ptr; |
58 | - | cursor = head->next; |
58 | + | } |
59 | - | } |
59 | + | ++size; |
60 | - | |
60 | + | |
61 | - | cout << endl << endl << cursor->data << endl << endl ; |
61 | + | |
62 | - | |
62 | + | |
63 | { | |
64 | - | */ |
64 | + | Node* cursor = head; |
65 | - | while (cursor != NULL) |
65 | + | while (cursor && cursor->next) { |
66 | - | { |
66 | + | |
67 | - | //toDelete = cursor; |
67 | + | cursor = cursor->next; |
68 | - | next = cursor->next; |
68 | + | } |
69 | - | delete cursor; |
69 | + | if (cursor) { cout << cursor->data << endl; } |
70 | - | cursor = NULL; |
70 | + |