Professional Documents
Culture Documents
Danh sách liên kết đơn
Danh sách liên kết đơn
}
p->next = last->next;
last->next = p;
}
}
void deletefirst(node *&head) //xóa head
{
if (head != NULL)
{
node* p = head;
head = head->next;
p->next = NULL;
delete (p);
}
}
void deletelast(node* head) // xóa tail
{
node* p = head;
node* r = head;
if (p->next == NULL)
{
delete (p);
}
if (head != NULL)
{
while (p->next != NULL)
{
r = p;
p = p->next;
}
r->next = NULL;
delete (p);
}
}
void deletenode(node *&head, int y)
{
node* p = head;
node* r = head;
if (head != NULL)
{
}
void deleteall(node*& head)
{
while (head != NULL)
{
deletefirst(head);
}
cout << " da xoa het ";
}
}
p = p->next;
}
}
return max;
}
void kttang(Node*& head)
{
int demtang=0;
int demgiam=0;
Node* p = head;
Node* q = head;
while (p -> next != NULL)
{
q = p;
p = p->next;
if (q->value < p->value)
{
demtang=demtang+1;
}
if (q->value > p->value)
{
demgiam = demgiam+ 1;
}
}
if (demtang > 0 && demgiam > 0)
{
cout << " ham vua taang vua giam ";
}
if (demtang > 0 && demgiam==0)
{
cout << " ham tang ";
}
if (demgiam > 0 && demtang==0)
{
cout << " ham giam ";
}
}
// ta có một danh sách đang tăng viết hàm chèn giá trị vào danh sách sao cho danh sách
vẫn tăng.
void add_incre(Node *&head, int k)
{
Node* p = createNode(k);
Node* tail = head;
Node* q = head;
if (head->next == NULL)
{
cout << " danh sach chi co 1 phan tu ";
}
else
{
while (tail->next != NULL)
{
q = tail;
tail = tail->next;
if (tail->value > k && q->value < k)
{
q->next = p;
p->next = tail;
break;
}
if (k < tail->value)
{
addfirst(head, k);
break;
}
}
if (tail->value < k)
{
tail->next = p;
}
}
}
}
if (tail->value == v2)
{
p->next=tail->next;
tail->next = p;
}
}
}
void add(Node*& head, int z, int count)
{
Node* p = createNode(z);
Node* tail = head;
Node* q = head;
int demds = 1;
if (count==1)
{
addfirst(head, z);
}
else
{
while (tail->next != NULL && count != demds)
{
q = tail;
tail = tail->next;
demds++;
}
if (count == demds)
{
p->next = tail;
q->next = p;
}
}
}
if (r == NULL)
{
root = r;
}
if (rkey > x)
{
r->left = t;
}
else
{
r->right = t;
}
delete p;
}
}
}
void countNode(Node* root, int &n1, int &n2, int &n3)
{
if (root!= NULL)
{
//CÁI NÀY TƯƠNG TỰ DUYỆT MẢNG, NÀY NHƯ LÀ DUYỆT LNR
//NHƯNG N Ở ĐÂY CÓ ĐK
//DUYỆT THEO TUẦN TỰ, DUYỆT HẾT NODE TRÁI RỒI QUAY
//NGƯỢC LÊN NẾU CÓ NHÁNH PHẢI THÌ DUYỆT HẾT NHÁNH PHẢI RỒI LẠI QUAY NGƯỢC LÊN
//TUẦN TỰ NHƯ THẾ CHO ĐẾN KHI DUYỆT HẾT NODE !!! <3
countNode(root->left, n1, n2, n3);
if (root->left == NULL && root->right == NULL)
{
n1++;
}
else if(root ->left==NULL || root->right ==NULL)
{
n2++;
}
else
{
n3++;
}
countNode(root->right, n1, n2, n3);
}
}
int calheight(Node* root)
{
if (root != NULL)
{
//ĐẦU TIÊN DUYỆT XUỐNG HẾT LEFT XONG DUYỆT LÊN, LÊN 1 NODE THÌ CỘNG 1
//GẶP NODE RIGHT THÌ DUYỆT HẾT RIGHT RỒI DUYỆT LÊN , LÊN 1 +1
int hleft = calheight(root->left) + 1;
int hright = calheight(root->right) + 1;
//NẾU HL>HR THÌ TRẢ VỀ HL NGƯỢC LẠI TRẢ VỀ HR <3
return hleft > hright ? hleft : hright;
}
return 0;
}
void showk(Node*& root,int v, int expected)
{
//KHÔNG KHAI BÁO v=0 CỤC BỘ VÌ ĐỆ QUY
//NÓ SẼ LẶP LẠI GIÁ TRỊ ĐÓ DẪN ĐẾN HÀM CHẠY SAI
if (root != NULL)
{
v++;
showk(root->left,v,expected);
if (v == expected)
{
cout << root->key << " ";
}
showk(root->right,v, expected);
}
}
void lnr(Node*& root)
{
if (root != NULL)
{
lnr(root->left);
cout << root->key << " ";
lnr(root->right);
}
}
#include<iostream>
using namespace std;
struct Node
{
int value;
Node* next;
Node* pre;
};
struct pointer
{
Node* head;
Node * tail;
};
void init(pointer &ls )
{
ls.head = NULL;
ls.tail = NULL;
}
Node* createNode( int x)
{
Node* p = new Node;
p->value = x;
p->next = NULL;
p->pre = NULL;
return p;
}
void addfirst(pointer& ls, int x)
{
Node* p = createNode(x);
if (ls.head == NULL)
{
ls.head = p;
ls.tail = p;
}
else
{
p->next = ls.head;
ls.head->pre = p;
ls.head = p;
}
}
// thắc mắc vì sao tail năm ở cuối đúng k.
//vì khi add last thì tail ko dịch chuyển nên nó luôn ở đuôi
void addlast(pointer& ls, int x)
{
Node* p = createNode(x);
if (ls.head==NULL)
{
ls.tail = ls.head = p;
}
else
{
ls.tail->next = p;
p->pre = ls.tail;
ls.tail = p;
}
}
void addaffter(pointer& ls, int& v ,int x )
{
Node *p = createNode(x);
Node* q=ls.head;
while (q != NULL && q->value != v)
{
q = q->next;
}
if (q->value == v)
{
p->next = q->next;
q->next = p;
p->pre = q;
q->next->pre = p;
}
}
void deletefirst(pointer& ls)
{
if (ls.head != NULL)
{
Node* p=ls.head;
if (p->next == NULL) //ds 1 phan tu
{
ls.tail = NULL;
ls.head = NULL;
}
else // 2 tro len
{
ls.head = ls.head->next;
p->next->pre = NULL;
p->next = NULL;
}
delete p;
}
}
void displaytoright(pointer &ls)
{
Node* p = ls.head;
while (p != NULL)
{
cout << p->value << " ";
p = p->next;
}
}
void displaytoleft( pointer ls)
{
Node* p = ls.tail;
while (p != NULL)
{
cout << p->value << " ";
p=p->pre;
}
}