Skip to content

Instantly share code, notes, and snippets.

@tabvn
Created May 28, 2019 15:23
Show Gist options
  • Select an option

  • Save tabvn/43f1673c5e2cd07eec0dfb289c1b3fd3 to your computer and use it in GitHub Desktop.

Select an option

Save tabvn/43f1673c5e2cd07eec0dfb289c1b3fd3 to your computer and use it in GitHub Desktop.
#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