【发布时间】: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<>(它的定义已经可逆,因为您可以在两端推送/弹出)。残酷的现实:您需要检查许多事情,例如指针用法、operator new、区分大小写、类名限定符等等。
标签: c++ list linked-list private public