Tải bản đầy đủ (.docx) (28 trang)

thảo luận thương mại điện tử danh sách hàng đợi xắp xếp

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (106.82 KB, 28 trang )

lÝ thuyết:
Mảng:
*khái niệm: mảng là 1 tập hợp có thứ tự gồm các phần tử cùng kiểu dữ liệu ->cấu
trúc dữ liệu
*mỗi phần tử được truy cập và xác định bởi tên mảng và chỉ số phần tử( số thứ tự
phần tử) -> truy cập ngẫu nhiên
*kiểu dữ liệu: 1 chiều, nhiều chiều
*cấu trúc lưu trữ mảng: hình thức lưu trữ kế tiêp
-địa chỉ các phần tử nối tiếp nhau
-các phần tử sắp xếp theo hang
-bộ nhớ cố định
*đặc điểm
-cấu trúc đơn giản , truy cập nhanh
-thiếu mềm dẻo trong phép xóa, chèn
DANH SÁCH:
*khái niệm: danh sách là 1 tập có thứ tự gồm các phần tử cùng kiêu:
- các phần tử biến động
-các phần tử sắp xếp theo thứ tự trước- sau
*Danh sách tuyến tinh: là quan hệ lân cận giữa các phần tử
-hoặc là danh sách rỗng hoặc có dạng( a1,a2, an)
-n là độ dài /kích thước của danh sách
-mỗi phần tử thường là 1 bản ghi bao gồm 1 hoặc nhiều trường (field)
* danh sách con: gồm các phần tử liên tiếp từ a1 đến aj của danh sách
-nếu i =1 gọi là phần tử đầu (prefix)
-nếu j=n gọi là phần tử cuối (postfix)
1
1
*dãy con: là 1 danh sách được tạo thành bằng cách loại 1 số phần tử từ danh sách .
HÀNG ĐỢI
*khái niệm: hang đợi là kiểu danh sách mà thao tác thêm phần tử được thực hiện ở
1 đầu danh sách con fthao tác lấy phần tử ra đươc thực hiện ở đầu kia của danh


sách
*nguyên tắc hoạt động: hoạt động theo nguyên tắc FIFO “vào trước – ra trước”
*ví dụ: xếp hang đợi thanh toán ở siêu thị, soát vé ở rạp chiếu phim
NGĂN XẾP
*khái niệm: ngăn xếp à 1 kiểu danh sách mà thao tác thêm và bớt phần tử được
thực hiện chỉ một đầu danh sách gọi là đỉnh
*nguyên tắc hoạt động: theo nguyên tắc LIFO “vào trước – ra sau”
*ví dụ. ngăn xếp tủ quần áo, chồng sách, hộp chứa đạn súng trường
Bài 1 : Mảng 1 chieu
// mang 1 chieu
#include<stdio.h>
#include<conio.h>
void Nhapmang(int a[100],int n) // nhap mang
{
printf("nhap so phan tu mang la:");
scanf("%d",&n);
2
2
for(int i=0;i<n;i++)
{
printf("Phan tu thu %d cua mang la:", i+1);
scanf("%d", &a[i]);
}
}
void Xuatmang(int a[100], int n) // xuat mang
{
printf("\n Mang da nhap la");
for(int i=0;i<n;i++)
printf("%d,", a[i]);
}

void sxnoibot(int a[100],int n) // sap ep noi bot
{
int i, j, x;
for(int i=1;i<n; i++)
for(int j=n;j>i; j )
if(a[j] < a[j-1])
{
x = a[j];
a[j] = a[j-1];
a[j-1] = x;
}
void sx_chen( int a[max], int n) // sap xep chen
{
3
3
int i,j,v;
for( i=1; i<n; i++)
{
v=a[i];
j=i-1;
while(a[j]>v&&j>=0)
{
a[j+1]=a[j];
j ;
}
a[j+1]=v;
}
}
void sx_chon(int a[max], int n) // sap xep lua chon
{ int i , j , T

for( int i=0; i<n-1; i++)
{
int min =i;
for( int j= 1; j<n; j++)
if (a[j] < a[min])
min =j;
T = a[i];
a[i]=a[min];
a[min]=T;
4
4
}

}
void sxnhanh(int a[ ],int left,int right)
{
int i,j,x;
i=left;
j=right;
x=a[left];
do
{
while(a[i]<x && i<right)
i++;
while(a[j]>x && j>left)
j ;
if(i<=j)
{
int temp=a[i];
a[i]=a[j];

a[j]=temp;
i++;
j ;
}
}
while(i<=j);
5
5
if(i<right)
sxnhanh(a,i,right);
if(j>left)
sxnhanh(a,left,j);
}
printf ("\n day sau khi sap xep la:");
for (int i=0; i<=n; i++)
printf ("%d",a[i]);
}
void timchanduong(int a[100], int n) // tim phan tu duong chan dau tien cua
mang
{
int dem = 0;
for(int i=0; i<=n; i++)
{
if(a[i]>0 && a[i]%2==0)
{
printf("\n phan tu can tim %d nam o vi tri thu %d ",a[i],i);
dem++;
}
}
if(dem==0)

printf("\n khong co phan tu chan duong");
}
6
6
int main() // ham chinh
{
int a[100], n;
printf("nhap vao so phan tu mang:");
scanf("%d",&n);
Nhapmang(a,n);
Xuatmang(a,n);
sxnoibot(a,n);
timchanduong(a,n);
getch();
}
Bài 2 :
Hàm đợi
// hang doi cai dat bang mang.
#include<conio.h>
#include<stdio.h>
#include<iostream.h>
#define max 100 // toi da 100 ptu
typedef struct // dinh nghia kieu
7
7
{
int data[max];
int head;
int tail;
} queue;

