3C科技 娛樂遊戲 美食旅遊 時尚美妝 親子育兒 生活休閒 金融理財 健康運動 寰宇綜合

Zi 字媒體

2017-07-25T20:27:27+00:00
加入好友
[C/C++ 演算法]-資料結構與演算法(文魁):堆疊[Stack]使用範例(先進後出)   線上執行結果:http://www.tutorialspoint.com/compile_c_online.php code2html:http://tohtml.com/   /* =============== Program Description =============== */ /* 程式名稱 : 7_2.cpp */ /* 程式目的 : 堆疊使用範例(先進後出) */ /* 輸 入 : 整數陣列資料 */ /* 輸 出 : 反轉的整數陣列資料 */ /* =================================================== */ #define n 5 // 宣告標頭檔 #include "stdio.h" // 宣告全域變數 int Stack[n+1]; int top=-1; // 宣告函式原型 // 從pop函式中回傳一個整數 int pop(void); // 利用傳值的方式把整數ch傳進push函式中 void push(int ch); void main(void) { int i; int A[n]={1,3,5,7,9}; // 反轉前的資料 printf(" Data before inversing : "); for(i=0;i<n;i++) printf("%d",A[i]); printf("\n"); // 依序放入堆疊 for(i=0;i<n;i++) push(A[i]); // 依序從堆疊取出 for(i=0;i<n;i++) A[i]=pop(); // 反轉後的資料 printf(" Data after inversing : "); for(i=0;i<n;i++) printf("%d",A[i]); printf("\n"); getchar(); } int pop(void) { return Stack[top--]; } void push(int ch) { Stack[++top]=ch; }  

本文由jashliaoeuwordpress提供 原文連結

寫了 5860316篇文章,獲得 23313次喜歡
精彩推薦