Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.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_Output_Infinite Loop - Fatal编程技术网

C++ 调用堆栈打印函数时的无限循环

C++ 调用堆栈打印函数时的无限循环,c++,stack,output,infinite-loop,C++,Stack,Output,Infinite Loop,该程序应采用后缀算术表达式,然后编译该表达式的值。。每次读取一个整数时,它都会被推入堆栈。。否则,如果读取+,-,*将弹出两个整数 class Stack { Node *head; public: Stack() { head = NULL; }; void push(int data); int pop(); bool isEmpty(); void print(); }; void Stack::push(int

该程序应采用后缀算术表达式,然后编译该表达式的值。。每次读取一个整数时,它都会被推入堆栈。。否则,如果读取+,-,*将弹出两个整数

class Stack {
    Node *head;

public:
    Stack() {
        head = NULL;
    };

    void push(int data);
    int pop();
    bool isEmpty();
    void print();
};

void Stack::push(int data)
{
    Node * temp = new Node(data);
    temp->next = head;
    head = temp;
    delete temp;
}

int Stack::pop()
{
    int x = head->data;
    head = head->next;
    return x;
}

bool Stack::isEmpty(){
    return head == NULL;
}

void Stack::print(){
    Node * temp = head;
    while (temp != NULL){
        cout << temp->data << " ";
        temp = temp->next;
    }
    delete temp;
}


int main() {

    Stack st;
    char exp [] = "23+", c;
    int i, a;

    for (i = 0; exp[i] != '\0'; i++){
        c = exp[i];

        if (c == '+'&&!st.isEmpty()){
            a = st.pop() + st.pop();
            st.push(a);
        }
        else if (c == '-'&&!st.isEmpty()){
            a = st.pop() - st.pop();
            st.push(a);
        }
        else if (c == '/'&&!st.isEmpty()){
            a = st.pop() / st.pop();
            st.push(a);
        }
        else if (c == '*'&&!st.isEmpty()){
            a = st.pop() * st.pop();
            st.push(a);
        }
        else if (c == '0')
            st.push(0);
        else if (c == '1')
            st.push(1);
        else if (c == '2')
            st.push(2);
        else if (c == '3')
            st.push(3);
        else if (c == '4')
            st.push(4);
        else if (c == '5')
            st.push(5);
        else if (c == '6')
            st.push(6);
        else if (c == '7')
            st.push(7);
        else if (c == '8')
            st.push(8);
        else if (c == '9')
            st.push(9);

        cout << c << endl;
        st.print();
    }
    cin >> a;
    return 0;    
}
类堆栈{
节点*头;
公众:
堆栈(){
head=NULL;
};
无效推送(int数据);
int-pop();
bool是空的();
作废打印();
};
void Stack::push(int数据)
{
节点*临时=新节点(数据);
温度->下一步=头部;
压头=温度;
删除临时文件;
}
int Stack::pop()
{
INTX=头部->数据;
头部=头部->下一步;
返回x;
}
bool Stack::isEmpty(){
返回头==NULL;
}
void Stack::print(){
节点*温度=头部;
while(temp!=NULL){
下一步是收集数据;
}
删除临时文件;
}
int main(){
斯塔克街;
字符exp[]=“23+”,c;
int i,a;
对于(i=0;exp[i]!='\0';i++){
c=exp[i];
如果(c='+'&&!st.isEmpty()){
a=圣普()+圣普();
圣普什(a);
}
else如果(c='-'&&&!st.isEmpty()){
a=圣普()-圣普();
圣普什(a);
}
else if(c=='/'&&&!st.isEmpty()){
a=圣普()/圣普();
圣普什(a);
}
else if(c=='*'&&&!st.isEmpty()){
a=圣普()*圣普();
圣普什(a);
}
else如果(c=='0')
圣推(0);
else如果(c=='1')
圣普什(1);
else如果(c=='2')
圣普什(2);
else如果(c=='3')
圣普什(3);
else如果(c=='4')
圣普什(4);
else如果(c='5')
圣普什(5);
else如果(c=='6')
圣普什(6);
else如果(c='7')
圣普什(7);
else如果(c='8')
圣普什(8);
else如果(c='9')
圣普什(9);
库塔;
返回0;
}
当我在main中调用print函数时,我得到一个无限循环作为输出。。
我试图寻找导致无限循环的东西,但找不到它。

这里是推送
弹出
的建议

试着理解逻辑

void Stack::push(int data) 
{
    Node * temp = new Node(data);
    temp->next=head;
    head=temp;
    //Do not delete temp; deleting temp will delete the new added Node
}

int Stack::pop() 
{ 
    Node* temp = Head;
    int x=head->data;
    head=head->next;
    delete temp; //here you free the released memory.
    return x;
}
此外,对于代码中的每个数字,您可以按如下方式执行所有if/else:

else if(c>=0 && c<=9){
     st.push(c-'0');
}

else如果(c>=0&&c这里是
推送和
pop
的建议

试着理解逻辑

void Stack::push(int data) 
{
    Node * temp = new Node(data);
    temp->next=head;
    head=temp;
    //Do not delete temp; deleting temp will delete the new added Node
}

int Stack::pop() 
{ 
    Node* temp = Head;
    int x=head->data;
    head=head->next;
    delete temp; //here you free the released memory.
    return x;
}
此外,对于代码中的每个数字,您可以按如下方式执行所有if/else:

else if(c>=0 && c<=9){
     st.push(c-'0');
}
else如果(c>=0&&c我发现问题:

  • push()中使用
    delete

  • 未在
    pop()中使用
    删除

    你需要:

    int Stack::pop()
    {
       if ( head != NULL )
       {
          int x = head->data;
          Node * temp = head;
          head = head->next;
          delete temp; 
          return x;
       }
       else
       {
          // Figure out what you want to do if head is NULL
       }
    }
    
  • 使用
    print()
    中的
    delete

  • 我看到的问题:

  • push()中使用
    delete

  • 未在
    pop()中使用
    删除

    你需要:

    int Stack::pop()
    {
       if ( head != NULL )
       {
          int x = head->data;
          Node * temp = head;
          head = head->next;
          delete temp; 
          return x;
       }
       else
       {
          // Figure out what you want to do if head is NULL
       }
    }
    
  • 使用
    print()
    中的
    delete


  • 为什么在<代码>堆栈中,当你的代码>空的时候,< /C> >:打印< /代码>方法?更好的问题:为什么你在<代码>堆栈中,代码>删除/临时>,< /Cube >::推压< /Cord>函数????根本不应该。不像java,C++没有垃圾收集器。如果我不删除它,我会从内存中删除…显然,同样,不像java,你需要花时间学习C++内存管理是如何工作的。在空指针上,<代码>删除<代码>当然不会导致无限循环,但是<代码>删除临时; >在你的代码>推< /Co>函数将最终调用平出und。定义的行为和最确定的行为可以导致自引用节点,从而导致无限循环你需要在你把结果存储到X之后立即删除头。为什么你在<代码>堆栈中等于空的空代码:< /Cuff>::打印< /代码>方法?更好的问题:为什么你在<代码>堆栈中:代码>删除代码?<:代码> <代码>函数????根本不应该存在。不像java,C++没有垃圾回收器。如果我不删除它,我会忘记内存,删除临时;在循环之外。我不认为它造成无限循环。显然,与java不同,你需要花时间学习C++内存管理是如何工作的。您的
    push
    函数最终将调用完全未定义的行为,并且极有可能导致自引用节点,从而导致无限循环在将结果存储到x中后,您需要立即删除磁头。但我不认为任何数字会被推入堆栈,因为打印函数不会打印任何内容。在pop()中你没有使用temp..所以我不认为我们需要创建一个temp节点并删除它。但是我不认为任何数字会被推到堆栈中,因为print函数没有打印任何内容。在pop()中你没有使用temp..所以我认为我们不需要创建一个temp节点并删除它。很抱歉,我没有得到数字4。你能告诉我你所说的“/”是什么意思吗?头的旧值消失了。这是内存泄漏。”?@Raghad,如果您不实现析构函数,分配给节点的内存将不会被释放。我感觉您试图在
    push()
    中这样做,但那不是正确的位置。在从堆栈中弹出一个项或对象超出范围之前,您需要分配的内存。@Raghad,关于“//head的旧值已消失。这是内存泄漏。”,假设堆栈中有两个项-1和2。如果弹出顶部,堆栈将只有2个。如果不删除与1对应的节点,则分配给该节点的内存不会释放,这是内存泄漏。这真的很有帮助。是吗
    Stack::~Stack()
    {
       while (head)
       {
          Node * temp = head;
          head = head->next;
          delete temp;
       }
    }