//411440281 Lu-Wei-Yi
#include <stdio.h>
#include <stdlib.h>
#define SIZE 100
int top=-1;
int A[SIZE];
int empty(){
return(top==-1);
}
int full(){
return(top==SIZE);
}
int push(int x){
if(full()){
printf("stack is empty");
exit(0);
}
A[++top]=x;
}
int pop(){
if(empty()){
printf("stack is full");
exit(1);
}
return A[top--];
}
//411440281 Lu-Wei-Yi
int main(){
//-------push the number in stack A----------------------------//
int i;
for(i=0;i<=SIZE;i++){
push(i);
printf("%d ",A[i]);
}
//-------assign integer k the 2nd element----------------------//
int k; //確定陣列是不是一開始就是空的--如果是的話 exit(1)
k=pop();
if(empty()){
printf("1-element stack\n");
exit(2); //取不到地接下來的--exit(2)
}
k=pop();
printf("%d\n",k);
//411440281 Lu-Wei-Yi
//-------assign integer k the 9th element----------------------//
k=pop();
int j;
for(j=0;j<=8;j++){
if(empty()){
printf("element is empty in %d\n",j);
exit(2); //取不到地接下來的--exit(2)
}
k=pop();
}
printf("%d\n",k);
//-------assign integer k the bottom element----------------------//
k=pop(); //確定陣列是不是一開始就是空的--如果是的話 exit(1)
while(!=empty()){
k=pop(); //不需要 printf("element is empty in %d") 是因為取最底的不可能一個都沒有,所以不用考慮取不到的狀況
}
//-------assign integer k the 2nd element from top----------------------//
k=pop(); //確定陣列是不是一開始就是空的--如果是的話 exit(1)
if(empty()){
printf("1-element stack\n");
}
while(!=empty()){
if(top==1){ //當堆疊在第二格時top=1,所以可以得到第 2 個值
k=pop();
printf("%d\n",k);
return 0;
}
k=pop();
}
//-------printout all the the element in stack---------------------//
int cnt=1; //數到第幾個會是空的,等於 1 是因為一開始先pop掉一個了
k=pop(); //確定陣列是不是一開始就是空的--如果是的話 exit(1)
printf("%d ",k);
while(!=empty()){
k=pop();
printf("%d ",k);
cnt++; //算有幾個 element
}
printf("element is empty in %d\n",cnt);
return 0;
}
//411440281 Lu-Wei-Yi
/* 錯誤寫法--只有在 stack 是滿的有辦法用
//-------assign integer k the bottom element----------------------//
k=pop();
int j;
for(j=0;j<=SIZE-1;j++){
if(empty()){
printf("element is empty in %d\n",j);
}
k=pop();
}
printf("%d\n",k);
//-------assign integer k the 2nd element from top----------------------//
for(j=0;j<=SIZE-2;j++){
if(empty()){
printf("element is empty in %d\n",j);
}
k=pop();
}
printf("%d\n",k);
//-------assign integer k the 9th element----------------------//
int j;
for(j=top;j>=0;j--){
printf("%d\n",A[j]);
}
*/
To embed this project on your website, copy the following code and paste it into your website's HTML: