1. Trang chủ
  2. » Công Nghệ Thông Tin

Code cấu trúc dữ liệu và giải thuật ĐH Bách Khoa HN

18 434 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 18
Dung lượng 16,76 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Trang 1

Buoi hoc 3: De quy thap ha noi

#include "conio.h"

#include "stdio.h"

#include "stdlib.h"

/* cai dat ham de qui de tinh n!

f(n) = 0! = 1, n=0

f(n) = f(n-1)*n, n>0

*/

int giaithua(int n)

{

if(n==0)

{

return 1;

}

else

{

int k = giaithua(n-1)*n;

printf("Ham voi dau vao %d tra ve %d\n",n,k); return k;

}

}

/* cai dat ham de qui de tinh x^n (n>=0)

f(n) = 1, n = 0

f(n) = f(n-1)*x , n > 0

*/

int power(int x, int n)

{

if(n==0)

{

return 1;

}

else

{

int k = power(x, n-1)*x;

//printf("Ham voi dau vao %d tra ve %d\n",n,k); return k;

}

}

Trang 2

/* thap ha noi */

void thaphanoi(int n, char a, char b, char c)

{

if (n==1) printf("chuyen 1 dia tu %c sang %c\n",a,c); else{

thaphanoi(n-1, a, c, b);

thaphanoi(1, a,b,c);

thaphanoi(n-1, b, a, c);

}

}

/* Tim min trong mang n ptu >=0 */

int timmin(int A[], int n)

{

if(n==1) return A[0];

else

{

if(timmin(A,n-1)<A[n-1]) return timmin(A,n-1); else return A[n-1];

}

}

int main()

{

//int n = 8;

//int x = giaithua(n);

//int x = power(2,n);

//printf("\nf(%d,%d)=%d\n", 2, n, x);

//thaphanoi(4,'A','B','C');

int A[]={5,1,2,9,4,0,80, 7};

int x = timmin(A,8);

printf("%d\n", x);

getch();

return 1;

}

Buoi hoc 2: Cai dat de quy

Trang 3

#include "conio.h"

#include "stdio.h"

#include "stdlib.h"

/* cai dat cong thuc de qui:

f(0) = 3, n = 0

f(n+1) = 2f(n) + 3, n > 0

*/

int hamf(int n)

{

/* in ra */

printf("Bat dau goi ham voi dau vao la %d\n",n); if(n==0)

{

printf("tra ve gia tri 3 khi n=0\n"); return 3;

}

else

{

printf("goi de qui ham %d\n",n-1); int k = 2*hamf(n-1)+3;

printf("tra ve gia tri %d khi n=%d\n",k,n); return k;

}

}

int main()

{

int x = hamf(30);

getch();

return 1;

}

Buoi hoc 4: De quy co nho

Trang 4

#include "conio.h"

#include "stdio.h"

#include "stdlib.h"

/* Tinh Fibo

f(n) = n voi n<2

f(n) = f(n-1) + f(n-2)

*/

int Fibo(int n)

{

printf("Ham fibo voi n=%d\n",n);

if(n < 2) return n;

else return Fibo(n-1) + Fibo(n-2);

}

int A[10];

int Fibowmem(int n)

{

printf("Ham fibo voi n=%d\n",n);

if(n < 2) return n;

else

{

if(A[n-1]==-1) A[n-1] = Fibowmem(n-1); if(A[n-2]==-1) A[n-2] = Fibowmem(n-2); return A[n-1] + A[n-2];

}

}

/* vi du don gian backtrack */

int C[] = {1,2,3,4};

int D[] = {5,6,7,8};

int kq[] = {0,0};

int UCV(int C[],int D[],int i,int k,int kq[])

