Mong bạn like hoặc +1 để giúp đỡ blog có thể duy trì hoạt động lâu dài
Đây là một bài nâng cao về danh sách đặc
dành cho các bạn đang học môn cấu trúc dữ liệu
Bài liên quan: Cài đặt và sử dụng Code::Blocks 10.05
#include <stdio.h>
#define SIZE 10
typedef struct List{
int X[SIZE];
int Y[SIZE];
int Count;
};
void MakeNullList(List &L){
L.Count=0;
}
int EmptyList(List L){
return (L.Count==0);
}
void PrintList(List L){
for(int i=0;i<L.Count;i++)
printf("(%d,%d) ; ",L.X[i],L.Y[i]);
}
void InsertList(int X,int Y, int P, List &L){
if (L.Count == SIZE)
printf("Danh sach Day! K them duoc");
else if (P<1 || P>L.Count+1)
printf("Vi tri khong hop le!");
else{
for(int i=L.Count;i>= P ; i--){
L.X[i]=L.X[i-1];
L.Y[i]=L.Y[i-1];
}
L.X[P-1]=X;
L.Y[P-1]=Y;
L.Count++;
}
}
void DeleteList(int P, List &L){
if (EmptyList(L))
printf("\nK co gi de xoa!");
else if (P<1 || P>L.Count)
printf("\nVi tri xoa k hop le");
else{
for(int i=P-1;i<L.Count-1;i++){
L.X[i] = L.X[i+1];
L.Y[i] = L.Y[i+1];
}
L.Count--;
}
}
void Reverse(List &L){
int n = L.Count/2;
for(int i=0;i<n ;i++ ){
int t = L.X[i];
L.X[i]=L.X[L.Count-1-i];
L.X[L.Count-1-i]=t;
t = L.Y[i];
L.Y[i]=L.Y[L.Count-1-i];
L.Y[L.Count-1-i]=t;
}
}
void Distinct(List &L){
for(int i=0;i<L.Count-1;i++){
int j = i+1;
while(j<L.Count){
if (L.X[i]==L.X[j]&&L.Y[i]==L.Y[j])
DeleteList(j+1,L);
else
j++;
}
}
}
void OrderList(List &L){
for(int i=0;i<L.Count-1;i++)
for(int j=i+1;j<L.Count;j++)
if (L.X[i] > L.X[j]){
int t = L.X[i];
L.X[i] = L.X[j];
L.X[j] = t;
t = L.Y[i];
L.Y[i] = L.Y[j];
L.Y[j] = t;
}
}
int main(){
List L;
MakeNullList(L);
InsertList(3,1,1,L);
InsertList(1,3,1,L);
InsertList(3,6,2,L);
InsertList(7,5,3,L);
InsertList(4,3,4,L);
InsertList(3,6,5,L);
InsertList(3,1,5,L);
Distinct(L);
PrintList(L);
printf("\n");
OrderList(L);
PrintList(L);
return 1;
}