【问题标题】:Retrieving chars from a linked list从链表中检索字符
【发布时间】: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


    【解决方案1】:

    你的课看起来不错,但主要需要改变:

     // pushed strings are of an arbitrary, but always known, length
     char stringA[] = "foo";
     char stringB[] = "fooba";
     char stringC[] = "foobar ";
    
     linklistCommands commandList;
    
     commandList.push(stringA);
     commandList.push(stringB);
     commandList.push(stringC);
    
     while(commandList.isEmpty()!=1)
     {
      cout << commandList.pop() << endl;
     }
    

    您应该考虑使用 std::string 代替 char*,它更简单、更安全。 另外, char[N] stringA = "...";它是 C# 或 Java 语法,而不是 C++

    【讨论】:

    • 请注意,如果您的 stringA、stringB 等超出范围(它们不会在主函数中,但如果在另一个函数中声明它们可能会),它们将不再存在于任何地方(嗯它们可能会也可能不会,这取决于您的程序正在执行的基本不相关的其他事情),如果它们后来被弹出,您的堆栈将返回垃圾。使用 std::string 可以防止这种情况发生。
    • 啊。 StringA,stringB 不是我实际代码的一部分,而只是在那里占位。至于我的实际变量,我正在管理那些永远不会超出范围使用的内存。
    • chac,我可以/我不应该在 C++ 中使用 char[N] 吗?我在那里键入 char[N] 只是为了表明字符串可能具有不同的长度,但长度是已知的,但我的实际代码没有将字符串初始化为其最终值 - 它们宁愿在 a 处添加一个字符时间,来自一个循环。
    • char X[N] 这是传统的 C 语法。如果使用得当,它非常好,并且可能比 std::string 更轻量级。但需要惯用的结构和更多的关注。例如,x + 3 连接,而是偏移指针...
    【解决方案2】:

    您必须分配和管理 char* 数据字段并复制输入值。 :) (或至少使用 strdup() )。您可能还想查看 const char* 作为输入数据的类型

    【讨论】:

      【解决方案3】:

      我从我的单元格发送,所以简短的回答:& 用于获取当前元素的地址,* 用于从地址获取值。所以通过使用 * 两次,就可以从一个指向某个值的地址的地址中获取值。

      【讨论】:

      • 是的,我通常都能做到这一点,但我觉得当运算符用于定义函数返回值或函数参数时,含义会发生变化。
      • 不是真的,但我想你需要看看它是如何编译的才能理解为什么会这样。无论如何,一颗星意味着您返回一个值的地址,两个开始意味着您有一个值地址的地址。编译器需要知道的是你的值的内存布局(类型)以及它必须遵循地址的频率。
      猜你喜欢
      • 1970-01-01
      • 2020-04-25
      • 1970-01-01
      • 2021-11-05
      • 1970-01-01
      • 1970-01-01
      • 2012-02-13
      • 2016-02-26
      • 2015-10-06
      相关资源
      最近更新 更多