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

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

Đ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

Buoi hoc 3: De quy thap 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 = f(n) = f(n-1)*x , n > */ 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; } } /* thap noi */ void thaphanoi(int n, char a, char b, char c) { if (n==1) printf("chuyen 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 mang n ptu >=0 */ int timmin(int A[], int n) { if(n==1) return A[0]; else { if(timmin(A,n-1) */ int hamf(int n) { /* in */ printf("Bat dau goi ham voi dau vao la %d\n",n); if(n==0) { printf("tra ve gia tri 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 n=%d\n",k,n); return k; } } int main() { int x = hamf(30); getch(); return 1; } Buoi hoc 4: De quy co nho #include "conio.h" #include "stdio.h" #include "stdlib.h" /* Tinh Fibo f(n) = n voi n 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 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;ilast;i++) printf("%d\n",lp->elements[i]); } int main() { list_type *danhsach1; danhsach1 = (list_type*)malloc(sizeof(list_type)); 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; 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; 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 { 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 } 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) //(1) { int i; for(i=0; ifront = 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) 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); } 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

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan