Professional Documents
Culture Documents
#include <iostream>
using namespace std;
typedef struct Node
{
int data;
Node* Left;
Node* Right;
};
typedef struct List
{
Node* Head;
Node* Tail;
};
void List_Init(List* list)
{
list->Head = NULL;
list->Tail = NULL;
}
int Isempty(List* list)
{
return (list->Head==NULL);
}
int List_Length(List* list)
{
Node* PH = list->Head;
Node* PT = list->Tail;
int i = 0;
if (PH != NULL)
i = 1;
while (PH != NULL)
{
if (PH == PT) break;
PH = PH->Right;
i++;
if (PH == PT) break;
PT = PT->Left;
i++;
}
return i;
}
Node* MakeNode(int data)
{
Node* newnode = new Node;
newnode->data = data;
newnode->Left = NULL;
newnode->Right = NULL;
return newnode;
}
void Insert_First(List* list, int data)
{
Node* newnode = MakeNode(data);
if (Isempty(list))
{
list->Head = newnode;
list->Tail = newnode;
}
else
{
newnode->Right = list->Head;
list->Head->Left = newnode;
list->Head = newnode;
}
}
void Insert_Last(List* list, int data)
{
Node* newnode = MakeNode(data);
if (Isempty(list))
{
list->Head = newnode;
list->Tail = newnode;
}
else
{
list->Tail->Right = newnode;
newnode->Left = list->Tail;
list->Tail = newnode;
}
}
void Insert_K (List* list, int data, int k)
{
Node *PH = list->Head, *PT, *R;
int i = 1, l = List_Length(list);
if (k<1 || k>l + 1)
cout << "Vi tri chen khong hop le " << endl;
else
{
R = MakeNode(data);
if (k == 1) Insert_First(list, data);
else
if (k == l + 1) Insert_Last(list, data);
else
{
while (PH != NULL && i != k - 1)
{
i++;
PH = PH->Right;
}
PT = PH->Right;
R->Right = PT;
R->Left = PH;
PH->Right = R;
PT->Left = R;
}
}
}
void Del_First(List* list)
{
if (!Isempty(list))
{
Node* obsoleteNode = list->Head;
list->Head = list->Head->Right;
delete obsoleteNode;
}
}
void Del_Last(List* list)
{
if (!(Isempty(list)))
{
Node* obsoleteNode = list->Tail;
list->Tail = list->Tail->Left;
list->Tail->Right = NULL;
delete obsoleteNode;
}
}
void Del_K(List* list, int k)
{
Node *PH = list->Head, *PT;
int i = 1, l = List_Length(list);
if (k == 1) Del_First(list);
else
if (k == l)
Del_Last(list);
else
{
while (PH != NULL && i != k - 1)
{
i++;
PH = PH->Right;
}
PT = PH->Right->Right;
PH->Right = PT;
PT->Left = PH;
}
}
int Search(List* list, int data)
{
Node* P = list->Head;
int i = 1;
while (P != NULL && P->data != data)
{
P = P->Right;
i++;
}
if (P != NULL) return i;
else
return 0;
}
void Del_X(List* list, int data)
{
int l = Search(list, data);
while (l)
{
Del_K(list, l);
l = Search(list, data);
}
}
void List_Display(List* list)
{
Node* P = list->Head;
while (P != list->Tail->Right)
{
cout << P->data<<" ";
P = P->Right;
}
cout << endl;
}
void main()
{
List list;
List_Init(&list);
int check;
cout << "CHON CHUC NANG" << endl;
cout << "*********************************" << endl;
cout << "1. Kiem tra danh sach rong" << endl;
cout << "2. Hien thi do dai danh sach" << endl;
cout << "3. Chen phan tu x vao vi tri k trong danh sach" << endl;
cout << "4. Tim mot phan tu trong danh sach" << endl;
cout << "5. Xoa phan tu tai vi tri x" << endl;
cout << "6. Xoa phan tu x trong danh sach" << endl;
cout << "7. Them mot phan tu vao dau danh sach" << endl;
cout << "8. Them mot phan tu vao cuoi danh sach" << endl;
cout << "9. Xoa man hinh" << endl;
cout << "10. Exit" << endl;
do
{
cout << "Bam chon" << endl;
cin >> check;
switch (check)
{
case 1:
{
if (Isempty(&list)) cout << "Danh sach rong" << endl;
else
cout << "Danh sach khong rong" << endl;
break;
}
case 2:
{
cout << "Do dai danh sach la: " << List_Length(&list) << endl;
break;
}
case 3:
{
int data, k;
cout << "Nhap vao gia tri cua phan tu can chen" << endl;
cin >> data;
cout << "Nhap vao vi tru can chen" << endl;
cin >> k;
Insert_K(&list, data, k);
cout << "Danh sach sau khi chen: " << endl;
List_Display(&list);
break;
}
case 4:
{
int data;
cout << "Moi nhap vao phan tu can tim" << endl;
cin >> data;
int k = Search(&list, data);
if (k)
cout << "Tim thay phan tu co gia tri " << data << " tai vi
tri : " << k << endl;
else
cout << "Khong tim thay phan tu co gia tri: " << data <<
endl;
break;
}
case 5:
{
int k;
cout << "Nhap vao vi tri can xoa: " << endl;
cin >> k;
Del_K(&list, k);
cout << "Danh sach sau khi xoa: " << endl;
List_Display(&list);
break;
}
case 6:
{
int data;
cout << "Nhap vao phan tu can xoa: " << endl;
cin >> data;
Del_X(&list, data);
cout << "Danh sach sau khi xoa: " << endl;
List_Display(&list);
break;
}
case 7:
{
int data;
cout << "Nhap vao phan tu can them vao dau danh sach: " << endl;
cin >> data;
Insert_First(&list, data);
cout << "Danh sach sau khi them vao dau danh sach: " << endl;
List_Display(&list);
break;
}
case 8:
{
int data;
cout << "Nhap vao phan tu can them vao cuoi danh sach: " << endl;
cin >> data;
Insert_Last(&list, data);
cout << "Danh sach sau khi them vao cuoi danh sach: " << endl;
List_Display(&list);
break;
}
case 9:
{
system("CLS");
cout << "CHON CHUC NANG" << endl;
cout << "*********************************" << endl;
cout << "1. Kiem tra danh sach rong" << endl;
cout << "2. Hien thi do dai danh sach" << endl;
cout << "3. Chen phan tu x vao vi tri k trong danh sach" << endl;
cout << "4. Tim mot phan tu trong danh sach" << endl;
cout << "5. Xoa phan tu tai vi tri x" << endl;
cout << "6. Xoa phan tu x trong danh sach" << endl;
cout << "7. Them mot phan tu vao dau danh sach" << endl;
cout << "8. Them mot phan tu vao cuoi danh sach" << endl;
cout << "9. Xoa man hinh" << endl;
cout << "10. Exit" << endl;
break;
}
case 10:
break;
}
} while (check != 10);
system("pause");
}
II.
#include <iostream>
using namespace std;
typedef struct Node
{
int data;
Node* next;
};
struct List
{
Node* firstNode;
};
void List_Init(List* list)
{
list->firstNode = 0;
}
void List_Add(List* list, int data)
{
Node* newnode = new Node;
newnode->data = data;
newnode->next = list->firstNode;
list->firstNode = newnode;
}
void List_InsertAfter(Node* node, int data)
{
Node* newnode = new Node;
newnode->data = data;
newnode->next = node->next;
node->next = newnode;
}
void List_RemoveFirst(List* list)
{
Node* obsoleteNode = list->firstNode;
list->firstNode = list->firstNode->next;
delete obsoleteNode;
}
void List_RemoveAfter(Node *node)
{
Node *obsolete = node->next;
node->next = obsolete->next;
delete obsolete;
}
void List_DeleteAll(List* list)
{
Node* node = list->firstNode;
while (node != 0)
{
Node* nextnode = node->next;
delete node;
node = nextnode;
}
list->firstNode = 0;
}
int List_Length(List* list)
{
Node* node = list->firstNode;
int i = 0;
while (node != 0)
{
i++;
node = node->next;
}
return i;
}
Node* List_Search(List* list, int data)
{
Node* node = list->firstNode;
while (node!=0)
{
if (node->data == data)
return node;
else
node = node->next;
}
return 0;
}
void List_Display(List* list){
Node* node = list->firstNode;
int i = List_Length(list);
cout << "Do dai cua list la " << i << endl;
if (list->firstNode == 0)
cout << "List rong" << endl;
else
{
while (node != 0)
{
cout << "Dia chi cua node la: " <<i<< &node->data<<endl;
cout << "Node->data= " << node->data << endl;
cout << "Node->next= " << node->next << endl;
node = node->next;
i--;
}
cout << endl;
}
}
void main()
{
List l;
List_Init(&l);
List_Display(&l);
List_Add(&l, 1);
List_Add(&l, 2);
cout << "Noi dung List sau hai lenh List_Add 1 va 2" << endl;
List_Display(&l);
List_Add(&l, 3);
List_Add(&l, 4);
cout << "Noi dung List sau hai lenh List_Add 3 va 4" << endl;
List_Display(&l);
List_RemoveFirst(&l);
cout << "Noi dung sau khi remove first" << endl;
List_Display(&l);
List_RemoveAfter(List_Search(&l, 3));
cout << "Noi dung sau khi remove node nam sau node co data=3" << endl;
List_Display(&l);
List_InsertAfter(List_Search(&l, 3),100);
cout << "Noi dung sau khi chen node co data 100 dung sau node co data=3"
<< endl;
List_Display(&l);
List_DeleteAll(&l);
cout << "Noi dung sau khi xoa tat ca" << endl;
List_Display(&l);
system("pause");
}
III.
//
//
//
//
//
#include <iostream>
#include <conio.h>
#include <string.h>
using namespace std;
typedef struct SV
{
char maso[30];
char hoten[30];
char lop[10];
};
typedef struct Node
{
SV data;
Node* Left;
Node* Right;
};
typedef struct List
{
Node* Head;
Node* Tail;
};
void List_Init(List* list)
{
list->Head = NULL;
list->Tail = NULL;
}
int Isempty(List* list)
{
return (list->Head == NULL);
}
int List_Length(List* list)
{
Node* PH = list->Head;
Node* PT = list->Tail;
int i = 0;
if (PH != NULL)
i = 1;
while (PH != NULL)
{
if (PH == PT) break;
PH = PH->Right;
i++;
if (PH == PT) break;
PT = PT->Left;
i++;
}
return i;
}
Node* MakeNode(SV data)
{
while (P != list->Tail->Right)
{
cout << P->data.maso << "****" << P->data.hoten << "****" << P>data.lop << endl;;
P = P->Right;
}
cout << endl;
}
void main()
{
List list;
List_Init(&list);
int check;
cout << "CHON CHUC NANG" << endl;
cout << "*********************************" << endl;
cout << "1. Them mot phan tu vao cuoi danh sach" << endl;
cout << "2. Hien thi danh sach Sinh vien" << endl;
cout << "3. Tim sinh vien theo ten" << endl;
cout << "4. Xoa sinh vien theo ten" << endl;
cout << "5. Xoa man hinh" << endl;
cout << "6. Exit" << endl;
do
{
cout << "Bam chon" << endl;
cin >> check;
switch (check)
{
case 1:
{
SV data;
cout << "Nhap vao phan tu can them vao cuoi danh sach: " <<
endl;
cout << "Nhap vao ma so cua Sinh vien can chen" << endl;
fflush(stdin);
cin.getline(data.maso, 30);
cout << "Nhap vao ho ten cua Sinh vien can chen" << endl;
fflush(stdin);
cin.getline(data.hoten, 30);
cout << "Nhap vao lop cua Sinh vien can chen" << endl;
fflush(stdin);
cin.getline(data.lop, 10);
//cin.ignore();
Insert_Last(&list, data);
cout << "Them sinh vien thanh cong" << endl;
break;
}
case 2:
{
cout << "Danh sach sinh vien" << endl;
cout << "******************************************" <<
endl;
List_Display(&list);
break;
}
case 3:
{
SV data ;
cout << "Nhap vao ma so cua Sinh vien can tim" << endl;
fflush(stdin);
cin.getline(data.maso, 30);
cout << "Nhap vao ho ten cua Sinh vien can tim" << endl;
fflush(stdin);
cin.getline(data.hoten, 30);
cout << "Nhap vao lop cua Sinh vien can tim" << endl;
fflush(stdin);
cin.getline(data.lop, 10);
//cin.ignore();
cout << "Thong tin sinh vien duoc tim thay" << endl;
IV.
//
//
//
//
Thut ton sp xp
#include <iostream>
#define N 5
void Selection_sort(int a[N])
{
using namespace std;
int min,id;
for (int i = 0; i < N; i++)
{
min = a[i];
id = i;
{
while (a[i]<x && i<right) i++;
while (a[j]>x && j>left) j--;
if (i <= j)
{
y = a[i]; a[i] = a[j]; a[j] = y;
i++; j--;
}
} while (i <= j);
if (left<j) qs(a, left, j);
if (i<right) qs(a, i, right);
}
void Quick_sort(int a[N])
{
using namespace std;
qs(a, 0, N - 1);
for (int i = 0; i < N; i++)
cout << a[i] << " ";
cout << endl;
}
void main()
{
using namespace std;
int a[N];
int c;
cout << "Nhap vao mang so nguyen" << endl;
for (int i = 0; i < N; i++)
{
cout << "Nhap vao phan tu thu: " << i +
cin >> a[i];
}
cout << "Chon thuat toan sap xep:" << endl;
cout << "=================================" <<
cout << "1. Selection Sort" << endl;
cout << "2. Bubble Sort" << endl;
cout << "3. Insertion Sort" << endl;
cout << "4. Quick Sort" << endl;
cin >> c;
switch (c)
{
case 1:
{
cout << "Ket qua sap xep khi dung thuat
endl;
Selection_sort(a);
break;
}
case 2:
{
cout << "Ket qua sap xep khi dung thuat
endl;
Bubble_sort(a);
break;
}
case 3:
{
cout << "Ket qua sap xep khi dung thuat
endl;
Insertion_sort(a);
break;
}
case 4:
{
cout << "Ket qua sap xep khi dung thuat
endl;
Quick_sort(a);
break;
1 << endl;
endl;
}
default:
break;
}
system("pause");
}
V.
Class s phc
#include<iostream>
using namespace std
class Sophuc{
float rm, ri
public:
Sophuc(float a=1, float b=1) {
rm=a
ri=b
}~
Sophuc() { }
friend Sophuc operator+(Sophuc a, Sophuc b)
friend Sophuc operator(Sophuc a, Sophuc b)
friend Sophuc operator*(Sophuc a, Sophuc b)
friend Sophuc operator/(Sophuc a, Sophuc b)
friend ostream& operator<<(ostream& os, Sophuc q)
}
Sophuc operator+(Sophuc a, Sophuc b) {
Sophuc q
q. rm=a. rm+b. rm
q. ri=a. ri+b. ri
return q
}S
ophuc operator-(Sophuc a, Sophuc b) {
Sophuc q
q. rm=a. rmb. rm
q. ri=a. rib. ri
return q
}S
ophuc operator*(Sophuc a, Sophuc b) {
Sophuc q
q. rm=a. rm*b. rma. ri*b. ri
q. ri=a. rm*b. ri+a. ri*b. rm
return q
}S
ophuc operator/(Sophuc a, Sophuc b) {
Sophuc q
q. rm=(a. rm*b. rm+a. ri*b. ri) /(b. rm*b. rm+b. ri*b. ri)
q. ri=(a. ri*b. rma. rm*b. ri) /(b. rm*b. rm+b. ri*b. ri)
return q
}o
stream& operator<<(ostream& os, Sophuc q) {
return os<<"("<<q. rm<<"+"<<q. ri<<"i) "
}i
nt main()
{
float a, b, c, d
cout<<"Nhap phan thuc cho so phuc thu nhat"<<endl
cin>>a
cout<<"Nhap phan ao cho so phuc thu nhat"<<endl
cin>>b
cout<<"Nhap phan thuc cho so phuc thu hai"<<endl
cin>>c
cout<<"Nhap phan ao cho so phuc thu hai"<<endl
cin>>d
Sophuc m(a, b) , n(c, d)
cout<<m<<"+"<<n<<"="<<m+n<<endl
cout<<m<<""<<n<<"="<<mn<<endl
cout<<m<<"*"<<n<<"="<<m*n<<endl
cout<<m<<"/"<<n<<"="<<m/n<<endl
system("pause")
return 0
}