cau truc du lieu- thay truong

10
//CAU TRUC STACK #define max 100 int a[max], sp; void empty_stack() { sp = -1; } void push(int x) { if(sp<max-1) a[++sp]=x; } int pop(int &x) { if(sp <0) return 0; else x = a[sp--]; return 1; } // CAU TRUC QUEUE #define max 10 int a[max], front, rear,n=0; void queue_empty() { front = -1; rear = -1; } int check_queue_empty() { if( front == -1||rear == -1) return 1; else return 0; } int check_queue_full() { if((rear -front == max-1)|| (front == rear +1)) return 1; else return 0; } int insert_queue(int x) { if(rear-front==max-1) return -1; else { if(front==-1) front = 0; if(rear == max-1) { for(int i = front;i<=rear;i++) a[i-front]=a[i]; rear = max-1-front; front = 0; } rear++; a[rear] = x; return rear; } } int delete_queue(int &x) { if(front == -1) return 0; else { x=a[front++]; if(front >rear) { front =-1; rear = -1; } return 1; } } //BAI 8 DSLK KHÔNG THỨ TỰ #include<iostream.h> struct node { int info; node *link; }; node *first; void empty_list() { first = NULL; } void print() { node *p = first; while(p!=NULL) { cout<<p->info<<"\t"; p = p->link; } } node *search_list( int x) { node *p = first; while(p!=NULL&&p->info!=x) p = p->link; return p; } void insert_first(int x) { node *p = new node; p->info = x; p->link=first; first = p; } void insert_last(int x) { node*p= new node; p->info = x; p->link=NULL; if(first==NULL) first = p; else { node *q = first; while(q->link!=NULL) q = q->link; q->link=p; } } int del_first() { if(first==NULL) return 0; else { node *p = first; first = p->link; delete p; return 1; } } int del_last() { if(first==NULL) return 0; else { node *p, *q; p = first; while(p->link!=NULL) { q=p; p=p->link; } if(q!=first) q->link=NULL; else//1gia tri duy nhat first=NULL; delete p; return 1; } } void search_del(int x) {

Upload: thien-dinh

Post on 20-Apr-2015

32 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Cau Truc Du Lieu- Thay Truong

//CAU TRUC STACK#define max 100int a[max], sp;void empty_stack(){

sp = -1;}void push(int x){

if(sp<max-1)a[++sp]=x;

}int pop(int &x){

if(sp <0) return 0;else x = a[sp--];return 1;

}// CAU TRUC QUEUE#define max 10int a[max], front, rear,n=0;void queue_empty(){

front = -1;

rear = -1;}int check_queue_empty(){

if( front == -1||rear == -1) return 1;else return 0;

}int check_queue_full(){ if((rear -front == max-1)||(front == rear +1))

return 1;else

return 0;}int insert_queue(int x){

if(rear-front==max-1) return -1;else{

if(front==-1) front = 0;if(rear == max-1){

for(int i = front;i<=rear;i++)

a[i-front]=a[i];rear = max-1-front;front = 0;

}rear++;a[rear] = x;return rear;

}}int delete_queue(int &x){

if(front == -1) return 0;else{

x=a[front++];if(front >rear){

front =-1;rear = -1;

}return 1;

}}

//BAI 8 DSLK KHÔNG THỨ TỰ#include<iostream.h>struct node{

