【问题标题】:Nodes declared in private私有声明的节点
【发布时间】:2013-08-30 22:48:02
【问题描述】:

我已经很久没有用 C++ 编程了,我真的很生疏。请帮帮我好吗?我的任务是开发一个可逆的单链表,但节点必须私下声明。我如何访问它们以推送/弹出我的堆栈。还是我做错了?

#include <iostream>
using namespace std;

class ReversibleStack 
{
public:
    void push(int item);
    int pop();
    bool IsEmpty();
    void Reverse();

private:
    // let's build our singly linked list in private
    typedef struct node
    {
        int first; //the first node
        node *next; //pointer to the next node available
    }node;
};

#include "ReversibleStack.h"

#include <iostream>
using namespace std;

//This is the Push function that pushes an item onto the stack
void Push(int Item)
{
    node* current = first; // sets current pointer to first
    node* new_item = Item; // sets previous pointer to NULL
    node* next = current->next; // next item in stack
}

【问题讨论】:

  • 简单的答案:使用std::deque&lt;&gt;(它的定义已经可逆,因为您可以在两端推送/弹出)。残酷的现实:您需要检查许多事情,例如指针用法、operator new、区分大小写、类名限定符等等。

标签: c++ list linked-list private public


【解决方案1】:

这几乎是一个语法问题。你定义这样的方法:

void ReversibleStack::Push(int Item)
{
    // ... 
}

【讨论】:

    猜你喜欢
    • 2012-09-01
    • 2017-11-18
    • 2023-04-11
    • 1970-01-01
    • 1970-01-01
    • 2011-04-04
    • 1970-01-01
    • 1970-01-01
    • 2012-01-13
    相关资源
    最近更新 更多