search
尋找貓咪~QQ 地點 桃園市桃園區 Taoyuan , Taoyuan

[C/C++ 演算法]-資料結構與演算法(文魁):堆疊[Stack]使用範例(先進後出) – jashliao部落格

[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 提供 原文連結

寵物協尋 相信 終究能找到回家的路
寫了7763篇文章,獲得2次喜歡
留言回覆
回覆
精彩推薦