void khoitao(queue &q) // khoi tao
{
q.head=0;
q.tail=-1;
}
int ktrong(queue &q) // kiem tra rong
{
return (q.tail==-1);
}
int ktday(queue &q) // kiem tra day
{
return(q.tail==max-1);
}
void them(queue &q,int x) // thêm phan tu
{
if(ktday(q))
cout<<"Hang doi day! \n";
else
{
q.tail++;
8
8
q.data[q.tail]=x;
}
}
void bot(queue &q) // bot phan tu
{
if(ktrong(q))
cout<<"Hang doi rong! \n";
else

{
if(q.head==q.tail)
khoitao(q);
else
q.head=q.head+1;
}
}
void xuat(queue &q) // xuat ra hang doi
{
queue p;
p=q;
if(ktrong(p))
cout<<"Hang doi rong! \n";
else
{
9
9
while(p.head!=p.tail+1)
{
cout<<p.data[p.head]<<" ";
p.head=p.head+1;
}
}
}
void nhap(queue &q) // nhap hang doi
{
int x;
while(x!=-1)
{
cout<<"Nhap nut, -1 de ket thuc: ";

cin>>x;
if(x!=-1)
them(q,x);
}
}

int main() // ham chinh
{
queue q;
int x,a,k;
khoitao(q);
cout<<"Nhap du lieu cho hang doi: ";
10
10
nhap(q);
xuat(q);
cout<<"Lay phan tu o cuoi hang doi: \n";
bot(q);
xuat(q);
cout<<"Nhap phan tu can them: ";
cin>>x;
them(q,x);
xuat(q);
getch();
}
// ham doi cai dat bang danh sach lien ket
#include<conio.h>
#include<stdio.h>
#include<string.h>
struct node // dinh nghia kieu node

