Skip to content

Instantly share code, notes, and snippets.

@tabvn
Created May 24, 2019 14:50
Show Gist options
  • Save tabvn/f72cf8b38f162155c459153dd150a9da to your computer and use it in GitHub Desktop.
Save tabvn/f72cf8b38f162155c459153dd150a9da to your computer and use it in GitHub Desktop.
#include <iostream>
using namespace std;
struct Node{
int x;
Node *right;
Node *left;
Node(int a){
x = a;
this->left = NULL;
this->right = NULL;
}
};
// 1
struct Queue
{
Node *first;
Node *last;
Queue(){
this->first = NULL;
this->last = NULL;
}
void push(int x){
Node *node = new Node(x);
node->right = this->first;
if(this->first != NULL){
this->first->left = node;
}
this->first = node;
if(this->last == NULL){
this->last = node;
}
}
//123
void pop(){
if(this->last == NULL || this->first == NULL){
cout << "Danh sach rong" << endl;
return;
}
this->last = this->last->left;
delete[] this->last;
if(this->last != NULL){
this->last->right = NULL;
}
}
void output(){
if(this->first == NULL){
cout << "Queue is empty." << endl;
return;
}
cout << "Queue:" ;
Node *node = this->first;
while(node != NULL){
cout << node->x << ", ";
node = node->right;
}
}
};
int main()
{
Queue q;
//123 -> sieu thi
q.push(3);
q.push(2);
q.push(1);
q.output();
cout << "Bat dau xoa" << endl;
q.pop();
q.pop();
q.pop();
q.output();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment