Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/147.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在C++中使用数组打印堆栈_C++_Stack - Fatal编程技术网

如何在C++中使用数组打印堆栈

如何在C++中使用数组打印堆栈,c++,stack,C++,Stack,我已经在我的程序中添加了所有必需的功能,例如push、pop和print,但无法在控制台屏幕上打印堆栈输出。我创建了三个单独的文件,其中包含类、函数和主文件。我想知道我在堆栈中插入的元素已成功插入,因此我需要打印更新的堆栈 stack.h #ifndef Stack_H #define Stack_H using namespace std; class Stack{ private: //int const capacity=50; int stack[50]; int count;

我已经在我的程序中添加了所有必需的功能,例如push、pop和print,但无法在控制台屏幕上打印堆栈输出。我创建了三个单独的文件,其中包含类、函数和主文件。我想知道我在堆栈中插入的元素已成功插入,因此我需要打印更新的堆栈

stack.h

#ifndef Stack_H
#define Stack_H

using namespace std;

class Stack{
private: 
//int const capacity=50;
int stack[50];

int count;
int top;
int maxitem;

 public:  //this is where the functions go
 Stack();//constructor
 void Push(int data);
 void Pop(int delData);
 void PrintStack();
 };


#endif 
stack.cpp

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <vector>
#include <string>
#include "Stack.h"

using namespace std;

Stack::Stack()
{
    top=-1;
};

void Stack::Push(int data){
    top++;
    cin>>data;
    stack[top]=data;
    count++;
    cout<<"inserted succefully :"<<data<<endl;
}

void Stack::Pop(int item)
{
 if(top<0){
 cout<<"stack is empty";
            }
 else{
     item=stack[top];
     top--;

    cout<<"The deleted elememt is: "<<item<<endl;
     }
}


void Stack::PrintStack()
{
    if (top<0)
    {
        cout<<"Stack is empty ";
    }
    for(int i =top; i<0; i--)
    {
        cout<<"STACK IS "<<stack[i]<<"  "<<endl;
    }
}
main.cpp

#include <cstdlib>
#include <iostream>
#include <stack>
#include "Stack.h"

using namespace std;


int main()
{
    Stack R;
    int ele;
    int data;
    cout<<"enter the maximum elements in stack "<<endl;
     cin>>ele;
    cout<<endl<<"now enter the elements in stack "<<endl;


    for(int data=0; data<ele; data++){
    R.Push(data);

    R.PrintStack();
    }


    R.Pop(item);
    R.PrintStack();//stack after deletion of element


    system("pause");
}

例如,可以通过以下方式定义函数

void PrintStack()
{
   for ( int i = top; i >= 0; --i ) std::cout << stack[i] << ' ';
}
我想知道我在堆栈中插入的元素已成功插入,因此我需要打印更新的堆栈

堆栈容器是一个后进先出容器适配器,只允许您直接*访问称为top的back元素。当您编写自己的堆栈类时,请记住这一点。如果要确保实际插入了值,请实现一个top函数,返回stack[top]处的值。每次推后检查顶部


*注意-您可以在std::stack的标准实现中编写一个适配器来访问底层容器c。

INTI=0如何;i=0-我不想知道我在堆栈中插入的元素是否成功插入了为什么不使用调试器?问题在哪里?
 void Pop(int &delData);