You are on page 1of 13

#include<iostream>

using namespace std;


struct node
{
int value;
node* next;
};
void init(node *&head)
{
head = NULL;
}
node* createnode(int x)// truyền giá trị vào note
{
node* p = new node;
p->next = NULL;
p->value=x;
return p;
}
void output(node* head)//xuất
{
node* p = head;
while (p != NULL)
{
cout << p->value << "\t";
p = p->next;
}
}
void addfirst(node *&head, int x) // thay đổi vị trí đầu
{
node* p = createnode(x);
p->next = head;
head = p;
}
void addlast(node *&head, int x)// thay đổi vị trí cuối
{
node* p = createnode(x);
node* last=head ;
if (head == NULL)
{
head=p;
}
else
{
while (last->next != NULL)
{
last = last->next;
}
last ->next= p;
}
}
void addafter(int x, int v, node* head) // thêm sau phần tử chỉ định
{
node* p = createnode(x);
node* last = head;
if (last->next == NULL)
{
head = p;
}
else
{
while (last->next != NULL && last->value != v)
{
last=last -> next;

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

while (p->next != NULL && p->value != y)


{
r = p;
p = p->next;
}
if (p->value == y && head -> value != y) // tìm thấy x
{
r->next = p->next;
p->next = NULL;
delete(p);
}
if (head->value == y)
{
deletefirst(head);
}
}

}
void deleteall(node*& head)
{
while (head != NULL)
{
deletefirst(head);
}
cout << " da xoa het ";
}

int findx(int x, Node*& head)


{
Node* p = head;
while ((p != NULL))
{
if ((p->value == x))
{
return 1;
}
p = p->next;
}
return -1;
}
int findxmax( Node*& head)
{
int max = 0;
Node* p = head;
if (p->next == NULL)
{
max = p->value;
}
else
{
while (p != NULL)
{
if (p->value > max)
{
max = p->value;

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

}
}
}

void addbefore(Node*& head, int x, int &v1)


{
Node* p = createNode(x);
Node* tail = head;
Node* q = head;
if (head->value == v1)
{
addfirst(head, x);
}
else
{
while (tail->next != NULL && tail->value != v1)
{
q = tail;
tail = tail->next;
}
if (tail->value==v1)
{
p->next = tail;
q->next = p;
}
}
}
void addafter(Node* head, int y, int &v2)
{
Node* p = createNode(y);
Node* tail = head;
Node* q = head;
if (tail->next == NULL)
{
tail->next = p;
}
else
{
while (tail->next != NULL && tail->value != v2)
{
tail = tail->next;

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

void output(Node *&head )


{
Node* p = head;
while (p != NULL)
{
if( (int)p->info % 3 == 0)
cout << p->info << " ";
p = p->plink;
}
}
void input(Node*& head, int& x)
{
int i = 1, n;
cout << " nhap mang n phan tu " << endl;
cin >> n;
while (i<=n)
{
cin >> x;
addlast(head, x);
i++;
}
}
void deleteAll(Node*& head)
{
while (head != NULL)
{
Node* p = head;
head = head->plink;
p->plink = NULL;
delete p;
}
}
//3 hàm search tương tự nhau
void search(Node *&head)
{
Node* p = head;
int x;
cout << " nhap so muon tim " << endl;
cin >> x;
while (p->plink != NULL && p->info != x)
{
p = p->plink;
}
if (p->info == x)
{
cout << " tim thay " << x;
}
else
{
cout << " khong tim thaay" << x;
}
}
int findx( Node*& head)
{
int x;
cout << " nhap x muon tim "; cin >> x;
Node* p = head;
while ((p != NULL))
{
if ((p->info == x))
{
return 1;
}
p = p->plink;
}
return -1;
}
void outputsearch(Node* head)
{
if (findx(head) == 1)
{
cout << " tim thay ";
}
else
{
cout << " ko tim thay";
}
}========================================================================================
//CÂY NH? PHÂN
#include<iostream>
using namespace std;
struct Node
{
int key;
Node* left;
Node* right;
};
void Init(Node*& root)
{
root = NULL;
}
Node* createNode(int x)
{
Node* p = new Node;
p->key = x;
p->right = NULL;
p->left = NULL;
return p;
}
void addNode(Node*& root, int x)
{
Node* p = root;
Node* r = NULL;
if (root == NULL)
{
root = createNode(x);
}
else
{
while (p != NULL && p->key != x)
{
r = p;
if (p->key > x)
{
p = p->left;
}
else
{
p = p->right;
}
}
if (p == NULL)
{
p = createNode(x);
if (r->key > x)
{
r->left = p;
}
else
{
r->right = p;
}
}
}
}
Node* searchNode(Node* root, int x)
{
Node* p = root;
if (root != NULL)
{
while (p != NULL && p->key != x)
{
if (p->key > x)
{
p = p->left;
}
else
{
p = p->right;
}
}
}
return p;
}
void report(Node *root, int &x)
{
cout << " NHAP X MUN TIM " << endl; cin >> x;
if (searchNode(root, x) != 0)
{
cout << " TIM THAY " << x;
}
else
{
cout << " KHONG BIET ";
}
}
void deleteNode(Node*& root, int& x)
{
Node* p = root;
Node* r = NULL;
int rkey = 0;
if (root != NULL)
{
while (p != NULL && p->key != x)
{
r = p;
rkey = r->key;
if (p->key > x)
{
p = p->left;
}
else
{
p = p->right;
}
}
if (p != NULL)
{
//xóa node kép
if (p->left != NULL && p->right != NULL)
{
r = p;
Node* p1 = p->right;
while (p1->left != NULL)
{
r = p1;
rkey = r->key;
p1 = p1->left;
}
p->key = p1->key;
p = p1;
}

//xóa node đơn


Node* t = NULL;
if (p->left == NULL)
{
t = p->right;
}
else
{
t = p->left;
}

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

You might also like