简体   繁体   English

C ++堆栈推送/打印实现

[英]C++ Stack Push/Print Implementation

I'm trying to make a stack implementation in C++ but when I try to print the stack, it just prints the first element instead of the whole stack. 我正在尝试使用C ++实现堆栈实现,但是当我尝试打印堆栈时,它只会打印第一个元素而不是整个堆栈。 I've tested it and I'm pretty sure that my Push function is right, but I'm not sure. 我已经测试过了,我很确定我的Push函数正确,但是我不确定。

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

int main(){

    StackElement *stack = new StackElement();
    stack->data = 20;
    stack->Push(30,stack);
    stack->Push(40,stack);

    stack->Print(stack);

}

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


class StackElement{

public:

    int data;   
    StackElement* next;     
    StackElement();     
    void StackElement::Push(int value, StackElement *oldElement);   
    void StackElement::Print(StackElement *element); 
};

StackElement::StackElement(){
    next = NULL;
}


void StackElement::Push(int value, StackElement *oldElement){

    StackElement *newElement = new StackElement();                
    newElement->data = value;       
    printf("Element added to stack: %d\n", newElement->data);       
    oldElement->next = newElement;       
}


void StackElement::Print(StackElement *element){

    while(element->next != NULL){       
        printf("%d\n",element->data);       
        element = element->next;    
    }

}

Your code kept loosing the previous pushed element, leaking memory, as @Beta described. 正如@Beta所描述的,您的代码不断丢失前一个推送的元素,从而导致内存泄漏。

I suggest comparing my code below to your code. 我建议将下面的代码与您的代码进行比较。 You'll see, I've moved the handling of the stack elements outside, just to be able to keep track of the first element. 您会看到,我已经将堆栈元素的处理移到了外面,只是为了能够跟踪第一个元素。 Also, notice that there is no pointer in the main function. 另外,请注意主函数中没有指针。 That is what we expect from a class. 这就是我们对课堂的期望。

Stack_element is a struct really as there's not much point in making the Stack_element itself encapsulated, it is just an implementation detail of Stack. 实际上,Stack_element是一个结构,因为将Stack_element本身封装起来没有多大意义,它只是Stack的实现细节。

So here's my code derived from yours 所以这是我的代码

#include<iostream>

struct Stack_element{
  int data;
  Stack_element*next;
};   

class Stack{
private:
  Stack_element*last_data, first_data;

public:

  Stack():last_data(NULL), first_data(NULL){}  
  void push(int data);
  void print() const;  
};

void Stack::push(int data)
{
  Stack_element*p=new Stack_element();
  p->data=data;
  p->next=NULL;
  if(last_data)
    last_data->next=p;
  else // empty stack
    first_data=p;
  last_data=p;
}

void Stack::print()
{
  for(Stack_element*p=first_data;p;p=p->next)
    std::cout << p->data << std::endl; // ** Do not use printf in c++. Ever. **
}    

and in the main function just call 在主函数中只需调用

Stack stack;
stack.push(30);
stack.push(40);
stack.print();

REMARK: For a C++ish print you might want to do an ostream& print(ostream& os) instead, where 备注:对于C ++的打印,您可能需要执行ostream& print(ostream& os)而在

std::ostream& Stack::print(std::ostream& os)
{
  for(Stack_element*p=first_data;p;p=p->next)
    os << p->data << std::endl;
  return os;
} 

just to be able to write std::cout << stack.print() << std::endl; 只是为了能够编写std::cout << stack.print() << std::endl; . The benefit of this is that you can easily redirect to a file. 这样做的好处是您可以轻松地重定向到文件。

std::ofstream ofs("yourfile.txt");
ofs << stack.print() << std::endl; // prints to file instead of screen.

Suppose this much works as planned: 假设这可以按计划进行:

StackElement *stack = new StackElement();
stack->data = 20;
stack->Push(30,stack);

Now your data looks like [20]->[30] 现在您的数据看起来像[20]->[30]

Now you attempt 现在你尝试

stack->Push(40,stack);

So the Push method creates a new StackElement , gives it the value 40, and sets Stack to point to it: [20]->[40] . 因此, Push方法将创建一个新的StackElement ,将其赋予值40,并将Stack设置为指向它: [20]->[40] Notice that [30] has been lost. 请注意, [30]已丢失。

Then the Print function: 然后是Print功能:

while(element->next != NULL){       
  printf("%d\n",element->data);       
  element = element->next;    
}

If there is only one element (whose next is NULL), this function will quit and print nothing. 如果只有一个元素( next为NULL),则此函数将退出并且不打印任何内容。 If there are two, this function will print the data of the first, then quit. 如果有两个,此功能将先打印第一个数据,然后退出。 And there will never be more than two, as long as Push has that bug. 只要Push有该错误,就永远不会超过两个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM