我已經研究了從算法介紹算法,然後我寫了這個代碼。但在我的另一輸出值被表示索引爲0和當我使用彈出功能它顯示代替3-使用數組的C++實現堆棧
#include <iostream>
int top;
void initialise_top(){
top = -1;
}
bool stack_empty(int a[]){
if(top == -1)
return true;
else
return false;
}
void push(int a[], int x, int s){
if(top < s - 1){
top = top + 1;
a[top] = x;
}
else
std::cout << "overflow" << "\n";
}
int pop(int a[]){
if (stack_empty(a) == true)
std::cout << "Underflow" << "\n";
else{
--top;
return a[top+1];
}
}
void display(int a[]){
for(int i = 0;i <= top; i++){
std::cout << a[i] << " ";
}
}
int main()
{
int arr[7];
push(arr,15,7);
push(arr,6,7);
push(arr,2,7);
push(arr,9,7);
push(arr,17,7);
push(arr,3,7);
display(arr);
std::cout << "\n";
int out = pop(arr);
std::cout << pop << "\n";
return 0;
}
這裏1被輸出 enter image description here
當你用調試器走過你的代碼時,你觀察到了什麼? – user0042
它表示「目標是最新的。」 無法完成(所有項目都是最新的)。「 – coder
這是您構建系統的消息。你知道調試器是什麼嗎? – user0042