int info;node *link;

};node *first;void empty_list(){

first = NULL;}void print(){

node *p = first;while(p!=NULL){

cout<<p->info<<"\t";p = p->link;

}}node *search_list( int x){

node *p = first;while(p!=NULL&&p->info!=x)

p = p->link;return p;

}void insert_first(int x){

node *p = new node;p->info = x;p->link=first;first = p;

}void insert_last(int x){

node*p= new node;p->info = x;p->link=NULL;if(first==NULL)

first = p;else{

node *q = first;while(q->link!=NULL)

q = q->link;q->link=p;

}}int del_first(){

if(first==NULL)return 0;

else{

node *p = first;first = p->link;delete p;return 1;

}}int del_last(){

if(first==NULL)return 0;

else{

node *p, *q;p = first;while(p->link!=NULL){

q=p;p=p->link;

}if(q!=first)

q->link=NULL;else//1gia tri duy nhat

first=NULL;delete p;return 1;

}}void search_del(int x){

node *p,*q;p = first;while(p!=NULL && p->info!=x){

q=p;p = p->link;

}cout<<p->info<<"da xoa \n";q->link = p->link;delete p;

}void main(){

int chon,x;empty_list();do{

cout<<"\t0.Thoat\n"<<"\t1.Them dau danh sach\n"<<"\t2.Them cuoi danh sach\n"<<"\t3.Xoa dau danh sach\n"<<"\t4.Xoa cuoi danh sach\n"<<"\t5.Duyet danh sach\n"<<"\t6.Tim phan tu\n"<<"\t7.Tim va xoa phan tu\n"<<"Nhap chon: ";cin >>chon;

switch(chon){

case 1: cout<<"Nhap gia tri can them: ";

cin >> x;insert_first(x);break;

case 2: cout<<"Nhap gia tri can them: ";

cin >> x;

Page 2: Cau Truc Du Lieu- Thay Truong

insert_last(x);break;

case 3:if( del_first())

cout<<"Da xoa \n";else

cout<<"Khong xoa dc\n";break;

case 4: if(del_last())

cout<<"Da xoa\n";else

cout<<"Khong xoa dc";break;

case 5:print();break;

case 6:cout<<"Nhap so can tim: ";cin >> x;;if(search_list(x))

cout<<"Tim thaytai :"<<search_list(x);

else

cout<<"Khong co\n";break;

case 7:cout<<"Nhap phtu can xoa: ";cin >> x;search_del(x);break;

}cout<<endl;

}while(chon != 0);cout<<endl;

//Bai 9 DSLK - co thu tu#include<iostream.h>struct node{

int info;node *link;

};node *first;void empty_list(){

first = NULL;}void print(){

node *p = first;while(p!=NULL){

cout<<p->info<<"\t";p = p->link;

}}void insert_list(int x){

node *p = new node;p->info = x;p->link= NULL;if(first==NULL)

first = p;else{

node *r,*q;q = first;while(q!=NULL && q->info<x){

r=q;q = q->link;

}r->link = p;p->link = q;

}}node *search_list(int x){

node *p = first;while(p!=NULL&&p->info<x)

p=p->link;return p;

}int search_del(int x){

if(first==NULL)return 0;

node *p,*q;p= first;while(p!=NULL&&p->info<x){

q=p;p=p->link;

}if(p->info!=x)

return 0;else{

cout<<p->info<<" da xoa \n";if(p==first)

first = p->link;else

q->link = p->link;delete p;return 1;

}}void main(){

int chon,x;empty_list();do

{cout <<"\t1.Them 1 phan tu\n";cout <<"\t2.Tim 1 phan tu\n";cout <<"\t3.Duyet danh sach\n";cout <<"\t4.Tim va xoa phan tu\n";cout <<"Nhap chon: ";cin >>chon;switch(chon){case 1:

cout<<"Nhap phan tu can them :";

cin >> x; insert_list(x);break;

case 2:cout<<"Nhap phtu can tim :";cin >> x;if(search_list(x))

cout<<"Tim thay\n";else

cout<<"khong tim thay\n";break;

case 3:print(); break;

case 4:cout<<"Nhap phtu can xoa :";

cin >> x;if(search_del(x))

cout<<"Da xoa\n";else

cout<<"khong tim thay\n";break;

}cout<<endl;

}while(chon != 0);cout<<endl;

}

BAI 10 – 11 CÀI ĐẶT STACK – QUEUE BẰNG DSLK –(XEM BÀI CÂY)// BAI 12 DANH SACH LIEN KIET KEP

#include<iostream.h>struct Node{

int info ;Node *pre,*next;

};Node *first ,*last ;void list_empty(){

first = NULL;last = NULL;

}void process_list(){

Node *p = first;while( p!=NULL){

cout<< p->info <<'\t';p=p->next;

}}void insert_first(int x)

