c++ - 我在堆栈中的 pop 方法有什么问题?

标签 c++ class templates stack

我正在制作一个基于指针的堆栈模板。 push方法还行,我的pop方法好像不行。有人能看出这里出了什么问题吗?在 gdb 中,我显示第二个 while 循环导致段错误。怎么了?
代码如下:

#include <iostream>
#include <string>
using namespace std;

template <typename k>
class stack;


template <typename k>
class node{
    private:
        friend class stack<k>;
        k data;
        node<k> *next;
    public:
        node(k _x): data(_x), next(NULL) {}
};
template <typename k>
class stack{
    private:
        node<k> *start;
        unsigned int i;
    public:
        stack(): start(NULL), i(0) {}
        ~stack(){
            while(i!=0) pop();
        }
        void push(k element){
            node<k> *ptr;
            node<k> *temp;
            ptr=new node<k>(element);
            if(start==NULL){
                start=ptr;
                ptr->next==NULL;
            }
            else{
                while(temp->next!=NULL) temp=temp->next;
                temp->next=ptr;
                ptr->next=NULL; 
            }
            i++;
        }
        int pop(){
            if(i==1){
                int item=start->data;
                start=NULL;
                i=0;
                return item;
            }
            else{
            node<k> *temp=start;        //k is my typenam in templates
            node<k> *top=start;
            while(temp->next!=NULL) temp=temp->next;  //getting to last element
            while(top->next!=temp) top=top->next;   //getting to element before the last
            top->next=NULL;         //setting next to NULL
            int item=temp->data;    //getting data from element popped
            delete(temp);           //deleting last node
            i--;                    //decreasing the size
            return item;            //returning popped element
            }           
        }
        bool isempty(){
            if(i==0) return 1;
            else return 0;
        }
        int rozmiar(){
                return i;
        }
};



int main()
{
    stack<char> s;
    string slowo;
    cin>>slowo;
    for(int i=0; i<slowo.length(); i++){
        s.push(slowo[i]);
    }
    for(int i=0; i<slowo.length(); i++){
        s.pop();
    }

    return 0;
}

在 main 中有一个测试,输入单词,将单个字母压入堆栈,然后使用 pop 反向读取。 编辑。添加了完整代码。

最佳答案

在你的push函数中,

    else{
        while(temp->next!=NULL) temp=temp->next;
        temp->next=ptr;
        ptr->next=NULL; 
    }

temp 未初始化并导致失败。将 temp 初始化为 start

    else{
        temp = start;
        while(temp->next!=NULL) temp=temp->next;
        temp->next=ptr;
        ptr->next=NULL; 
    }

这应该可以解决您的问题。您可以通过

进行测试
for(int i=0; i<slowo.length(); i++){
    std::cout<<(char)s.pop();
}

Input : foo
Output : oof

关于c++ - 我在堆栈中的 pop 方法有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54413111/

相关文章:

c++ - 类模板出现在类成员访问表达式中

c++ - 从 C++ 代码调用 Tiny C 编译器

C++ 未处理的异常。 0xC0000005 : Access violation reading location 0xccccccd0

android - 将当前时间传递给 OpenGL ES 2.0 着色器以进行纹理动画 : animation stops after certain time

java - 检索类类型,并实例化一个相同类型的新类

c++ - 在 AngelScript 中注册一个 C++ 类并传递一个类实例

c++链表模板和节点

c++ - 模板构造函数重载问题

c++ - chrono::month 和 chrono::months 有什么区别

C++ 模板错误