我正在尝试为一个使用基于链接列表的堆栈创建一个程序,该堆栈包含指向包含字符串对象的节点的指针。
我想打印使用rod
的堆栈pop()
,它会在每次迭代时删除头对象,直到它为空。所以我需要找到一种方法来打印它之前备份rod[]
。
我已尝试将rod[]
的内容推送到rodCopy[]
,然后调用rod[x] = rodCopy[x]
,这应该将rod []恢复到原始状态,但这并不是&#39} ; t似乎工作,因为我再次开始获得seg错误。
另一个问题是它向后复制堆栈,因为它存储了最近存储的对象。
例如:
output:
rod[0] = "x", "xx", "xxx" //"x" is the top() object but "xxx" was the first
rodCopy[0] = "xxx", "xx", "x" // "x" is the first object, "xxx" is the top()
TowerHanoi.h
档案:
#ifndef TOWERHANOI_CAMERON_H
#define TOWERHANOI_CAMERON_H
#include "LStack.h"
namespace oreilly_A2 {
class TowerHanoi {
public:
TowerHanoi();
TowerHanoi(size_t numDiscs);
LStack<std::string> get_Discs();
void set_Discs(size_t disc);
void print_Game();
LStack<std::string> copyRod(int r);
void setRod(LStack<std::string> r1, LStack<std::string> r2);
private:
LStack<std::string> rod[3];
int discs, source, target;
};
}
#endif
TowerHanoi.cpp print_Game()
:
void TowerHanoi::print_Game() {
string xString;
LStack<string> rodCopy = rod[0];
//string spacing;
//int spaceInt= discs-1;
for (size_t a=0; a<discs;a++) {
xString = rod[0].pop();
cout << xString; //print rod[0]
if (!rod[1].empty()) { //is rod 2 empty?
cout << " ";
xString = rod[1].pop();
cout << xString; //print rod[1]
}
cout << endl;
}
string bottomRow; //print the bottom row after printing rods
for (int c=0; c < (((discs*2)-1)+3);c++) {
bottomRow.append("-");
}
string rodNum;
for (int c=0; c < (discs/2);c++) {
rodNum.append("-");
}
rodNum.append("rod 1");
cout << bottomRow << endl;
cout << rodNum << endl;
}
LStack.template
档案:
namespace oreilly_A2 {
template <typename Obj>
LStack<Obj>::LStack() {
list = new LinkedList<Obj>();
used=0;
}
template <typename Obj>
LStack<Obj>::~LStack() {
delete list;
}
//push
template <typename Obj>
void LStack<Obj>::push(Obj head_in) {
list->addToHead(head_in);
used++;
}
//pop
template <typename Obj>
Obj LStack<Obj>::pop() {
used--;
return list->removeFromHead();
}
//top
template <typename Obj>
const Obj& LStack<Obj>::top() {
return list->list_getHead();
}
//empty
template <typename Obj>
bool LStack<Obj>::empty() {
if (list->getEmpty()) {
return true;
}
return false;
}
//size
template <typename Obj>
int LStack<Obj>::size() const {
return used;
}
}
LinkedList.template文件addToHead()
和removeFromHead()
:
template <typename Item>
void LinkedList<Item>::addToHead(Item entry) {
if (head == NULL) {
head = new node<Item>(entry, NULL);
}
else {
head = new node<Item>(entry, head);
}
}
template <typename Item>
const Item LinkedList<Item>::removeFromHead() {
node<Item>* tmp_ptr;
tmp_ptr = head->link();
std::string head_copy;
head_copy = head->data(); //create a copy of head
head = tmp_ptr;
//delete tmp_ptr;
//head->set_data(head->link()); //set head's data to the previous object
return head_copy; //return head's original data
//delete head_copy;
}
Node.template set_data
和set_link
:
template <typename Object>
void node<Object>::set_data(const Object& new_data){
std::string new_string;
new_string = new_data;
word = new_string;
}
template <typename Object>
void node<Object>::set_link(node<Object>* new_link){
next= new_link;
}