Created
May 28, 2019 15:23
-
-
Save tabvn/43f1673c5e2cd07eec0dfb289c1b3fd3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| using namespace std; | |
| /* 1 | |
| 2 | |
| 3 | |
| 4 | |
| */ | |
| struct Node{ | |
| int x; | |
| Node *top; | |
| Node *bottom; | |
| Node(int a){ | |
| x = a; | |
| top = NULL; | |
| bottom = NULL; | |
| } | |
| }; | |
| struct Stack{ | |
| Node *first; | |
| Node *last; | |
| Stack(){ | |
| first = NULL; | |
| last = NULL; | |
| } | |
| void push(int a){ | |
| Node *node = new Node(a); | |
| if(this->first != NULL){ | |
| node->bottom = this->first; | |
| this->first->top = node; | |
| } | |
| first = node; | |
| if(last == NULL){ | |
| last = node; | |
| } | |
| } | |
| void output(){ | |
| cout << "Danh sach stak:"; | |
| Node *node = this->first; | |
| while(node != NULL){ | |
| cout << node->x << " "; | |
| node = node->bottom; | |
| } | |
| cout << endl; | |
| } | |
| void pop(){ | |
| if(this->first != NULL){ | |
| Node *node = this->first->bottom; | |
| delete[] this->first; | |
| this->first = node; | |
| if(this->first != NULL){ | |
| this->first->top = NULL; | |
| } | |
| } | |
| } | |
| }; | |
| int main() | |
| { | |
| Stack s; | |
| s.push(4); | |
| s.push(3); | |
| s.push(2); | |
| s.push(1); | |
| s.output(); | |
| cout << "Xoa phan tu" << endl; | |
| s.pop(); | |
| s.output(); | |
| s.pop(); | |
| s.output(); | |
| s.pop(); | |
| s.output(); | |
| s.pop(); | |
| s.output(); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment