2017-07-20 276 views
-4

我已經研究了從算法介紹算法,然後我寫了這個代碼。但在我的另一輸出值被表示索引爲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

+0

當你用調試器走過你的代碼時,你觀察到了什麼? – user0042

+0

它表示「目標是最新的。」 無法完成(所有項目都是最新的)。「 – coder

+0

這是您構建系統的消息。你知道調試器是什麼嗎? – user0042

回答

0

我有這樣的堆疊陣列代碼的快照在C.你可以用它作爲你的指導來實現它到C++。

#include <stdio.h> 
#include <stdlib.h> 

void push(void); 
void pop(void); 

int a[5]; 
int top = -1; 
int counter = 0; 
int choice; 


main() { 

do{ 
    printf("*********************************************\nSTACK\nPress the 
corresponding button you desire.\n\nPress 1 to push a number to 
stack.\nPress 2 to display the current stack.\nPress 3 to pop the current 
stack.\nPress 0 to exit.\n\n"); 
    scanf("%d", &choice); 
    if(choice == 0){ 
     choice = 0; 
    } 
    else if(choice == 1){ 
     push(); 
    } 
    else if(choice == 2){ 
     int i; 
    printf("Current Stack:\n"); 
    for(i = 0;i <= 4;i++){ 
     printf("%d", a[i]); 
    } 
    printf("\n\n"); 
    } 
    else if(choice == 3){ 
     pop(); 
    } 
}while(choice != 0); 


} 

void push(){ 

    if(top <= 3){ 
    int input; 
    printf("Enter number to push: "); 
    scanf("%d", &input); 
    top = top + 1; 
    a[top] = input; 

    int i; 
    printf("Current Stack:\n"); 
    for(i = 0;i <= 4;i++){ 
     printf("%d", a[i]); 
    } 
    printf("\n\n"); 
    }else{ 
    printf("Out of Bounds\n\n"); 
    exit(0); 
    } 
} 

void pop(){ 
    if(top >= 0){ 
    printf("You just popped: "); 
    printf("%d \n\n", a[top]); 
    a[top] = 0; 

    printf("Current Stack:\n"); 
    int i; 
    for(i = 0;i <= 4;i++){ 
     printf("%d", a[i]); 
    } 
    printf("\n\n"); 
    top = top - 1; 
    }else{ 
    printf("Out of Bounds\n\n"); 
    exit(0); 
    } 

} 
1

在你的實現中,你有「initialise_top()」函數。

void initialise_top(){ 
    top=-1; 
} 

但是你不要在主函數中調用它。如果你不調用它,你不能初始化「top」變量,「top」變量將保存垃圾值。 你可以在這裏閱讀細節: Default variable value

而且也theese行你有一些失誤:

int out=pop(arr); 
std::cout<<pop<<"\n"; 

你必須打印 「出」 變量:

std::cout << out << "\n"; 

你可以看看您在此實施的更正代碼:

https://repl.it/JaOd/0

0
#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() 
{ 
    **initialise_top();**//this statement initialises top=-1 
    int arr[7]; 
    //std::cout<<stack_empty(arr)<<"\n"; 
    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<<**out**<<"\n"; 
    return 0; 
} 

1.在您的程序中插入第一個元素15時,top = 1的值。由於 到這個另外的值被顯示爲索引0. 因此要有top = 0,在main函數中調用函數initialise_top();。 2.要顯示3而不是1,請使用 std::cout<<out<<"\n"; 程序中的修改是大膽的。

0

我試圖改進我的代碼。請告訴我,如果可以改善。

#include <iostream> 

#define max 1000 
class Stack{ 
    int top; 

public: 
    int a[max]; 
    Stack(){ 
     top=-1; 
     } 
     bool stack_empty(); 
     void push(int x); 
     int pop(); 
     void display(); 
}; 

bool Stack::stack_empty(){ 
if(top==-1) 
    return true; 
else 
    return false; 
} 

void Stack::push(int x){ 
    int s=max-1; 
if(top<s){ 
top=top+1; 
a[top]=x; 
} 
else 
    std::cout<<"overflow"<<"\n"; 
} 

int Stack::pop(){ 
if (stack_empty()==true) 
    std::cout<<"Underflow"<<"\n"; 
else{ 
    --top; 
    return a[top+1]; 
} 
} 

void Stack::display(){ 
for(int i=0;i<=top;i++){ 
    std::cout<<a[i]<<" "; 
} 
} 

int main() 
{ 
    Stack stack1; 
    stack1.push(15); 
    stack1.push(6); 
    stack1.push(2); 
    stack1.push(9); 
    stack1.push(3); 
    stack1.display(); 
    std::cout<<"\n"; 
    std::cout<<stack1.pop()<<"\n"; 
    stack1.display(); 
    return 0; 
}