{
int item;
struct node *next;
};
typedef struct node *hangdoi;
typedef struct
{
hangdoi head;
hangdoi tail;
11
11
} queue;
void khoitao(queue *q) // khoi tao hang doi
{
q->head=q->tail=NULL;
}
int ktrong(queue *q) // kiem tra hang doi rong
{
return(q->head==NULL);
}
void them(queue *q,int x) // them ptu
{
hangdoi p;
p=new node;
p->item=x;
p->next=NULL;
if(ktrong(q)) q->head=p;
else
{
q->tail->next=p;

q->tail=p;
}
}
int Pop(queue *q) // bot ptu
{
hangdoi p;
12
12
int x;
if(ktrong(q)) printf("Hang Doi rong”);
else
{
p=q->head;
q->head =p->next;;
if(q->head==NULL)
q->tail=q->head;
x=p->item;
}
}
void nhap(queue *q) // nhap
{
int x;
while(x!=-1)
{
printf("Nhap -1 de ket thuc: ");
scanf("%d",&x);
if(x!=-1)
them(q,x);
}
}

void xuat(queue *q) // xuat
{
hangdoi p;
13
13
p=q->head;
if(ktrong(q))
{
printf("Ngan xep rong! ");
return;
}
else
while(p!=NULL)
{
printf("%d",p->item);
p=p->next;
};
}
int main() // ham chinh
{
queue *q;
int x;
khoitao(q);
nhap(q);
xuat(q);
Pop(q);
14
14
xuat(q);
getch();

}
NGĂN XẾP
// cai dat ngan xep bang mang
#include<conio.h>
#include<stdio.h>
#include<iostream.h>
#define max 100
struct stack{ // dinh nghia kieu
int a[max];
int top;
};
void khoitao(stack &s) // khoi tao
{
s.top=-1;
}
int ktrong(stack &s) { // kiem tra rông
return(s.top==-1);
}
int ktday(stack &s){ // kiem tra day
return(s.top==max-1);
}
void them(stack &s,int x){ // them ptu
if(ktday(s)) cout<<"Stack day";
15
15
else
{
s.top++;
s.a[s.top]=x;
}

}
void bot(stack &s){ // bot ptu
int x;
if(ktrong(s))
printf("Stack rong!");
else
{
x=s.a[s.top];
s.top ;
}
}
void nhap(stack &s) // nhap ngan xep
{
int x;
while(x!=-1)
{
printf("Nhap phan tu, -1 de ket thuc:");
scanf("%4d", &x);
if(x!=-1)
16
16
them(s,x);
} }
void xuat(stack &s) // xuat ra
{
stack temp=s;
while (temp.top!=-1)
{
printf("temp.a[temp.top] \n");
temp.top ;

}
printf("Hien thi stack thanh cong! ");
}
int main() // ham chinh
{
int x,a,k;
stack s;
khoitao(s);
printf("Nhap du lieu cho stack: \n");
nhap(s);
xuat(s);
printf("Lay phan tu o dinh ngan xep:\n");
bot(s);
xuat(s);
17
17
getch();
}
// ngan xep bang danh sach lien ket
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{ int d;
node *next;
};
typedef node *stack;
void ktao(stack *top)
{ top=NULL;}
int ktrong(stack top) // kiem tra rong

{return (top==NULL);}
void them(stack *top,int x) // thêm ptu
{
node *p;
p=(node*)malloc(sizeof(node));
p->d=x;
(*p).next=(*top);
18
18
*top=p;
}
void bot(stack *top,int *v) // bot ptu
{ if (ktrong(*top)) printf ("ds rong!");
else
{ *v=(*top)->d;
(*top)=(*top)->next;
}
}
void xuat(stack top) // xuat
{
stack p;
p=top;
if(ktrong(top))
{
printf("Ngan xep rong");
}
else
while(p)
{
printf("%4d",p->d);

p=p->next;
}
}
19
19
int main() // ham chinh
{
stack top; int x; int v;
ktao(&top);
while (x!=-1)
{
printf ("nhap phan tu cho danh sach, -1 de thoat:");
scanf ("%d",&x);
if (x!=-1)
them(&top,x);
}
printf("\n Hien thi stack: ");
xuat(top);
//printf ("\n ds sau khi thuc hien bot\n");
//bot(&top,&v);
//xuat(top);
getch();
}
Câu 3 :
// Cay nhi phan tim kiem.
#include <iostream.h>
#include <conio.h>
#include<stdio.h>
20
20

typedef struct node
{
int item;
node *left;
node *right;
};
typedef struct node *tree;
void khoitao(tree &T) // khoi tao cay
{
T=NULL;
}
void duyettruoc(tree T) // duyet trc
{
if(T!=NULL)
{
printf("%4d", T->item);
duyettruoc(T->left);
duyettruoc(T->right);
}
}
void duyetgiua(tree T) // duyet giua
{
if(T!=NULL)
21
21
{
duyetgiua(T->left);
printf("%4d", T->item);
duyetgiua(T->right);
}

}
void duyetsau(tree T) // duyet sau
{
if(T!=NULL)
{
duyetsau(T->left);
duyetsau(T->right);
printf("%4d", T->item);
}
}
int themnode(tree &T,int x) // tham nut
{
if(T!=NULL)
{
if(T->item==x)
return 0;
22
22
if(T->item>x)
return themnode(T->left,x);
else
return themnode(T->right,x);
}
T=new node;
if(T==NULL)
return -1;
T->item=x;
T->left=T->right=NULL;
return 1;
}

int timkiem(tree &T,int x) // tiem kiem
{
if(T=NULL) printf("\nKhong tim thay %d ",x);
23
23
if(T!=NULL)
{
if(T->item==x)
{printf("\nTim kiem thanh Cong!");
return 1;
}
else
if(T->item <x)
return timkiem(T->right,x); //tim ben trai
else if(T->item >x)
return timkiem(T->left,x); //tim ben phai
}
return 0;
}
void nhap(tree &T) // nhap cay
{
int x;
while(x!=-1)
{
printf(" nhap nut -1 de thoat");
scanf("%d", &x);
if(x!=-1)
themnode(T,x); // them nut moi
}
}

24
24
int demnut(tree &T) // dem nut
{
if(T==NULL)
return 0;
else
return 1+demnut(T->left)+demnut(T->right);
}
int demla(tree &T) // dem nut la
{ int dem=0;
if(T==NULL)
return 0;
dem=demla(T->left)+demla(T->right);
if(T->left==NULL && T->right==NULL)
dem++;
return dem;
}
int chieucao(tree &T) // xac dinh chieu cao cua cay
{
int h;
if(T==NULL)
return 0;
if(T!=NULL)
{
if(T->left==NULL && T->right==NULL)
return 1;
25
25

×