{

if(k==1) return 1;

if(k==2){

if((D[i]+kq[0])==10) return 1;

else return 0;

}

return 0;

Trang 5

void Inkq()

{

int i;

printf("\n mot loi giai %d %d \n",kq[0], kq[1]);

}

void backtrack_vidu1(int k)

{

int i;

for(i=0;i<4;i++)

{

if(UCV(C,D,i,k,kq)==1){

if(k==1) kq[0] = C[i];

if(k==2) kq[1] = D[i];

if (k == 2) Inkq();

else backtrack_vidu1(k+1);

} }

}

int main()

{

/*int i;

for (i=0;i<10;i++) A[i]=-1;

printf("************De qui ko nho***********\n");

int x = Fibo(9);

printf("%d\n", x);

printf("************De qui co nho***********\n");

x = Fibowmem(9);

printf("%d\n", x);*/

backtrack_vidu1(1);

getch();

return 1;

}

Buoi hoc 5p1: Quay lui Hien thi hoan vi

#include "conio.h"

Trang 6

#include "stdio.h"

#include "stdlib.h"

int A[]={1,2,3};

int kq[]={0,0};

int n;

void Inkq()

{

int i;

printf(" kq:");

for(i=0;i<3;i++)

printf(" %d", kq[i]);

printf(" \n");

}

int UCV(int ai, int kq[], int k, int n)

{

int i;

for(i=0;i<k-1; i++)

{

if(kq[i]==ai) return 0;

}

return 1;

}

void hienthihv(int A[], int kq[], int k, int n) {

int i;

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

{

if(UCV(A[i],kq,k,n)==1)

{

kq[k-1] = A[i];

if(k==2) Inkq();

else hienthihv(A, kq, k+1, n); }

}

}

int main()

{

n=3;

hienthihv(A,kq,1,n);

getch();

Trang 7

return 1;

}

Buoi hoc 5p2: Quay lui Bai toan Xep Hau

#include "conio.h"

#include "stdio.h"

#include "stdlib.h"

int kq[]={0,0,0,0};

int n;

void Inkq()

{

int i;

printf(" kq:");

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

printf(" %d", kq[i]);

printf(" \n");

}

int UCV(int ai, int kq[], int k, int n)

{

int i;

for(i=0;i<k-1; i++)

{

if(kq[i]==ai) return 0;

if(abs(kq[i]-ai) == abs(k-i-1)) return 0;

}

return 1;

}

void dathau(int kq[], int k, int n)

{

int i;

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

{

if(UCV(i+1,kq,k,n)==1)

{

kq[k-1] = i+1;

if(k==n) Inkq();

Trang 8

else dathau(kq, k+1, n);

}

}

}

int main()

{

/*

Mot ket qua tim thay duoc: 2, 4, 1, 3

o o x o

x o o o

o o o x

o x o o

*/

n=4;

dathau(kq,1,n);

getch();

return 1;

}

Buoi hoc 6: Bieu dien danh sach tuyen tinh bang mang

#include "conio.h"

#include "stdio.h"

#include "stdlib.h"

# define maxlength 1000

typedef int elementtype; /* elements are integers */

typedef struct list_tag

{ elementtype elements[maxlength];

int last;

} list_type;

int end (list_type *lp)

{

return (lp->last +1);

}

void insert (elementtype x , int p , list_type * lp)

{ //int v; // running position

Trang 9

if (lp -> last >= maxlength - 1)

{

printf("\n%s","list is full");

return;

}

if ((p < 0) || (p > lp -> last + 1))

{

printf("\n%s ","position does not exist"); return;

}

else {

for (int q = lp -> last; q >= p; q )

lp -> elements [q+1] = lp -> elements [q];

lp -> last = lp -> last +1 ;

lp -> elements[p] = x;

}

}

void deleteL(int p , list_type * lp)

{

int q; /* running position */

if ((p > lp-> last) || (p < 0))

{

printf("\n%s ","position does not exist");

return;

}

else /* shift elements */ {

lp -> last ;

for (int q = p; q <= lp ->last; q++)

lp -> elements [q] = lp -> elements [q+1];

}

}

void inptutrongds(list_type * lp)

{

int i;

printf("\ncac ptu in ra:\n");

for(i=0;i<lp->last;i++)

printf("%d\n",lp->elements[i]);

}

int main()

{

list_type *danhsach1;

danhsach1 = (list_type*)malloc(sizeof(list_type));

Trang 10

insert(100, 0, danhsach1);

insert(-20, 1, danhsach1);

insert(-30, 1, danhsach1);

insert(-40, 1, danhsach1);

deleteL(2, danhsach1);

inptutrongds(danhsach1);

}

Buoi hoc 7: Danh sach moc noi

#include "conio.h"

#include "stdio.h"

#include "stdlib.h"

typedef int ElementType;

struct NodeType{

ElementType Inf;

NodeType *Next;

};

typedef struct NodeType LIST;

void duyet(LIST *lp)

{

LIST *tg;

tg = lp;

printf(" \n Ds dang duyet: ");

while(tg != NULL)

{

printf(" %d ", tg->Inf);

tg = tg->Next;

}

}

void InsertToLast(LIST *lp, ElementType X)

{

if(lp==NULL) return;

LIST *nutx;

nutx = (LIST*)malloc(sizeof(LIST));

nutx->Inf = X;

nutx->Next = NULL;

Trang 11

LIST *tg;

tg = lp;

while(tg->Next != NULL) tg = tg->Next;

tg->Next = nutx;

}

LIST *InsertToHead(LIST *lp, ElementType X)

{

LIST *nutx;

nutx = (LIST*)malloc(sizeof(LIST));

nutx->Inf = X;

nutx->Next = lp;

lp = nutx;

return lp;

}

LIST *Insert_Middle(NodeType *Pred, ElementType X)

{

LIST *TempNode;

TempNode = (NodeType *)malloc(sizeof(NodeType)); //(1) TempNode->Inf=X; //(1)

TempNode->Next=Pred->Next; //(2)

Pred->Next=TempNode; //(3)

return TempNode;

}

LIST *DeleteAtHead(LIST *lp)

{

LIST *tempprt;

if(lp==NULL) return NULL;

tempprt = lp->Next;

free(lp);

//lp = tempprt;

//return lp;

return tempprt;

}

void DeleteAtLast(LIST *lp)

{

LIST *tg, *tg_old;

Trang 12

if((lp==NULL)||(lp->Next==NULL)) return;

tg = lp;

tg_old;

while(tg->Next != NULL)

{

tg_old = tg;

tg = tg->Next;

}

free(tg);

tg_old->Next=NULL;

}

int main()

{

LIST *danhsach3=NULL;

danhsach3 = InsertToHead(danhsach3, 1);

InsertToLast(danhsach3, 3);

Insert_Middle(danhsach3, 2);

danhsach3=DeleteAtHead(danhsach3);

//printf("\n danh sach khoi tao ban dau \n");

//duyet(danhsach3);

//danhsach3 = DeleteAtHead(danhsach3);

//printf("\n danh sach vua xoa nut dau \n");

//DeleteAtLast(danhsach3);

//printf("\n danh sach vua xoa nut cuoi \n");

duyet(danhsach3);

getch();

return 1;

}

Buoi hoc 8: Stack (ktra ngoac dung)

#include "conio.h"

#include "stdio.h"

#include "stdlib.h"

/* Cai dat ngan xep su dung ds noi don */

typedef char eletype;

struct StackNode

Trang 13

eletype item;

StackNode *next;

};

typedef struct Stack

{

StackNode *top;

}Stack;

Stack *StackConstruct()

{

Stack *s;

s = (Stack*)malloc(sizeof(Stack));

if (s == NULL)

{

return NULL; // No memory }

s->top = NULL;

return s;

}

/*** Kiem tra Stack rong ***/

int StackEmpty(const Stack *s)

{

return (s->top == NULL);

}

/*** Thông báo Stack tràn ***/

int StackFull()

{

printf("\n NO MEMORY! STACK IS FULL");

getch();

return 1;

}

int StackPush(Stack *s, eletype item)

{

StackNode *node;

node = (StackNode*)malloc(sizeof(StackNode)); //(1)

if (node == NULL)

{

StackFull();

return 1; // Tràn Stack: het bo nho }

Trang 14

node->item = item; //(1)

node->next = s->top; //(2)

s->top = node; //(3)

return 0;

}

eletype StackPop(Stack *s)

{

eletype item;

StackNode *node;

if (StackEmpty(s))

// Empty Stack, can't pop return 0;

}

node = s->top; //(2)

item = node->item; //(3)

s->top = node->next; //(4)

free(node); //(5)

return item; //(6)

}

/* bai toan ngoac dung */

int ktngmo(char in)

{

if((in=='{')||(in=='[')||(in=='('))

return 1;

else return 0;

}

int ktracungkieu(char a, char b)

{

if(a=='{')

if(b=='}') return 1;

else return 0;

if(a=='[')

if(b==']') return 1;

else return 0;

if(a=='(')

if(b==')') return 1;

else return 0;

}

int kiemtrangoacdung(Stack *s, char in[], int n)

Trang 15

int i;

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

{

if(ktngmo(in[i])==1)

{

// dua vao stack

StackPush(s, in[i]);

}

else

{//ngoac dong if(StackEmpty(s)!=0) return 0; else

{

char tempchar = StackPop(s); if(ktracungkieu(tempchar,in[i])==0) return 0;

}

}

}

if(StackEmpty(s)!=0) return 1;

else return 0;

}

