else if i1 printf scanfd value pushvalue else

  • Slides: 102
Download presentation

 else if (i==1) { printf("請輸入元素值: "); scanf("%d", &value); push(value); } else if(i==0) printf("彈出的元素為%dn",

else if (i==1) { printf("請輸入元素值: "); scanf("%d", &value); push(value); } else if(i==0) printf("彈出的元素為%dn", pop()); } while(i!=-1); printf("==============n"); while(!is. Empty()) /*將資料陸續從頂端彈出*/ printf("堆疊彈出的順序為: %dn", pop()); printf("=============n"); system("pause"); return 0; } int is. Empty() { if(top==NULL) return 1; else return 0; } /*將指定的資料存入堆疊*/ void push(int data) { Linked_Stack new_add_node; /*新加入節點的指標*/ 4 -1 堆疊簡介 17

#include <stdio. h> #include <stdlib. h> #include <string. h>/* 包含字串處理函數 */ int push(char*, int);

#include <stdio. h> #include <stdlib. h> #include <string. h>/* 包含字串處理函數 */ int push(char*, int); /* 置入堆疊資料 */ int pop(); /* 取出堆疊資料 */ int show(); /* 顯示堆疊資料 */ struct student { char name[20]; int score; struct student *next; }; typedef struct student s_data; s_data *top = NULL; int main() { struct student int select, score; { char name[20]; do char name[20]; { int score; printf("(1)存入 (2)取出 (3)走訪 (4)離開 => "); scanf("%d", &select); struct student *next; switch (select) { }; case 1: printf("姓名 成績:"); 4 -1 堆疊簡介 • 範例 4. 1. 3 – 請設計一C程式,使用malloc()函數來配置串列 堆疊中新元素的記憶體空間,本題中元素為學 生姓名及成績的結構資料,並進行堆疊資料的 存入、取出與走訪動作。元素結構如下: 19

scanf("%s %d", name, &score); push(name, score); break; case 2: pop(); break; case 3: show();

scanf("%s %d", name, &score); push(name, score); break; case 2: pop(); break; case 3: show(); break; } } while (select != 4); system("pause"); return 0; } int push(char* name, int score)/* 存入新元素 */ { s_data *new_data; new_data = (s_data*) malloc(sizeof(s_data)); /* 配置空間給新元素 */ strcpy(new_data->name, name); /* 設定新元素的資料內容 */ new_data->score = score; new_data->next = top; /* 將新元素的next指向原堆疊頂端 */ top = new_data; /* 設定新元素為堆疊頂端 */ } int pop() /* 取出新元素 */ { s_data *freeme; 4 -1 堆疊簡介 20

{ Node<Type>* new. Node = new Node<Type>; // 新增一個節點 new. Node->data = data; //

{ Node<Type>* new. Node = new Node<Type>; // 新增一個節點 new. Node->data = data; // 紀錄資料 new. Node->next = first; // 指到前一個節點 first = new. Node; // 指到新的節點 } template<class Type> void Linked. List<Type>: : display() { Node<Type>* current. Node = first; // 由第一個節點開始顯示 while( current. Node != NULL ) { cout << current. Node->data << " -> "; current. Node = current. Node->next; } } int main() { Linked. List<double> dbl. List; // 建立一個儲存double型態資料的鏈結串列 double num; // 紀錄輸入的資料 char ch; // 紀錄使用者的選擇 do{ cout << endl <<"請輸入一個數字 : "; cin >> num; 4 -1 堆疊簡介 24

 dbl. List. add. Node( num ); cout << "繼續輸入(y / n)? "; cin

dbl. List. add. Node( num ); cout << "繼續輸入(y / n)? "; cin >> ch; }while( ch != 'n' ); cout << endl; dbl. List. display(); cout << endl; system("pause"); return 0; } 4 -1 堆疊簡介 // 顯示所有的資料 25

Type Stack<Type, size> : : pop() { return st[ top-- ]; } int main()

Type Stack<Type, size> : : pop() { return st[ top-- ]; } int main() { Stack<> stk_1; // 宣告一堆疊物件, 並使用其預設值 Stack<char*, 4> stk_2; // 宣告堆疊物件, 其型態為字串, 大小為 4 stk_1. push( 11 ); stk_1. push( 22 ); stk_1. push( 33 ); cout << "stack_1 [1] = " << stk_1. pop() << endl; cout << "stack_1 [2] = " << stk_1. pop() << endl; cout << "stack_1 [3] = " << stk_1. pop() << endl; cout << endl; stk_2. push( "第一名" ); stk_2. push( "第二名" ); stk_2. push( "第三名" ); cout << "stack_2 [1] = " << stk_2. pop() << endl; cout << "stack_2 [2] = " << stk_2. pop() << endl; cout << "stack_2 [3] = " << stk_2. pop() << endl; cout << endl; system("pause"); return 0; } 4 -1 堆疊簡介 27

link push(link stack, int x, int y) { link newnode; newnode = (link)malloc(sizeof(node)); if(!newnode)

link push(link stack, int x, int y) { link newnode; newnode = (link)malloc(sizeof(node)); if(!newnode) { printf("Error!記憶體配置失敗!n"); return NULL; } newnode->x=x; newnode->y=y; newnode->next=stack; stack=newnode; return stack; } link pop(link stack, int* x, int* y) { link top; if(stack!=NULL) { top=stack; stack=stack->next; *x=top->x; *y=top->y; free(top); 4 -1 堆疊簡介 34

} else 4 -1 堆疊簡介 *x=-1; return stack; } int chk. Exit(int x, int

} else 4 -1 堆疊簡介 *x=-1; return stack; } int chk. Exit(int x, int y, int ex, int ey) { if(x==ex&&y==ey) { if(NORTH==1||SOUTH==1||WEST==1||EAST==2) return 1; if(NORTH==1||SOUTH==1||WEST==2||EAST==1) return 1; if(NORTH==1||SOUTH==2||WEST==1||EAST==1) return 1; if(NORTH==2||SOUTH==1||WEST==1||EAST==1) return 1; } return 0; } int main() { int i, j, x, y; link path = NULL; 35

x=1; /*入口的X座標*/ y=1; /*入口的Y座標*/ printf("[迷宮的地模擬圖(0表示牆, 2表示入口, 3表示出口]n"); /*印出迷宮的路徑圖*/ for(i=0; i<10; i++) { for(j=0; j<12;

x=1; /*入口的X座標*/ y=1; /*入口的Y座標*/ printf("[迷宮的地模擬圖(0表示牆, 2表示入口, 3表示出口]n"); /*印出迷宮的路徑圖*/ for(i=0; i<10; i++) { for(j=0; j<12; j++) printf("%2 d", MAZE[i][j]); printf("n"); } while(x<=Exit. X&&y<=Exit. Y) { MAZE[x][y]=6; if(NORTH==0) { x -= 1; path=push(path, x, y); } else if(SOUTH==0) { x+=1; path=push(path, x, y); } else if(WEST==0) { y-=1; 4 -1 堆疊簡介 36

path=push(path, x, y); } else if(EAST==0) { y+=1; path=push(path, x, y); } else if(chk.

path=push(path, x, y); } else if(EAST==0) { y+=1; path=push(path, x, y); } else if(chk. Exit(x, y, Exit. X, Exit. Y)==1) /*檢查是否走到出口了*/ break; else { MAZE[x][y]=2; path=pop(path, &x, &y); } } printf("--------------n"); printf("[6表示老鼠走過的路線]n"); /*印出老鼠走完迷宮後的路徑圖*/ printf("--------------n"); for(i=0; i<10; i++) { for(j=0; j<12; j++) printf("%2 d", MAZE[i][j]); printf("n"); } system("pause"); return 0; 4 -1 堆疊簡介 37

int compare(char stack_o, char infix_o) { /*在中序表示法佇列及暫存堆疊中,運算子的優先順序表,*/ /*其優先權值為INDEX/2 */ char infix_priority[9] ; char stack_priority[8]

int compare(char stack_o, char infix_o) { /*在中序表示法佇列及暫存堆疊中,運算子的優先順序表,*/ /*其優先權值為INDEX/2 */ char infix_priority[9] ; char stack_priority[8] ; int index_s=0, index_i=0; infix_priority[0]='q'; infix_priority[1]=')'; infix_priority[2]='+'; infix_priority[3]='-'; infix_priority[4]='*'; infix_priority[5]='/'; infix_priority[6]='^'; infix_priority[7]=' '; infix_priority[8]='('; stack_priority[0]='q'; stack_priority[1]='('; stack_priority[2]='+'; stack_priority[3]='-'; stack_priority[4]='*'; stack_priority[5]='/'; stack_priority[6]='^'; stack_priority[7]=' '; while (stack_priority[index_s] != stack_o) index_s++; while (infix_priority[index_i] != infix_o) index_i++; return ((int)(index_s/2) >= (int)(index_i/2) ? 1 : 0); } void infix_to_postfix() { int rear=0, top=0, flag=0, i=0; 4 -2 算術運算式的表示法 52

char stack_t[MAX]; for (i=0; i<MAX; i++) 4 -2 算術運算式的表示法 stack_t[i]='�'; gets(infix_q); i=0; while(infix_q[i]!='�') {

char stack_t[MAX]; for (i=0; i<MAX; i++) 4 -2 算術運算式的表示法 stack_t[i]=''; gets(infix_q); i=0; while(infix_q[i]!='') { i++; rear++; } infix_q[rear] = 'q'; printf("t後序表示法 : "); stack_t[top] = 'q'; for (flag = 0; flag <= rear; flag++) { switch (infix_q[flag]) { /*輸入為),則輸出堆疊內運算子,直到堆疊內為(*/ case ')': while(stack_t[top]!='(') printf("%c", stack_t[top--]); top--; break; /*輸入為q,則將堆疊內還未輸出的運算子輸出*/ 53

4 -3 佇列 void FRONT_VALUE(int *Queue) /*傳回佇列前端的值*/ ﹛ if (front==rear) printf(“%s”, " 這是空佇列"); else

4 -3 佇列 void FRONT_VALUE(int *Queue) /*傳回佇列前端的值*/ ﹛ if (front==rear) printf(“%s”, " 這是空佇列"); else printf("%s", queue[front]); } /* 傳回佇列前端的值 */ 71

#include <stdio. h> #include <stdlib. h> #include <conio. h> #define MAX 10 /*定義佇列的大小*/ int

#include <stdio. h> #include <stdlib. h> #include <conio. h> #define MAX 10 /*定義佇列的大小*/ int main() { int front, rear, val, queue[MAX]={0}; char choice; front=rear=-1; while(rear<MAX-1 && choice!='e') { printf("[a]表示存入一個數值[d]表示取出一個數值[e]表示跳出此程式: "); choice=getche(); switch(choice) { case 'a': printf("n[請輸入數值]: "); scanf("%d", &val); rear++; queue[rear]=val; break; case 'd': if(rear>front) { front++; 4 -3 佇列 • 範例 4. 3. 1 – 請設計一C程式,來實作佇列的 作運算,加入 資料時請輸入"a",要取出資料時可輸入"d",將 會直接印出佇列前端的值,要結束請按"e"。 72

printf("n[取出數值為]: [%d]n", queue[front]); queue[front]=0; 4 -3 佇列 } else { printf("n[佇列已經空了]n"); exit(0); } break;

printf("n[取出數值為]: [%d]n", queue[front]); queue[front]=0; 4 -3 佇列 } else { printf("n[佇列已經空了]n"); exit(0); } break; default: printf("n"); break; } } printf("n---------------------n"); printf("[輸出佇列中的所有元素]: "); if(rear==MAX-1) printf("[佇列已滿]n"); else if (front>=rear) { printf("沒有n"); printf("[佇列已空]n"); } else 73

4 -3 佇列 { while (rear>front) { front++; printf("[%d] ", queue[front]); } printf("n"); printf("---------------------n");

4 -3 佇列 { while (rear>front) { front++; printf("[%d] ", queue[front]); } printf("n"); printf("---------------------n"); } printf("n"); system("pause"); return 0; } 74

#include <stdio. h> int main(void) { int front, rear, val, queue[5]={0}; front=rear=-1; while(rear<5&&val!=-1) {

#include <stdio. h> int main(void) { int front, rear, val, queue[5]={0}; front=rear=-1; while(rear<5&&val!=-1) { printf("請輸入一個值以存入佇列,欲取出值請輸入 0。(結束輸入-1):"); scanf("%d", &val); if(val==0) { if(front==rear) { printf("[佇列已經空了]n"); break; } front++; if (front==5) front=0; printf("取出佇列值 [%d]n", queue[front]); queue[front]=0; } else if(val!=-1&&rear<5) { if(rear+1==front||rear==4&&front<=0) 4 -3 佇列 • 範例 4. 3. 2 – 請設計一C程式,來實作環形佇列的 作運算, 當要取出資料時可輸入"0",要結束時可輸入"-1"。 80

{ 4 -3 佇列 printf("[佇列已經滿了]n"); break; } rear++; if(rear==5) rear=0; queue[rear]=val; } } printf("n佇列剩餘資料:n");

{ 4 -3 佇列 printf("[佇列已經滿了]n"); break; } rear++; if(rear==5) rear=0; queue[rear]=val; } } printf("n佇列剩餘資料:n"); if (front==rear) printf("佇列已空!!n"); else { while(front!=rear) { front++; if (front==5) front=0; printf("[%d]", queue[front]); queue[front]=0; } } printf("n"); system("pause"); return 0; } 81

4 -3 佇列 /* 環狀佇列的加入修正演算法 */ viod Add. Q (int item) { rear=(rear+1)%MAX_SIZE; if(front==rear

4 -3 佇列 /* 環狀佇列的加入修正演算法 */ viod Add. Q (int item) { rear=(rear+1)%MAX_SIZE; if(front==rear && TAG==1) printf(“%s”, "佇列已滿! "); else queue[rear]=item; if(front==rear) TAG=1; } /* 環形佇列的刪除修正演算法 */ void dequeue(int item) { if(rear==front && TAG==0) printf(“%s”, "佇列是空的!"); else { front=(front+1)%MAX_SIZE; item=Queue[front]; if (front==rear) TAG=0; } } 83

#include <stdio. h> #include <stdlib. h> #include <string. h> int enqueue(char*, int); /* 置入佇列資料

#include <stdio. h> #include <stdlib. h> #include <string. h> int enqueue(char*, int); /* 置入佇列資料 */ int dequeue(); /* 取出佇列資料 */ int show(); /* 顯示佇列資料 */ struct student { char name[20]; int score; struct student *next; }; typedef struct student s_data; 4 -3 佇列 • 範例 4. 3. 3 – 請利用串列結構來設計一C程式,串列中元素節 點仍為學生姓名及成績的結構資料。 – 本程式還能進行佇列資料的存入、取出與走訪 動作: s_data *front =NULL; struct student s_data *rear = NULL; { int main() char name[20]; { int score; int select, score; struct student *next; char name[20]; do }; { typedef struct student s_data; printf("(1)存入 (2)取出 (3)顯示 (4)離開 => "); scanf("%d", &select); 87

 switch (select) { case 1: printf("姓名 成績:"); scanf("%s %d", name, &score); enqueue(name, score);

switch (select) { case 1: printf("姓名 成績:"); scanf("%s %d", name, &score); enqueue(name, score); break; case 2: dequeue(); break; case 3: show(); break; } } while (select != 4); system("pause"); return 0; } int enqueue(char* name, int score) { s_data *new_data; new_data = (s_data*) malloc(sizeof(s_data)); /* 配置記憶體給新元素 */ strcpy(new_data->name, name); /* 設定新元素的資料 */ new_data->score = score; 4 -3 佇列 88

 if (ptr == NULL) puts("佇列已空!"); else { puts("front -> rear"); while (ptr !=

if (ptr == NULL) puts("佇列已空!"); else { puts("front -> rear"); while (ptr != NULL) /* 由front往rear走訪佇列 */ { printf("姓名:%st成績:%dn", ptr->name, ptr->score); ptr = ptr->next; } } } 4 -3 佇列 90

4 -3 佇列 • C/C++的節點宣告、加入與刪除運算法 struct Node { int data; struct Node *next; };

4 -3 佇列 • C/C++的節點宣告、加入與刪除運算法 struct Node { int data; struct Node *next; }; typedef struct Node Queue. Node; typedef Queue. Node *Queue. By. Linked. List; Queue. By. Linked. List front=NULL; Queue. By. Linked. List rear=NULL; 92

4 -3 佇列 void enqueue(int value) /*方法enqueue: 佇列資料的加入*/ { Queue. By. Linked. List node;

4 -3 佇列 void enqueue(int value) /*方法enqueue: 佇列資料的加入*/ { Queue. By. Linked. List node; /*建立節點*/ node=(Queue. By. Linked. List)malloc(sizeof(Queue. Node)); node->data=value; node->next=NULL; /*檢查是否為空佇列*/ if (rear==NULL) front=node; /*新建立的節點成為第 1個節點*/ else rear->next=node; /*將節點加入到佇列的尾端*/ rear=node; /*將佇列的尾端指標指向新加入的節點*/ } 93

4 -3 佇列 int dequeue(int action)/*方法dequeue: 佇列資料的取出*/ { int value; Queue. By. Linked. List

4 -3 佇列 int dequeue(int action)/*方法dequeue: 佇列資料的取出*/ { int value; Queue. By. Linked. List temp. Node, start. Node; /*從前端取出資料*/ if (!(front==NULL) && action==1) { if(front==rear) rear=NULL; value=front->data; /*將佇列資料從前端取出*/ front=front->next; /*將佇列的前端指標指向下一個*/ return value; } /*從尾端取出資料*/ else if(!(rear==NULL) && action==2) { start. Node=front; /*先記下前端的指標值*/ value=rear->data; /*取出目前尾端的資料*/ /*找尋最尾端節點的前一個節點*/ temp. Node=front; while (front->next!=rear && front->next!=NULL) 94

4 -3 佇列 { front=front->next; temp. Node=front; } front=start. Node; /*記錄從尾端取出資料後的佇列前端指標*/ rear=temp. Node; /*記錄從尾端取出資料後的佇列尾端指標*/

4 -3 佇列 { front=front->next; temp. Node=front; } front=start. Node; /*記錄從尾端取出資料後的佇列前端指標*/ rear=temp. Node; /*記錄從尾端取出資料後的佇列尾端指標*/ /*下一行程式是指當佇列中僅剩下最節點時, 取出資料後便將front及rear指向null*/ if ((front->next==NULL) || (rear->next==NULL)) { front=NULL; rear=NULL; } return value; } else return -1; } 95

#include <stdio. h> #include <stdlib. h> struct Node { int data; struct Node *next;

#include <stdio. h> #include <stdlib. h> struct Node { int data; struct Node *next; }; typedef struct Node Queue. Node; typedef Queue. Node *Queue. By. Linked. List; Queue. By. Linked. List front=NULL; Queue. By. Linked. List rear=NULL; /*方法enqueue: 佇列資料的存入*/ void enqueue(int value) { Queue. By. Linked. List node; /*建立節點*/ node=(Queue. By. Linked. List)malloc(sizeof(Queue. Node)); node->data=value; node->next=NULL; /*檢查是否為空佇列*/ if (rear==NULL) front=node; /*新建立的節點成為第 1個節點*/ else rear->next=node; /*將節點加入到佇列的尾端*/ rear=node; /*將佇列的尾端指標指向新加入的節點*/ 4 -3 佇列 • 範例 4. 3. 4 – 請利用串列結構來設計一輸入限制的雙向佇列C 程式,我們只能從一端加入資料,但取出資料 時,將分別由前後端取出。 96

} int dequeue(int action)/*方法dequeue: 佇列資料的取出*/ { int value; Queue. By. Linked. List temp. Node,

} int dequeue(int action)/*方法dequeue: 佇列資料的取出*/ { int value; Queue. By. Linked. List temp. Node, start. Node; /*從前端取出資料*/ if (!(front==NULL) && action==1) { if(front==rear) rear=NULL; value=front->data; /*將佇列資料從前端取出*/ front=front->next; /*將佇列的前端指標指向下一個*/ return value; } /*從尾端取出資料*/ else if(!(rear==NULL) && action==2) { start. Node=front; /*先記下前端的指標值*/ value=rear->data; /*取出目前尾端的資料*/ /*找尋最尾端節點的前一個節點*/ temp. Node=front; while (front->next!=rear && front->next!=NULL) { front=front->next; temp. Node=front; 4 -3 佇列 97

printf("n"); if(ch=='a') { printf("加入的元素值: "); scanf("%d", &item); enqueue(item); } else if(ch=='d') { temp=dequeue(1); printf("從雙向佇列前端依序取出的元素資料值為:%dn",

printf("n"); if(ch=='a') { printf("加入的元素值: "); scanf("%d", &item); enqueue(item); } else if(ch=='d') { temp=dequeue(1); printf("從雙向佇列前端依序取出的元素資料值為:%dn", temp); temp=dequeue(2); printf("從雙向佇列尾端依序取出的元素資料值為:%dn", temp); } else break; } while(ch!='e'); system("pause"); return 0; } 4 -3 佇列 99