{Node *p;p = new Node;p ->info = x;p->pre = NULL;if(first == NULL){

p->next = NULL;last = p;

}else

Page 3: Cau Truc Du Lieu- Thay Truong

{p->next = first;first->pre = p;

}first = p;

}void insert_last(int x){

Node *p;p = new Node;p ->info = x;p->next = NULL;if(last == NULL){

p->pre = NULL;first = p;

}else{

p->pre = last;last->next = p;

}last = p;

}int delete_first(int &x){

if ( first != NULL ){

Node * p = first ;x = first -> info;if ( first==last )

last = first = NULL;else {first = first ->next ;first -> pre = NULL ;}x = p->info;delete p ;return 1;

}else

return 0;}int delete_last(int &x){

if ( last != NULL ){

Node * p = last ;x = last ->info;if ( first == last )

last = first = NULL;else {

last = last -> pre ;last -> next = NULL ;

}delete p ;return 1 ;

}else

return 0 ;}int search_delete(int x){

if(first)

{Node * p = first;

while(p->next!=NULL&&p->info!=x)p = p->next;

if(first==last)first=last = NULL;

else if(first == p){first = first->next;

first->pre = NULL;}else if(last ==p){

last= last->pre;last->next = NULL;

}else{

Node *q = p->next, *r = p->pre;

r->next =q;q->pre = r;

}delete p;return 1;

}return 0;

}int search_insert(int x,int y){// tim phtu co giab tri = x hay > // gan nhat sau x them 1 phtu vao sau phtu vua tim duoc

if(first == NULL)return 0;

else {

Node *p,*q,*r;p =q = first;while(p!= NULL && (q->info)<x){

q=p;p = p->next;

}if(q->info<x && p == NULL)

return 0;else{

if(p == first){// p = first (do ko thuc hien vong

lap while) p = p->next;p->pre = first;

}if(p == NULL){//p = null (phan tu can chen o

cuoi ds)insert_last(y);return 1;

}r = new Node;r->info =y;q->next = r; r->pre = q;r->next = p; p->pre = r;return 1;

}}

return 0;}int main (){

int chon,x,y;list_empty();do{

cout<<"1.Xuat cac phtu trong DS !\n";

cout<<"2.Them phtu vao dau DS !\n";

cout<<"3.Them phtu vao cuoi DS !\n";

cout<<"4.Xoa phan tu dau DS !\n";cout<<"5.Xoa phan tu cuoi DS !\n";cout<<"6.Tim va xoa phtu \n";cout<<"7.Tim va them phan tu\n";cout<<"Chon ";cin >>chon;switch (chon){case 1:

process_list();cout<<endl;break;

case 2:cout<<"Nhap phtu can them vao

:";cin>>x; insert_first(x);break;

case 3:cout<<"Nhap phtu can them vao

:";cin>>x; insert_last(x);break;

case 4:if(delete_first(y))

cout<<y<< " da xoa\n";else cout<<" DS rong \n";break;

case 5:if(delete_last(y))

cout<<y<<" da xoa\n";else cout<<" DS rong \n";break;

case 6:cout<<"Nhap phtu can xoa:";cin>>x;if(search_delete( x))

cout<<" da xoa \n";else

cout<<" DS rong\n";break;

case 7:cout<<"Nhap phan tu can tim:";cin>>x;cout<<"Nhap phan tu can

them:";cin>>y;if(search_insert( x, y))

cout<<" da thuc hien \n";else

cout<<"khong the them !!!\n";break;

}}while(chon!=0);return 0;

Page 4: Cau Truc Du Lieu- Thay Truong

}

//DANH SACH LIEN KET VONG#include<iostream.h>struct Node{