int main()

{

Stack *s = StackConstruct();

char A[]={'{','[',']','}','(',')'};

int i = kiemtrangoacdung(s, A, 6);

if(i==1) printf("\nBieu thuc dung");

else

printf("\nBieu thuc sai");

getch();

return 1;

}

Buoi hoc 9: Queue

#include "conio.h"

#include "stdio.h"

#include "stdlib.h"

Trang 16

/* Cai dat hang doi su dung ds noi don */

typedef int eletype;

struct QueueNode {

eletype item;

QueueNode *Next;

};

typedef struct _Queue

{

QueueNode *front, *back;

} Queue;

Queue *QueueConstruct()

{

Queue *q;

q = (Queue *)malloc(sizeof(Queue));

if (q == NULL) {

return NULL; // No memory

}

q->front = NULL;

q->back = NULL;

return q;

}

/*** Ki?m tra Stack r?ng ***/

int QueueEmpty(const Queue *q)

{

return (q->front == NULL);

}

/*** Thông báo Stack tràn ***/

int QueueFull()

{

printf("\n NO MEMORY! STACK IS FULL");

getch();

return 1;

}

int EnQueue(Queue *q, eletype item)

{

QueueNode *node;

node = (QueueNode*)malloc(sizeof(QueueNode)); //(1)

Trang 17

if (node == NULL)

{

QueueFull(); return 1; // Tràn Stack: h?t b? nh? }

node->item = item;

node->Next = NULL;

if((q->front==NULL)&&(q->back==NULL))

{

q->front = node;

q->back = node;

}

else

{

q->back->Next = node;

q->back = node;

}

return 0;

}

eletype DeQueue(Queue *q)

{

eletype item;

QueueNode *node;

if (QueueEmpty(q))

{ //(1)

// Empty Queue, can't dequeue

return 0;

}

node = q->front;

item = node->item;

if((q->front==q->back)&&(q->front!=NULL))

{

q->front = NULL;

q->back = NULL;

free(node);

}

else

{

q->front=node->Next;

free(node);

Trang 18

}

return item;

}

int main()

{

Queue *q = QueueConstruct();

EnQueue(q,2);

EnQueue(q,3);

EnQueue(q,4);

EnQueue(q,3);

while(!QueueEmpty(q))

printf(" ket qua dequeue la %d\n",DeQueue(q));

getch();

return 1;

}

Ngày đăng: 04/11/2015, 15:45

TỪ KHÓA LIÊN QUAN

🧩 Sản phẩm bạn có thể quan tâm

w