0% found this document useful (0 votes)
6 views

struct Stack

The document contains implementations of various data structures in C++, including stacks, queues (both regular and priority), linked lists, and their associated operations. Each data structure includes functions for initialization, checking if empty/full, adding/removing elements, and displaying contents. Additionally, it provides specific functionalities like calculating average prices for books in a stack and handling elements in a linked list.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

struct Stack

The document contains implementations of various data structures in C++, including stacks, queues (both regular and priority), linked lists, and their associated operations. Each data structure includes functions for initialization, checking if empty/full, adding/removing elements, and displaying contents. Additionally, it provides specific functionalities like calculating average prices for books in a stack and handling elements in a linked list.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 34

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;
}

**Hàng đợi vòng:**


struct Queue {
int info[Max];
int front;
int rear;
};
void Init(Queue& Q) {
Q.front = -1;
Q.rear = -1;
}
bool isEmpty(Queue Q) {
return Q.front == -1;
}
bool isFull(Queue Q) {
return Q.front == (Q.rear + 1) % Max;
}
void enQueue(Queue& Q, int x) {
if (isFull(Q)) {
cout << "Queue da day, khong the them" << endl;
return;
}
if (isEmpty(Q)) {
Q.front = 0;
Q.rear = 0;
}
else {
Q.rear = (Q.rear + 1) % Max;
}
Q.info[Q.rear] = x;
}
int deQueue(Queue& Q) {
if (isEmpty(Q)) {
cout << "Queue rong!" << endl;
return -1;
}
int value = Q.info[Q.front];
if (Q.rear == Q.front) {
Q.front = -1;
Q.rear = -1;
}
else {
Q.front = (Q.front + 1) % Max;
}
return value;
}
void Input(Queue& Q, int n) {
for (int i = 0; i < n; i++) {
int t;
cout << "Nhap gia tri thu " << i + 1 << " = ";
cin >> t;
enQueue(Q, t);
}
}
void Show(Queue Q) {
if (isEmpty(Q)) return;
int i = Q.front;
while (true) {
cout << Q.info[i] << " ";
if (i == Q.rear) break;
i = (i + 1) % Max;
}
cout << endl;
}

**Hàng đợi ưu tiên:**

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) {

return Q.size == Max;

void enQueue(Queue& Q, int value, int priority) {

if (isFull(Q)) {

cout << "Queue da day, khong the them!" << endl;

return;

int i;

for (i = Q.size - 1; i >= 0; i--) {

if (Q.info[i].priority > priority) {

Q.info[i + 1] = Q.info[i];

else {

break;

Q.info[i + 1].value = value;

Q.info[i + 1].priority = priority;

Q.size++;

int deQueue(Queue& Q) {

if (isEmpty(Q)) {
return -1;

int t = Q.info[Q.size - 1].value;

Q.size--;

return t;

void Input(Queue& Q, int n) {

for (int i = 0; i < n; i++) {

int v, p;

cout << "Nhap gia tri thu " << i + 1 << " va gia tri uu tien cua no:";

cin >> v; cout << "/"; cin >> p;

enQueue(Q, v, p);

void Show(Queue Q) {

if (isEmpty(Q)) {

return;

for (int i = 0; i < Q.size; i++) {

cout << Q.info[i].value << " - " << Q.info[i].priority << endl;

**DSLK đơn**

struct Node {

int info;

Node* next;

};

typedef Node* Pnode;

typedef Node* LinkedList;

//khoi tao ds rong

void InitList(LinkedList& H) {
H = NULL;

//kiem tra danh sach rong

int isEmpty(LinkedList H) {

return (H == NULL);

//bo sung phan tu k vao dau danh sach

void insertBegin(LinkedList& H, int k) {

Pnode Q = new Node;

Q->info = k;

Q->next = H;

H = Q;

//tim kiem phan tu co gia tri bang X

Pnode search(LinkedList& H, int value) {

Pnode P = H;

while (P != NULL) {

if (P->info == value) return P;

else P = P->next;

return NULL;

//bo sung phan tu k vao sau phan tu duoc tro boi p

void insertAfter(LinkedList& H, Pnode P, int k) {

Pnode Q = new Node;

Q->info = k;

if (isEmpty(H)) {

H = Q;

Q->next = NULL;

else {
Q->next = P->next;

P->next = Q;

//bo sung phan tu k vao sau phan tu co gia tri bang X

void insertAfterElement(LinkedList& H, int k, int X) {

Pnode Q = search(H, X);

if (H == NULL || Q == NULL) {

cout << "\n ds rong hoac khong thay " << X;

return;

insertAfter(H, Q, k);

//bo sung phan tu k vao truoc phan tu duoc tro boi P

void insertBefore(LinkedList& H, Pnode P, int k) {

if (P == H) {

insertBegin(H, k);

return;

Pnode pre = H;

while (pre->next != P) {

pre = pre->next;

insertAfter(H, pre, k);

//bo sung phan tu k vao truoc phan tu co gia tri bang X

void insertBeforeElement(LinkedList& H, int k, int X) {

Pnode Q = search(H, X);

if (Q == NULL || H == NULL) {

cout << "khong tim thay gia tri cua " << X;
return;

insertBefore(H, Q, k);

//bo sung phan tu vao cuoi danh sach

void insertEnd(LinkedList& H, int k) {

if (isEmpty(H)) {

insertBegin(H, k);

return;

Pnode P = H;

while (P->next != NULL) {

P = P->next;

Pnode Q = new Node;

Q->info = k;

Q->next = NULL;

P->next = Q;

//dem so phan tu trong danh sach

int ListLength(LinkedList H) {

int count = 0;

Pnode P = H;

while (P != NULL) {

count++;

P = P->next;

return count;

//Tinh gia tri trung binh cua danh sach


double average(LinkedList H) {

if (isEmpty(H)) return 0;

int sum = 0, count = 0;

Pnode P = H;

while (P != NULL) {

sum += P->info;

count++;

P = P->next;

return (double)sum / count;

//xoa phan tu dau tien

void deleteFirstNode(LinkedList& H) {

if (H == NULL) cout << "danh sach rong!";

else {

Pnode ptr = H;

H = H->next;

delete ptr;

void deleteNode(LinkedList& H, Pnode noteToDelete) {

if (H == NULL || noteToDelete == NULL) {

return;

if (H == noteToDelete && noteToDelete->next == NULL) {

H = NULL;

delete noteToDelete;

if (H == noteToDelete) {

H = noteToDelete->next;

delete noteToDelete;
}

else {

Pnode R = H;

while (R->next != noteToDelete) R = R->next;

if (R->next == noteToDelete)

R->next = noteToDelete;

delete noteToDelete;

void deleteNodeWithValue(LinkedList& H, int k) {

Pnode P = search(H, k);

deleteNode(H, P);

void deleteAllNodeWithvalue(LinkedList& H, int k) {

Pnode P = search(H, k);

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 pre = NULL;

Pnode P = H;

while (P->next != NULL) {

pre = P;

P = P->next;

deleteNode(H, P);

pre->next = NULL;

//hien thi du lieu cua danh sach ra man hinh

void display(LinkedList H) {

if (isEmpty(H)) cout << "NULL";

Pnode P;

P = H;

cout << "\n Hien thi danh sach\n ";

while (P != NULL) {

cout << P->info << " ";

P = P->next;

cout << "NULL";

**DSLK đơn Stack**


struct Node {

int info;

Node* next;

};

typedef Node* Pnode;

typedef Node* Stack;

void createStack(Stack& H) {

H = NULL;

//kiem tra danh sach rong

int isEmpty(Stack H) {

return (H == NULL);

//bo sung phan tu k vao dau danh sach

void Push(Stack& H, int k) {

Pnode Q = new Node;

Q->info = k;

Q->next = H;

H = Q;

//tim kiem phan tu co gia tri bang X

Pnode search(Stack& H, int value) {

Pnode P = H;

while (P != NULL) {

if (P->info == value) return P;

else P = P->next;

return NULL;

void Pop(Stack& H) {

if (H == NULL) cout << "danh sach rong!";


else {

Pnode ptr = H;

H = H->next;

delete ptr;

int top(Stack H) {

//if (isEmpty(H)) cout << "NULL";

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;

cout << "\n Hien thi danh sach\n ";

while (P != NULL) {

cout << P->info << " ";

P = P->next;

cout << "NULL";

**DSLK đơn Queue**

struct Node {
int data;

Node* next;

};

struct Queue {

Node* F;

Node* R;

};

void InitQueue(Queue& Q) {

Q.F = Q.R = NULL;

int isEmpty(Queue Q) {

return (Q.F == NULL);

void enQueue(Queue& Q, int x) {

Node* newNode = new Node;

newNode->data = x;

if (isEmpty(Q)) {

Q.F = newNode;

Q.R = newNode;

return;

Q.R->next = newNode;

Q.R = newNode;

int deQueue(Queue& Q) {

if (isEmpty(Q)) cout << "danh sach rong";

else {

Node* ptr = Q.F;

int value = ptr->data;

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;

int count = 1; //Queue co 1 phan tu

Node* ptr = Q.F;

while (ptr != Q.R) {

count++;

ptr = ptr->next;

return count;

void display(Queue Q) {

Node* ptr = Q.F;

while (ptr != Q.R->next) {

cout << ptr->data << "--";

ptr = ptr->next;

cout << "NULL";

**DSLK đơn Queue ưu tiên**

struct Node {

int value; // Giá trị của phần tử

int priority; // Độ ưu tiên của phần tử

Node* next; // Con trỏ trỏ tới phần tử tiếp theo

};

// Cấu trúc của hàng đợi ưu tiên


struct PriorityQueue {

Node* front; // Con trỏ trỏ tới phần tử có độ ưu tiên cao nhất (phần tử đầu danh sách)

};

// Khởi tạo hàng đợi rỗng

void InitQueue(PriorityQueue& Q) {

Q.front = nullptr;

// Kiểm tra hàng đợi có rỗng hay không

bool isEmpty(PriorityQueue Q) {

return Q.front == nullptr;

// Thêm một phần tử vào hàng đợi với giá trị và mức độ ưu tiên

void enQueue(PriorityQueue& Q, int value, int priority) {

Node* newNode = new Node; // Tạo node mới

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

if (isEmpty(Q) || priority < Q.front->priority) {

newNode->next = Q.front;

Q.front = newNode; // Đưa phần tử mới lên đầu hàng đợi

else {

Node* temp = Q.front;

// Tìm vị trí để chèn phần tử dựa vào mức độ ưu tiên

while (temp->next != nullptr && temp->next->priority <= priority) {

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)

int value = temp->value; // Lưu giá trị của phần tử

Q.front = Q.front->next; // Di chuyển con trỏ `front` đến phần tử tiếp theo

delete temp; // Giải phóng bộ nhớ của phần tử đã xoá

return value;

// Xem giá trị của phần tử có độ ưu tiên cao nhất

int peek(PriorityQueue Q) {

if (isEmpty(Q)) {

cout << "Hàng đợi rỗng!" << endl;

return -1;

return Q.front->value;

// Hiển thị tất cả các phần tử trong hàng đợi

void display(PriorityQueue Q) {

if (isEmpty(Q)) {

cout << "Hàng đợi rỗng!" << endl;

return;

Node* temp = Q.front;

while (temp != nullptr) {

cout << temp->value << "(" << temp->priority << ") -> ";

temp = temp->next;
}

cout << "NULL" << endl;

**DSLK đôi**

struct Dnode {

int data;

Dnode* nextL, * nextR;

};

struct DoubleLinkedList {

Dnode* H;

Dnode* T;

};

void InitDoubleLinkedList(DoubleLinkedList& DL) {

DL.H = DL.T = NULL;

bool isEmpty(DoubleLinkedList& DL) {

return (DL.H == NULL);

Dnode* createNode(int _data) {

Dnode* newNode = new Dnode;

newNode->data = _data;

newNode->nextL = NULL;

newNode->nextR = NULL;

return newNode;

//bo sung vao dau ds

void insertBegin(DoubleLinkedList& DL, int _data) {

Dnode* newNode = createNode(_data);

if (isEmpty(DL)) {

DL.H = newNode;

DL.T = newNode;
return;

newNode->nextR = DL.H;

DL.H->nextL = newNode;

DL.H = newNode;

//bo sung phan tu vao trc ptu dc tro boi P

void insertBefore(DoubleLinkedList& DL, Dnode* P, int _data) {

Dnode* newNode = createNode(_data);

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;

//xoa phan tu o dau danh sach

void deleteBegin(DoubleLinkedList& DL) {

if (isEmpty(DL)) {

cout << "DS rong khong the xoa!";

return;

Dnode* ptr = DL.H;

if (DL.H == DL.T) {

DL.H = DL.T = NULL;


delete ptr;

else {

DL.H = DL.H->nextR;

DL.H->nextL = NULL;

delete ptr;

//xoa phan tu tro boi P

void deleteNode(DoubleLinkedList& DL, Dnode* P) {

if (isEmpty(DL)) {

cout << "DS rong";

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;

void deleteEnd(DoubleLinkedList& DL) {

if (isEmpty(DL)) {

cout << "DS rong khong the xoa!";

return;
}

Dnode* ptr = DL.T;

if (DL.H == DL.T) {

DL.H = DL.T = NULL;

delete ptr;

else {

DL.T = DL.T->nextL;

DL.T->nextR = NULL;

delete ptr;

//Tim ptu co gia tri data

Dnode* searchNode(DoubleLinkedList& DL, int _data) {

Dnode* ptr = DL.H;

while (ptr != NULL) {

if (ptr->data == _data) return ptr;

ptr = ptr->nextR;

return NULL;

void display(DoubleLinkedList& DL) {

if (isEmpty(DL)) cout << "DS rong";

else {

cout << '\n' << "Danh sach: ";

Dnode* ptr = DL.H;

while (ptr != NULL) {

cout << ptr->data << " ";

ptr = ptr->nextR;

}
}

**DSLK đôi vòng**

struct Node {

int data;

Node* next;

};

Node* createNode(int _data) {

Node* ptr = new Node;

ptr->data = _data;

ptr->next = NULL;

return ptr;

void insertBegin(Node*& last, int _data) {

Node* ptr = createNode(_data);

if (last == NULL) {

last = ptr;

last->next = ptr;

else {

ptr->next = last->next;

last->next = ptr;

void deleteBegin(Node* last) {

if (last == NULL) {

cout << "DS rong";

return;

if (last == last->next) {

delete last;

last = NULL;
}

else {

Node* ptr = last->next;

last->next = ptr->next;

delete ptr;

void hienthi(Node* last) {

Node* ptr = last->next;

while (ptr != last) {

cout << ptr->data << " ";

ptr = ptr->next;

cout << last->data;

**Đệ Quy**

int UCLN(int A, int B) {

if (B == 0) return A;

return UCLN(B, A % B);

int TinhTong(int A) {

if (A == 0) return 0;

return A % 10 + TinhTong(A / 10);

void DaoNguoc(char str[], int start, int end) {

if (start >= end) return;

char temp = str[start];

str[start] = str[end];

str[end] = temp;

DaoNguoc(str, start + 1, end - 1);

}
int TinhToHop(int n, int k) {

if (n == k || k == 0) return 1;

return TinhToHop(n - 1, k - 1) + TinhToHop(n - 1, k);

void CapSoCong(int n, int d, int m) {

if (n <= 0) return;

cout << m << " ";

CapSoCong(n - 1, d, m + d);

**Cây NP Tìm kiếm**

struct Node {

int data;

Node* LP, * RP;

};

typedef Node* Pnode;

typedef Node* BinaryTree;

void InitBT(BinaryTree& S) {

S = NULL; }

Node* createNode(int _data) {

Node* newNode = new Node;

newNode->data = _data;

newNode->LP = NULL;

newNode->RP = NULL;

return newNode; }

void AddNode(BinaryTree& T, int x) {

if (T == NULL) {

Node* P = createNode(x);

T = P;

else {

if (T->data > x) {
AddNode(T->LP, x);

else if (T->data < x) {

AddNode(T->RP, x);

} } }

void insertBSTree2(Node** root, int x) {

Node* Q;

if (*root == NULL) {

Q = new Node;

Q->data = x;

Q->LP = NULL;

Q->RP = NULL;

*root = Q;

else if (x < (*root)->data)

insertBSTree2(&(*root)->LP, x);

else if (x > (*root)->data)

insertBSTree2(&(*root)->RP, x);

else

return;

Node* insertBSTree3(Node* root, int x) {

Node* Q;

if (root == NULL) {

Q = new Node;

Q->data = x;

Q->LP = NULL;

Q->RP = NULL;

root = Q;

else if (x < root->data)


root->LP = insertBSTree3(root->LP, x);

else if (x > root->data)

root->RP = insertBSTree3(root->RP, x);

return root;

Pnode SearchT(BinaryTree Root, int x) {

if (Root == NULL) return NULL;

if (x == Root->data) return Root;

else if (x < Root->data) return SearchT(Root->LP, x);

else return SearchT(Root->RP, x);

// duyet cay theo thu tu truoc

void PreOderTraversal(BinaryTree T) {

if (T != NULL) {

cout << T->data << " ";

PreOderTraversal(T->LP);

PreOderTraversal(T->RP);

// duyet cay theo thu tu giua

void InOderTraversal(BinaryTree T) {

if (T == NULL) return;

InOderTraversal(T->LP);

cout << T->data << " ";

InOderTraversal(T->RP);

// duyet cay theo thu tu sau

void PostOrderTraversal(BinaryTree T) {

if (T == NULL) return;

PostOrderTraversal(T->LP);

PostOrderTraversal(T->RP);
cout << T->data << " ";

// xac dinh chieu cao cua cay

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) {

int sum, sumLeft, sumRight;

sum = sumLeft = sumRight = 0;

if (T == NULL) return 0;

sumLeft = Sum(T->LP);

sumRight = Sum(T->RP);

sum = T->data + sumLeft + sumRight;

return sum;

Node* insertTree(int a[], int i, int n) {

Node* root = nullptr;

if (i < n) {

root = createNode(a[i]);

root->LP = insertTree(a, 2 * i + 1, n);

root->RP = insertTree(a, 2 * i + 2, n);

return root;

void DeleteNode(BinaryTree& T, int _data) {


if (T == NULL) {

return;

else {

if (_data < T->data) {

DeleteNode(T->LP, _data);

else if (_data > T->data) {

DeleteNode(T->RP, _data);

else {

Node* X = T;

if (T->LP == NULL)

T = T->RP; }

else if (T->RP == NULL)

T = T->LP; }

delete X;

}}}

void DelNode_Left(Node*& P) {

Node* Q, * R;

if (P->LP == NULL) {

Q = P;

P = P->RP;

else if (P->RP == NULL) {

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;

} while (Q->RP != NULL);

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;

else if (P->LP == NULL)

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;

} while (Q->LP != NULL);

P->data = Q->data;

R->LP = Q->RP;

} }

delete Q;

void hienthi(Node* root) {

if (root == NULL) return;

cout << root->data << "\t";

hienthi(root->RP);

hienthi(root->LP);

int CountNodes(BinaryTree T) {

if (T == NULL) return 0;

return 1 + CountNodes(T->LP) + CountNodes(T->RP); }

You might also like