C++ C++;利用数组实现堆栈

C++ C++;利用数组实现堆栈,c++,arrays,stack,C++,Arrays,Stack,我从介绍算法开始研究算法,然后 我已经写了这段代码。但在我的输出中,索引0显示了另一个值。当我使用pop函数时,它显示1而不是3 #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

我从介绍算法开始研究算法,然后 我已经写了这段代码。但在我的输出中,索引0显示了另一个值。当我使用pop函数时,它显示1而不是3

#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;
}
#包括
int top;
无效初始化_top(){
top=-1;
}
布尔堆栈_为空(int a[]{
如果(顶部==-1)
返回true;
其他的
返回false;
}
无效推送(int a[],int x,int s){
如果(顶部STD::CUT< P>我在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);
    }

 }
#包括
#包括
无效推送(void);
void-pop(void);
int a[5];
int top=-1;
int计数器=0;
智力选择;
main(){
做{
printf(“************************************************************\n堆栈\n按
所需的相应按钮。\n\n按1可将数字按入
堆栈。\n按2显示当前堆栈。\n按3弹出当前堆栈
堆栈。\n按0退出。\n\n“;
scanf(“%d”,选择(&C);
如果(选项==0){
选择=0;
}
else if(选项==1){
推();
}
else if(选项==2){
int i;
printf(“当前堆栈:\n”);

对于(i=0;i在您的实现中,您有“initialise_top()”函数

void initialise_top(){
   top=-1;
}
但你们不在主函数中调用它。若你们不调用它,你们就不能初始化“top”变量,“top”变量将保存垃圾值。 您可以在此处阅读详细信息:

在这些行中,你也有一些错误:

int out=pop(arr);
std::cout<<pop<<"\n";
int out=pop(arr);
std::cout
#包括
int top;
无效初始化_top(){
top=-1;}
布尔堆栈_为空(int a[]{
如果(顶部==-1)
返回true;
其他的
返回false;
}
无效推送(int a[],int x,int s){

如果(顶部我已尝试改进我的代码。请告诉我是否可以改进

#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;
}
#包括
#定义最大1000
类堆栈{
int top;
公众:
int a[max];
堆栈(){
top=-1;
}
bool stack_empty();
无效推力(intx);
int-pop();
void display();
};
bool Stack::Stack_empty(){
如果(顶部==-1)
返回true;
其他的
返回false;
}
void Stack::push(int x){
int s=max-1;

if(Top)当使用调试器检查代码时,您观察到了什么?它说“目标是最新的。无需执行任何操作(所有项都是最新的)。”这是构建系统的一条消息。您知道调试器是什么吗?不。我使用代码块,并且有调试选项。因此我单击了那里。
#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;
}
#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;
}