struct Stack
struct Stack
int info[MAX];
int n;
};
void Initialize(Stack& S) {
S.n = 0;
}
bool isEmpty(Stack S) {
return S.n == 0;
}
bool isFull(Stack S) {
return S.n == MAX;
}
void push(Stack& S, int k) {
if (isFull(S)) return;
S.info[S.n] = k;
S.n++;
}
int pop(Stack& S) {
if (isEmpty(S)) return -1;
S.n--;
return S.info[S.n];
}
int top(Stack S) {
if (isEmpty(S)) return -1;
return S.info[S.n - 1];
}
void Show(Stack S) {
for (int i = 0; i < S.n; i++) {
cout << S.info[i] << " ";
}
}
struct Book {
int id;
char name[100];
long price;
};
struct Stack {
Book info[Max];
int n;
};
void Initialize(Stack& S) {
S.n = 0;
}
bool isEmpty(Stack S) {
return S.n = 0;
}
bool isFull(Stack S) {
return S.n == Max;
}
void push(Stack& S, Book b) {
if (isFull(S)) {
cout << "Stack da day, khong the push!" << endl;
return;
}
S.info[S.n] = b;
S.n++;
}
Book pop(Stack& S) {
if (isEmpty(S)) {
cout << "Stack rong, khong the pop!" << endl;
return Book{ -1, "", -1 };
}
S.n--;
return S.info[S.n];
}
Book top(Stack& S) {
if (isEmpty(S)) {
cout << "Stack rong!" << endl;
return Book{ -1, "", -1 };
}
return S.info[S.n - 1];
}
void Show(Stack S) {
if (isEmpty(S)) {
cout << "Stack rong!" << endl;
return;
}
for (int i = 0; i < S.n; i++) {
cout << "Book ID: " << S.info[i].id << ", Name: " << S.info[i].name << ", Gia: " << S.info[i].price << endl;
}
}
double averagePrice(Stack S) {
if (isEmpty(S)) return 0;
long totalPrice = 0;
for (int i = 0; i < S.n; i++) {
totalPrice += S.info[i].price;
}
return totalPrice / S.n;
}
struct List {
float info[Max];
int size; // so luong phan tu
};
void Init(List& L) {
L.size = 0;
}
bool isEmpty(List L) {
return L.size == 0;
}
bool isFull(List L) {
return L.size == Max;
}
void Add(List& L, int position, float value) {
if (isFull(L)) {
cout << "List da day, khong the them";
return;
}
if (position < 0 || position > L.size) {
cout << "vi tri khong hop le";
return;
}
for (int i = L.size; i > position; i--) {
L.info[i] = L.info[i - 1];
}
L.info[position] = value;
L.size++;
}
void Remove(List& L, int position) {
if (L.size == 0) {
cout << "List trong, khong the xoa" << endl;
return;
}
if (position < 0 || position > L.size) {
cout << "vi tri khong hop le";
return;
}
for (int i = position; i < L.size - 1; i++) {
L.info[i] = L.info[i + 1];
}
L.size--;
}
float get(List L, int position) {
if (position < 0 || position >= L.size) {
cout << "vi tri lay khong hop le!" << endl;
return -1;
}
return L.info[position];
}
void Input(List& L, int n) {
for (int i = 0; i < n; i++) {
float x;
cout << "Phan tu thu " << i + 1 << " = ";
cin >> x;
Add(L, i, x);
}
}
void Show(List L) {
if (isEmpty(L)) {
cout << "List rong!";
return;
}
for (int i = 0; i < L.size; i++) {
cout << L.info[i] << " ";
}
cout << endl;
}
struct phantu {
int value;
int priority;
};
struct Queue {
phantu info[Max];
int size;
};
void Init(Queue& Q) {
Q.size = 0;
bool isEmpty(Queue Q) {
return Q.size == 0;
bool isFull(Queue Q) {
if (isFull(Q)) {
return;
int i;
Q.info[i + 1] = Q.info[i];
else {
break;
Q.size++;
int deQueue(Queue& Q) {
if (isEmpty(Q)) {
return -1;
Q.size--;
return t;
int v, p;
cout << "Nhap gia tri thu " << i + 1 << " va gia tri uu tien cua no:";
enQueue(Q, v, p);
void Show(Queue Q) {
if (isEmpty(Q)) {
return;
cout << Q.info[i].value << " - " << Q.info[i].priority << endl;
**DSLK đơn**
struct Node {
int info;
Node* next;
};
void InitList(LinkedList& H) {
H = NULL;
int isEmpty(LinkedList H) {
return (H == NULL);
Q->info = k;
Q->next = H;
H = Q;
Pnode P = H;
while (P != NULL) {
else P = P->next;
return NULL;
Q->info = k;
if (isEmpty(H)) {
H = Q;
Q->next = NULL;
else {
Q->next = P->next;
P->next = Q;
if (H == NULL || Q == NULL) {
return;
insertAfter(H, Q, k);
if (P == H) {
insertBegin(H, k);
return;
Pnode pre = H;
while (pre->next != P) {
pre = pre->next;
if (Q == NULL || H == NULL) {
cout << "khong tim thay gia tri cua " << X;
return;
insertBefore(H, Q, k);
if (isEmpty(H)) {
insertBegin(H, k);
return;
Pnode P = H;
P = P->next;
Q->info = k;
Q->next = NULL;
P->next = Q;
int ListLength(LinkedList H) {
int count = 0;
Pnode P = H;
while (P != NULL) {
count++;
P = P->next;
return count;
if (isEmpty(H)) return 0;
Pnode P = H;
while (P != NULL) {
sum += P->info;
count++;
P = P->next;
void deleteFirstNode(LinkedList& H) {
else {
Pnode ptr = H;
H = H->next;
delete ptr;
return;
H = NULL;
delete noteToDelete;
if (H == noteToDelete) {
H = noteToDelete->next;
delete noteToDelete;
}
else {
Pnode R = H;
if (R->next == noteToDelete)
R->next = noteToDelete;
delete noteToDelete;
deleteNode(H, P);
while (P != NULL) {
deleteNode(H, P);
P = search(H, k);
void DeleteAllNodes(LinkedList& H) {
if (isEmpty(H)) return;
Pnode P = H;
Pnode Pnext;
while (P != NULL) {
Pnext = P->next;
delete P;
P = Pnext;
}
H = NULL;
void deleteEnd(LinkedList& H) {
if (isEmpty(H)) return;
if (H->next == NULL) {
delete H;
H = NULL;
return;
Pnode P = H;
pre = P;
P = P->next;
deleteNode(H, P);
pre->next = NULL;
void display(LinkedList H) {
Pnode P;
P = H;
while (P != NULL) {
P = P->next;
int info;
Node* next;
};
void createStack(Stack& H) {
H = NULL;
int isEmpty(Stack H) {
return (H == NULL);
Q->info = k;
Q->next = H;
H = Q;
Pnode P = H;
while (P != NULL) {
else P = P->next;
return NULL;
void Pop(Stack& H) {
Pnode ptr = H;
H = H->next;
delete ptr;
int top(Stack H) {
return (H->info);
int StackLength(Stack H) {
int count = 0;
Pnode P = H;
while (P != NULL) {
count++;
P = P->next;
return count;
void display(Stack H) {
Pnode P;
P = H;
while (P != NULL) {
P = P->next;
struct Node {
int data;
Node* next;
};
struct Queue {
Node* F;
Node* R;
};
void InitQueue(Queue& Q) {
int isEmpty(Queue Q) {
newNode->data = x;
if (isEmpty(Q)) {
Q.F = newNode;
Q.R = newNode;
return;
Q.R->next = newNode;
Q.R = newNode;
int deQueue(Queue& Q) {
else {
Q.F = Q.F->next;
delete ptr;
return value;
int peek(Queue Q) {
return (Q.F->data);
int getSize(Queue Q) {
if (isEmpty(Q)) return 0;
count++;
ptr = ptr->next;
return count;
void display(Queue Q) {
ptr = ptr->next;
struct Node {
};
Node* front; // Con trỏ trỏ tới phần tử có độ ưu tiên cao nhất (phần tử đầu danh sách)
};
void InitQueue(PriorityQueue& Q) {
Q.front = nullptr;
bool isEmpty(PriorityQueue Q) {
// Thêm một phần tử vào hàng đợi với giá trị và mức độ ưu tiên
newNode->value = value;
newNode->priority = priority;
newNode->next = nullptr;
// Nếu hàng đợi rỗng hoặc phần tử mới có độ ưu tiên cao hơn phần tử đầu
newNode->next = Q.front;
else {
temp = temp->next;
newNode->next = temp->next;
temp->next = newNode;
}
}
// Lấy và xóa phần tử có mức độ ưu tiên cao nhất ra khỏi hàng đợi
int deQueue(PriorityQueue& Q) {
if (isEmpty(Q)) {
cout << "Hàng đợi rỗng, không thể lấy phần tử!" << endl;
return -1;
Node* temp = Q.front; // Lấy phần tử đầu tiên (có mức độ ưu tiên cao nhất)
Q.front = Q.front->next; // Di chuyển con trỏ `front` đến phần tử tiếp theo
return value;
int peek(PriorityQueue Q) {
if (isEmpty(Q)) {
return -1;
return Q.front->value;
void display(PriorityQueue Q) {
if (isEmpty(Q)) {
return;
cout << temp->value << "(" << temp->priority << ") -> ";
temp = temp->next;
}
**DSLK đôi**
struct Dnode {
int data;
};
struct DoubleLinkedList {
Dnode* H;
Dnode* T;
};
newNode->data = _data;
newNode->nextL = NULL;
newNode->nextR = NULL;
return newNode;
if (isEmpty(DL)) {
DL.H = newNode;
DL.T = newNode;
return;
newNode->nextR = DL.H;
DL.H->nextL = newNode;
DL.H = newNode;
if (isEmpty(DL)) {
DL.H = newNode;
DL.T = newNode;
return;
if (P == NULL) {
return;
newNode->nextR = P;
P->nextL->nextR = newNode;
newNode->nextL = P->nextL;
P->nextL = newNode;
if (isEmpty(DL)) {
return;
if (DL.H == DL.T) {
else {
DL.H = DL.H->nextR;
DL.H->nextL = NULL;
delete ptr;
if (isEmpty(DL)) {
return;
if (P == NULL) {
return;
if (DL.H == DL.T) {
delete P;
InitDoubleLinkedList(DL);
else {
P->nextL->nextR = P->nextR;
P->nextR->nextL = P->nextL;
delete P;
if (isEmpty(DL)) {
return;
}
if (DL.H == DL.T) {
delete ptr;
else {
DL.T = DL.T->nextL;
DL.T->nextR = NULL;
delete ptr;
ptr = ptr->nextR;
return NULL;
else {
ptr = ptr->nextR;
}
}
struct Node {
int data;
Node* next;
};
ptr->data = _data;
ptr->next = NULL;
return ptr;
if (last == NULL) {
last = ptr;
last->next = ptr;
else {
ptr->next = last->next;
last->next = ptr;
if (last == NULL) {
return;
if (last == last->next) {
delete last;
last = NULL;
}
else {
last->next = ptr->next;
delete ptr;
ptr = ptr->next;
**Đệ Quy**
if (B == 0) return A;
int TinhTong(int A) {
if (A == 0) return 0;
str[start] = str[end];
str[end] = temp;
}
int TinhToHop(int n, int k) {
if (n == k || k == 0) return 1;
if (n <= 0) return;
CapSoCong(n - 1, d, m + d);
struct Node {
int data;
};
void InitBT(BinaryTree& S) {
S = NULL; }
newNode->data = _data;
newNode->LP = NULL;
newNode->RP = NULL;
return newNode; }
if (T == NULL) {
Node* P = createNode(x);
T = P;
else {
if (T->data > x) {
AddNode(T->LP, x);
AddNode(T->RP, x);
} } }
Node* Q;
if (*root == NULL) {
Q = new Node;
Q->data = x;
Q->LP = NULL;
Q->RP = NULL;
*root = Q;
insertBSTree2(&(*root)->LP, x);
insertBSTree2(&(*root)->RP, x);
else
return;
Node* Q;
if (root == NULL) {
Q = new Node;
Q->data = x;
Q->LP = NULL;
Q->RP = NULL;
root = Q;
return root;
void PreOderTraversal(BinaryTree T) {
if (T != NULL) {
PreOderTraversal(T->LP);
PreOderTraversal(T->RP);
void InOderTraversal(BinaryTree T) {
if (T == NULL) return;
InOderTraversal(T->LP);
InOderTraversal(T->RP);
void PostOrderTraversal(BinaryTree T) {
if (T == NULL) return;
PostOrderTraversal(T->LP);
PostOrderTraversal(T->RP);
cout << T->data << " ";
int Height(BinaryTree T) {
if (T == NULL) return 0;
int a = Height(T->LP);
int b = Height(T->RP);
if (a > b) {
return (a + 1);
return (b + 1);
int Sum(BinaryTree T) {
if (T == NULL) return 0;
sumLeft = Sum(T->LP);
sumRight = Sum(T->RP);
return sum;
if (i < n) {
root = createNode(a[i]);
return root;
return;
else {
DeleteNode(T->LP, _data);
DeleteNode(T->RP, _data);
else {
Node* X = T;
if (T->LP == NULL)
T = T->RP; }
T = T->LP; }
delete X;
}}}
void DelNode_Left(Node*& P) {
Node* Q, * R;
if (P->LP == NULL) {
Q = P;
P = P->RP;
Q = P;
P = P->LP;
else {
Q = P->LP;
if (Q->RP == NULL) {
P->data = Q->data;
P->LP = Q->LP;
else {
do {
R = Q;
Q = Q->RP;
P->data = Q->data;
R->RP = Q->LP;
}}
delete Q;
void DelNode_Right(Node*& P) {
Node* Q, * R;
if (P->RP == NULL) {
Q = P;
P = P->LP;
Q = P;
P = P->RP;
else {
Q = P->RP;
if (Q->LP == NULL) {
P->data = Q->data;
P->RP = Q->RP;
else {
do {
R = Q;
Q = Q->LP;
P->data = Q->data;
R->LP = Q->RP;
} }
delete Q;
hienthi(root->RP);
hienthi(root->LP);
int CountNodes(BinaryTree T) {
if (T == NULL) return 0;