【发布时间】:2012-03-10 07:46:00
【问题描述】:
C++ 问题在这里。我已经成功(经过一些研究:P)为一堆整数创建了一个链表实现。我在为 char* 修改它时遇到了一些麻烦......
我认为这可能只是与我在下面定义的 linklistCommands 类使用的函数相关的引用/取消引用指针的问题。 (我一直难以理解何时在参数和返回值中使用 & 或 *。)我已经在我的代码中注释了我可能混淆的行。
无论如何,这是我迄今为止的代码:
struct linkc { // one 'link', stores a pointer to a char array
char * value;
linkc *next;
};
class linklistCommands
{
public:
linklistCommands()
{top = NULL;}
~linklistCommands()
{}
void push(char * address) // Pretty sure I'm OK here.
{
linkc *temp = new linkc;
temp->value = address;
temp->next = top;
top = temp;
}
char* pop() // Pretty sure I have to change something on this line
{
if (top == NULL)
return 0;
linkc * temp;
temp = top;
char * value;
value = temp->value;
top = temp->next;
delete temp;
return value;
}
bool isEmpty()
{
if (top == NULL)
return 1;
return 0;
}
private:
linkc *top;
};
int main(void)
{
// pushed strings are of an arbitrary, but always known, length
char[4] stringA = "foo";
char[6] stringB = "fooba";
char[8] stringC = "foobar ";
linklistCommands commandList;
commandList.push(stringA);
commandList.push(stringB);
commandList.push(stringC);
while(commandList.isEmpty!=1)
{
cout << (*commandList.pop()) << endl;
}
}
感谢您阅读我的问题和/或您可以提供的任何澄清:)
【问题讨论】:
标签: c++ string linked-list char