简体   繁体   English

使用链表在C ++中实现堆栈

[英]Implementing a Stack in C++ using a linked list

So I am trying to implement a Stack using a linked list and classes. 所以我试图使用链表和类来实现Stack。 Right now I have 6 different files: node.h, node.cpp, LL.h, LL.cpp, Stack.h, and Stack.cpp. 现在我有6个不同的文件:node.h,node.cpp,LL.h,LL.cpp,Stack.h和Stack.cpp。 I want to complete the Stack.h and Stack.cpp files so that they work as they should. 我想完成Stack.h和Stack.cpp文件,以便它们能够正常工作。 I already implemented the linked list functions and they work as they should. 我已经实现了链表功能,它们可以正常工作。 Here is the code: 这是代码:

node.h : node.h:

// node.h

class node { // node class used in the LL (linked list) class
    private:
        node * next; // Pointer to next node of an LL
        int data;    // integer data stored in this node

    public:
        node(int x, node * n);  // Constructor
        ~node();                // Destructor
        void set_data(int x);   // Change the data of this node
        void set_next(node * n);// Change the next pointer of this node
        int get_data();         // Access the data of this node
        node * get_next();      // Access the next pointer of this node
};  

LL.h : LL.h:

// LL.h
#include "node.h"

// Linked list class, used in the Stack class
class LL {
    private:
        node * head; // pointer to first node
        node * tail; // pointer to last node

    public:
        LL(); // Constructor
        ~LL(); // Destructor
        void prepend(int value); // add a node to the beginning of the LL
        int removeHead();        // remove the first node of the LL
        void print();            // print the elements of the LL
    node * get_head();       // access the pointer to the first node of the LL
};

Stack.h: Stack.h:

// Stack.h
#include "LL.h"

class Stack {
private:
    LL_t intlist;

public:

    Stack();    // Constructor
    ~Stack();       // Destructor

    void push(int value);
    int pop();      
    int isEmpty();
    void print();
};

And lastly, Stack.cpp: 最后,Stack.cpp:

// Stack.cpp
#include "Stack.h"
#include <stdio.h>

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

Stack::~Stack() {
    delete intlist;
}

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

void Stack::push(int value) {

    head= value;
}

int Stack::pop() {

    if ( !isEmpty() ) {
        int temp= tail->get_data();
        delete tail;
        return temp;    
    }
    return -1;
}

I am having compiling issues. 我正在编译问题。 It says get_data() is undefined and "head" and "tail" is undefined, even though I have " #include "LL.h" " in Stack.h and in LL.h, I have "#include "node.h" ", so they all build on one another so it should work correct? 它说get_data()是未定义的,而“ head”和“ tail”是未定义的,即使我在Stack.h和LL.h中有“ #include“ LL.h”“,我也有” #include“ node.h “”,所以他们都建立在彼此之上,所以它应该正常工作? I want it to compile so I can see if I am implementing Stack.h and Stack.cpp correctly. 我希望它编译,所以我可以看到我是否正确实现Stack.h和Stack.cpp。 Do you see any issues with the way I am implementing them? 你看到我实施它们的方式有什么问题吗? If so, can you point them out? 如果是这样,你能指出它们吗? Also, any idea as to why I am getting these compiling issues? 另外,任何想法为什么我得到这些编译问题? Any help appreciated! 任何帮助赞赏!

Let's look at your actual questions 让我们看看您的实际问题

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

results in error "head" and "tail" is undefined . 导致错误"head" and "tail" is undefined Now look at the header files, where are the declarations of head and tail ? 现在看一下头文件, headtail的声明在哪里? Answer, in the LL class not the Stack class. 回答,在LL类而不是Stack类中。 It's the responsibility of the LL class to initialise head and tail which it does the in the LL class default constructor. LL类负责初始化headtail ,它在LL类的默认构造函数中执行。 So your Stack constructor should look like this 所以你的Stack构造函数应该是这样的

Stack::Stack() {
}

Whenever you have a constructor for a class which contains another class a constructor for the other class will be called. 每当你有一个包含另一个类的类的构造函数时,就会调用另一个类的构造函数。 In the case of Stack the default constuctor for LL is called implicitly, and this initialises head and tail for you. Stack的情况下,隐式调用LL的默认构造函数,这会为您初始化headtail You don't have to do anything. 你不需要做任何事情。

Now lets look at some more of your implementation. 现在让我们看看你的更多实现。

Stack::~Stack() {
    delete intlist;
}

intList is not a pointer, so it cannot be deleted. intList不是指针,因此无法删除。 It's clear that you are trying to 'call` the destructor for your list, but just like the constructor this happens automatically. 很明显,你试图为你的列表“调用”析构函数,但就像构造函数一样,它会自动发生。 Your destructor should look like this 你的析构函数应该是这样的

Stack::~Stack() {
}

Or you could (probably should) just remove it completely. 或者,您可以(可能应该)将其完全删除。

Moving on 继续

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

Again you are trying to access head somewhere it isn't accessible. 你再次尝试访问某个无法访问的head Your Stack class has an LL intlist object and that's what it should use, so (for instance) 你的Stack类有一个LL intlist对象,它应该使用它,所以(例如)

int Stack::isEmpty() {
    return intlist.get_head() == NULL;
}

Smae thing here Smae在这里

void Stack::push(int value) {
    head= value;
}

should be 应该

void Stack::push(int value) {
    intlist.prepend(value);
}

Use the object that the stack has (the intlist ) not the internals of other objects. 使用堆栈具有的对象( intlist )而不是其他对象的内部。

I'll leave you do sort out the rest. 我让你去整理其余的。 But you must understand the division of responsbilities that exist in your class design. 但是您必须了解班级设计中存在的责任划分。 The Stack class should not (and cannot) concern itself with the internals of the LL class. Stack类不应该(也不能)关注LL类的内部。 All that the operations that Stack needs to perform should be doable with the public interface of the LL class. 所有Stack需要执行的操作都应该与LL类的公共接口相关。 If not then it's the LL class that needs to change. 如果不是,则需要更改LL类。

Also note that your pop implementation is not just wrong in executuion it's wrong in concept. 还要注意你的pop实现不仅仅是错误的执行,它在概念上是错误的。 Pop should remove the head of the list, not the tail. Pop应该删除列表的头部 ,而不是尾部。 A stack is a LIFO list (last in, first out) so pop removes the most recentaly added item. 堆栈是LIFO列表(后进先出),因此pop会删除最近添加的项目。 Now looking at the LL class there is a removeHead method (hint, hint). 现在看一下LL类有一个removeHead方法(提示,提示)。

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

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