int info ;Node *link;

};Node *first,*last ;void list_empty(){

first = NULL;last = NULL;

}void process_list() {

if(first){

Node *p= first;do{

cout<<(p->info)<<'\t';p =p->link;

}while(p!= first);}

}void insert_first(int x){

Node *p= new Node;p->info = x;if(first == NULL){

first =last = p;first->link =last;last->link = first;

}else{

p->link= first;first = p;last->link = first;

}}void insert_last(int x){

Node *p = new Node;p ->info = x;if (first == NULL){

first =last = p;first->link =last;last->link = first;

}else{

last->link = p;last = p;last->link = first;

}}int delete_first(int &x){

if (first == NULL)return 0;

else

{Node *p = first;x =p->info;if(first == last){

first = last = NULL;}else{

first = p->link;last->link = first;

}delete p;return 1;

}return 0;

}int delete_last(int &x){

if(last == NULL)return 0;

else{

Node *p, *q;p = first;while((p->link)!=first){

q=p;p=p->link;

}if(last== first)

first = last = NULL;else{

last = q;last->link = first;

}x = p->info;delete p;return 1;

}}int search_delete(int x){

if(first){

if(first->info==x){

int y;delete_first(y);

}else{

Node *p, *q;p= first;do{

q=p;p=p->link;

}while(p!= first&&p ->info!=x);if(p ==last){

last = q;last->link = first;

}else

q->link=p->link;delete p;

}return 1;

}return 0;

}void main (){

int chon, x;list_empty();do{

cout <<"\t0.thoat\n"<<endl;cout <<"\t1.Xuat cac phan tu\n";cout <<"\t2.Them phan tu vao dau\

n";cout <<"\t3.Xoa phan tu dau\n";cout <<"\t4.them phan tu cuoi\n";cout <<"\t5.Xoa phan tu cuoi\n";cout <<"\t6.Tim va xoa \n";cout <<"Nhap chon: ";cin >>chon;switch(chon){case 1:

process_list();break;

case 2:cout <<"Nhap phan tu can them:"; cin>>x;insert_first(x);break;

case 3:if(delete_first(x))cout<<"da xoa"<<x<<endl;else

cout <<"DS rong \n";break;

case 4:cout <<"Nhap phan tu can

them :";cin>>x;insert_last(x);break;

case 5:if(delete_last(x))cout<<" da xoa "<<x<<endl;else

cout <<"DS rong \n";break;

case 6:cout <<"Nhap phan tu can

xoa:";cin>>x;if(search_delete( x))

cout<<" da xoa\n";else cout<<" DS rong \n";break;

}cout<<endl;

}while(chon != 0);cout<<endl;

Page 5: Cau Truc Du Lieu- Thay Truong

}

//BAI 18 SAP XEP#include <iostream.h>#define max 100int a[max],n;void Nhap(){

for (int i = 0; i<n; i++){

cout<<"Nhap ["<<i<<"] : "; cin>>a[i];}

}void xuat(){

for (int i = 0; i<n; i++)cout<<a[i]<<"\t";

cout<<endl;}void hoanvi(int &a, int &b){

int tam = a;a = b;b = tam;

}void SelectionSort(int a[], int n){//chon truc tiep min

int min, i , j;for( i = 0; i<n-1;i++){

min = i;for( j = i+1; j<n;j++)

if(a[min]>a[j])min = j;

hoanvi(a[min],a[i]);}

}void InsertionSort(int a[], int n){//chen truc tiep post

int post, i , x;for( i = 1; i<n; i++){

x = a[i];//luu a[i] laipost = i-1;while(post>=0&&a[post]>x){

a[post+1]=a[post];post--;

}a[post+1] = x;

}}void InterChangeSort(int a[],int n){//doi cho truc tiep

int i,j;for(i = 0; i<n-1;i++)

for(j = i+1; j<n;j++)if(a[j]<a[i])

hoanvi(a[j],a[i]);}void BubbleSort(int a[], int n){//nho noi len tren

int i,j;for(i = 0; i<n-1;i++)

for(j = n-1; j>i;j--)if(a[j-1]>a[j])

hoanvi(a[j-1],a[j]);}int TimTuanTu(int x){

int i=0;while(i<n&&a[i]<x) i++;if(i==n) return 0;else return i;

}int TimNhiPhan(int x){

int dau = 0, giua, cuoi = n-1;while(dau<=cuoi){

giua = (dau+cuoi) / 2;if(a[giua]>x) cuoi = giua -1;else if(a[giua]<x) dau = giua +1;

else return giua;}return 0;

}void main(){

int chon,x ;do{

cout<<"1.Nhap danh sach \n";cout<<"2.Xuat danh sach \n";cout<<"3.SelectionSort \n";cout<<"4.InsertionSort\n";cout<<"5.InterChangeSort \n";cout<<"6.BubbleSort \n";cout<<"7.Tim tuan tu \n";cout<<"8.Tim nhi phan\n";cout<<"Chon :"; cin>>chon;switch(chon){case 1:

cout<<"Nhap so phan tu :";cin>>n;Nhap();break;

case 2:xuat();break;

case 3:SelectionSort( a,n);break;

case 4:InsertionSort( a,n);break;

case 5:InterChangeSort( a,n);break;

case 6:BubbleSort( a,n);break;

case 7:cout<<"Nhap phan tu can tim ";cin>>x;cout<<" tim duoc o vi tri thu "

<<TimTuanTu(x)<<endl;break;

case 8:cout<<"Nhap phan tu can tim ";cin>>x;

cout<<" tim duoc o vi tri thu "<<TimNhiPhan( x)<<endl;

break;}

}while(chon!=0);}

//BAI 19 CAY NHI PHAN#include<iostream.h>///////////cau tuc cay////////struct node{

int info;node *left, * right;

};node *root;typedef node *stnode;void empty_tree(){

root = NULL;}node *search_tree(node *p, int x){

if(p){

if(p->info== x)

return p;else

if(p->info>x)return search_tree(p->left,x);

elsereturn search_tree(p-

>right,x);}return NULL;

}int insert_tree(stnode &p, int x){

if(p){

if(p->info==x)return 0;

elseif(p->info>x)

return insert_tree(p->left,x);else

return insert_tree(p->right,x);}p = new node;if(p==NULL) return -1;p->info= x;p->left= NULL;p->right = NULL;return 1;

}void searchStandFor(stnode &p, stnode &q){//tim nut trai nho nhat ben nhanh phai

if(q->left)searchStandFor(p,q->left);

else{

Page 6: Cau Truc Du Lieu- Thay Truong

p->info = q->info;p = q;q = q->right;

}}int del_tree(stnode &T, int x){

if(T==NULL)return 0;

if(T->info>x)return del_tree(T->left,x);

if(T->info<x)return del_tree(T->right,x);

else{//da tim thay

node *p = T;if(T->left==NULL)

T=T->right;else

if(T->right==NULL)T=T->left;

elsesearchStandFor(p, T->right);

delete p;return 1;

}}void NLR(node *p){

if(p){

cout<<p->info<<"\t";NLR(p->left);NLR(p->right);

}}void LNR(node *p){

if(p){

LNR(p->left);cout<<p->info<<"\t";LNR(p->right);

}}void LRN(node *p){

if(p){

LRN(p->left);LRN(p->right);cout<<p->info<<"\t";

}}////////////stack////////////////////struct stack{

node *key;stack *link;

};stack *sp;void empty_stack(){

sp = NULL;}void push(node *p)

{stack *q = new stack;q->key = p;q->link = sp;sp = q;

}int pop(node *&p){

if(sp==NULL)return 0;

else{

p = sp->key;stack *q = sp;sp = q->link;delete q;return 1;

}}int pop1(stnode &p){//chi lay gia ko, ko xóa trong Stack

if(sp==NULL)return 0;

else{

p = sp->key;return 1;

}}void NLR_stack(node*p){

empty_stack();node *q;push(p);while(sp!=NULL){

pop(q);cout<<q->info<<"\t";if(q->right)

push(q->right);if(q->left)

push(q->left);}

}void LNR_stack(node *p){

empty_stack();node *q;push(p);while(sp!=NULL){

if(p->left){

push(p->left);p = p->left;

}else{

pop(q);cout<<q->info<<"\t";if(q->right){

push(q->right);p = q->right;

}}

}}void LRN_stack(node *p){

empty_stack();node *q,*s;int x =0;//gia tri xuat ra cuoi cungpush(p);while(sp!=NULL){

if(p->left){

push(p->left);p = p->left;

}else{

pop1(s);if(s->right && s->info>x){

push(s->right);p = s->right;

}else{

pop(q);x = q->info;cout<<x<<"\t";

}}

}}////////////////QUEUE////////////////struct queue{

