#include <stdio.h> //標準輸出入函數的標頭檔 #include <stdlib.h> void Travel(int* Tree, int curNode) { if (Tree[curNode]==-1) return; Travel(Tree, curNode * 2);//走訪左子樹 printf("%3d ", Tree[curNode]);//走訪樹根 Travel(Tree, curNode * 2 + 1);//走訪右子樹 } int main(int argc, char *argv[])//主函數入口 int Level = 12; int Nodes = 8192; int Tree[Nodes]; int i, j, data, curNode, curLevel; srand( (unsigned) time( NULL ) );//srand()是產生亂數的種子 //讓每個節點啟始值是-1表示是空的 for (i=0; i< Nodes; i++) Tree[i] = -1; //插入20個資料項到這個樹 for (i=0; i< 20; i++) { data = rand()%100; curNode = 1;//根節點是編號是1 curLevel = 1;//目前階層 while (curLevel <= Level) { if (Tree[curNode] == -1)//目前節點是空的 { Tree[curNode] = data; break; } else if (data < Tree[curNode]) curNode = 2 * curNode; else //if (data >= Tree[curNode]) curNode = 2 * curNode +1; curLevel++; } } //使用中序列印出整個二元搜尋樹 Travel(Tree, 1); return 0; }
Posts Tagged ‘103(暑)’
#include <stdio.h> //標準輸出入函數的標頭檔 #include <stdlib.h> typedef struct node//節點結構的自定型別定義 { int data;//整數 struct node *left;//節點的指標 struct node *right;//節點的指標 } Node; Node* NewNode() { Node *newNode = malloc(sizeof(Node));//新節點(node) newNode->data = 0; newNode->left = NULL; newNode->right = NULL; return newNode; } void Travel(Node* curNode) { if (curNode==NULL) return; Travel(curNode->left);//走訪左子樹 printf("%3d ", curNode->data);//走訪樹根 Travel(curNode->right);//走訪右子樹 } int main(int argc, char *argv[])// {//程式寫在main()函數裡 Node* Tree = NewNode(); Node* curNode = NULL; int i, newdata; srand( (unsigned) time( NULL ) );//srand()是產生亂數的種子 //插入20個資料項到這個樹 newdata = rand()%100; Tree->data = newdata; for (i=1; i< 20; i++) { newdata = rand()%100; curNode = Tree while (1) { if (newdata < curNode->data) { if (curNode->left == NULL) { curNode->left = NewNode(); curNode->left->data = newdata; break; } else curNode = curNode->left; } else { if (curNode->right == NULL) { curNode->right = NewNode(); curNode->right->data = newdata; break; } else curNode = curNode->right; } } } //使用中序列印出整個二元搜尋樹 Travel(Tree); return 0;
29
八月
資料結構程式 08/29: ex9.c
Node* push(Node *stack, int indata) { Node *newNode = malloc(sizeof(Node))//新節點(node) newNode->data = indata; newNode->next = stack; return newNode; } Node* pop(Node *stack) { Node *oldnode = stack; stack = stack->next; free(oldnode); return stack; } int main() { Node *stack = NULL; int i; srand( (unsigned) time( NULL ) )//srand()是產生亂數的種子 //在這個堆疊上push資料 for (i=1; i<5; i++) stack = push(stack, rand()%100); PrintList(stack); } //在這個堆疊上pop資料 for (i=1; i<5; i++) stack = pop(stack); PrintList(stack); } }
27
八月
資料結構程式 08/27: ex8.c
void FreeList(Node *listA) { Node *curNode = listA; Node *nextNode; while ( curNode !=NULL)//從第一個節點把節點釋放 { nextNode = curNode->next; free(curNode); curNode = nextNode; } } main() { Node *prevNode;//->操作刪除時要記住前一個的指標 Node *delNode;//->操作刪除的節點 //在這個串列刪除節點 if (listA!=NULL && listA->data >=30 && listA->data <=60) listA = listA->next; if (listA!=NULL) { prevNode = listA; curNode = listA->next; while (curNode!=NULL) { delNode = NULL; if (curNode->data >=30 && curNode->data <=60) { prevNode->next = curNode->next; delNode = curNode; } else { prevNode = curNode; } curNode = curNode->next; if (delNode!=NULL) free(delNode); } } PrintList(listA); FreeList(listA); // 釋放節點 }
25
八月
資料結構程式 08/25: ex7.c
Node* NewNode() { Node *newNode = malloc(sizeof(Node));//新節點(node) newNode->data = rand()%100; newNode->next = NULL; return newNode; } void PrintList(Node *listA) { Node *curNode = listA; printf("Print the nodes:"); while ( curNode !=NULL)//從第一個節點把資料印出來 { printf("%2d ",curNode->data); curNode = curNode->next; } printf("\n"); } int main(int argc, char *argv[])//主函數入口 { Node *listA = NULL;//->永遠指向第一個的指標 Node *curNode = NULL;//->操作(插入或刪除)第N個資料的指標 Node *newNode;//->操作(插入或刪除)第N個資料的指標 int i; srand( (unsigned) time( NULL ) );//srand()是產生亂數的種子 listA = NewNode(); PrintList(listA); //在這個串列附加上新的資料節點 curNode = listA; for (i=1; i<20; i++) { newNode = NewNode(); if ( newNode->data < listA->data) { newNode->next = listA; listA = newNode; } else { curNode = listA; while (curNode->next!=NULL && curNode->next->data < newNode->data) curNode = curNode->next; newNode->next = curNode->next; curNode->next = newNode; } PrintList(listA); } return 0; }