node *key;queue *link;

};queue *front, *rear;void empty_queue(){

front = NULL;rear = NULL;

}void Insert_queue(node *x){

queue *p = new queue;p->key = x;p->link = NULL;if(rear==NULL)

front = p;else

rear->link = p;rear = p;

}int del_queue(node *&x){

if(front !=NULL){

queue *p=front;front = p->link;x = p->key;if(front==NULL)

rear = NULL;delete p;return 1;

Page 7: Cau Truc Du Lieu- Thay Truong

}else

return 0;}void duyetcaytheomuc(node *p){

empty_queue();node *q;Insert_queue(p);while(rear !=NULL){

del_queue(q);cout<<q->info<<"\t";if(q->left)

Insert_queue(q->left);if(q->right)

Insert_queue(q->right);}

}void main(){

int chon,x;empty_tree();do{

cout<<"0. Nhap 0 de thoat \n"<<"1.Them 1 phan tu \n"<<"2.Tim 1 phan tu \n"

<<"3.Xoa 1 phan tu \n"<<"4. NLR(de quy) \n"<<"5. LNR(de quy) \n"<<"6. LRN(de quy) \n"<<"7. NLR(stack) \n"<<"8. LNR(stack) \n"<<"9. LRN(stack) \n"<<"10.Duyet cay theo muc \n"<<"Chon :"; cin>>chon;

switch (chon){case 1:

cout<<"Nhap ph tu can them :";cin>>x;if(insert_tree(root,x))

cout<<"them thanh cong\n";else

cout<<"khong the \n";break;

case 2:cout<<"Nhap phan tu can tim :"; cin>>x;if(search_tree(root, x))

cout<<"Tim thay\n";else

cout<<"khong tim thay\n";break;

case 3:

cout<<"Nhap phan tu can xoa :"; cin>>x;if(del_tree(root,x))

cout<<"Da xoa \n";else

cout<<"khong the xoa \n";break;

case 4:NLR(root); break;

case 5:LNR(root); break;

case 6:LRN(root); break;

case 7:NLR_stack(root); break;

case 8:LNR_stack(root); break;

case 9:break;

case 10:duyetcaytheomuc(root);break;

}cout<<endl;

}while (chon != 0);}

//BAI 20 BANG BAM

Page 8: Cau Truc Du Lieu- Thay Truong

#include<iostream.h>#define M 11struct node{

int key;node *next;

};node *heads[M];node *z;// phan tu cam canhvoid init(){

z = new node;z->next = z;for(int i = 0; i<M;i++){

heads[i] = new node; heads[i]->next = z;

}}node *insert(int k,node *t){

node *x,*p;p = t->next;z->key = k;while(p->key<k){

t=p;p = p->next;

}x = new node;x->next = p;t->next = x;x->key = k;return x;

}node *find(int k,node *t){

z->key = k;do{

t = t->next;}while(t->key<k);if(t->key==k)

return t;else

return z;}void xuat(){

for(int i = 0; i<M;i++){

node *p; p = heads[i]->next;cout<<i<<" \t";while(p!=z){

cout<<(p->key)<<"";p = p->next;

}cout <<endl;

}}void main(){

init();int chon,k,x ;do

{cout<<"0. Thoat\n"

<<"1. them phan tu vao bang \n"<<"2.tim 1 phan tu trong bang \n"<<"Chon :";cin>>chon;

switch(chon){

case 1:cout<<"Nhap gia tri can them:";

cin>>k;cout<<k <<"them vao o vi tri "

<<insert(k,heads[k%M])<<endl;break;

case 2:cout<<"Nhap gia tri can tim :";

cin>>x; if(find(x,heads[x%M])==z)

cout<<"khong tim thay"<<endl;else

cout <<x<<" o vi tri "<<find(x,heads[x/M])<<endl;break;

case 3:xuat();break;

}}while(chon != 0);

}