0

对于一个项目,我试图设置一个链表对象,以便可以使用显式值构造函数对其进行初始化。我希望它看起来像这样:

WORD you("you");//where the object you's linked list now contains y o u;

但是当我打印出你的对象时,我看到的只是这个符号“=”,当我打印出你的长度时,我得到 -858993459

这是我的显式值构造函数,有人可以告诉我我做错了什么吗?

WORD::WORD(string s)
{
front = 0;
int i = 0;
int len = s.length();

if(front == 0)
{   
    front = new alpha_numeric;
    alpha_numeric *p = front;

    while(s[i] <= len)
    {
        p -> symbol = s[i];
        p -> next = new alpha_numeric;
        p = p -> next;
        p -> symbol = s[i++];
    }
    p -> next = 0;
}
}

如果有帮助,这是类声明文件

#include <iostream>
#include <string>

using namespace std;
#pragma once

class alpha_numeric //node
{
public:
char symbol; //data in node
alpha_numeric *next;//points to next node
};

class WORD
{
public:
WORD(); //front of list initially set to Null
WORD(const WORD& other);
WORD(string s); //***EXPLICIT VALUE CONSTRUCTOR
bool IsEmpty(); //done
int Length();
void Add(char); //done
//void Insert(WORD bword, int position);
//void operator=(char *s);

friend ostream & operator<<(ostream & out, const WORD& w);//done

private:
alpha_numeric *front; //points to the front node of a list
int length;
};
4

4 回答 4

1

您已经初始化std::string s为. 然后你已经初始化并且你想要遍历这个字符串:int i0int lens.length();

while(s[i] <= len) <----------------- THIS IS WRONG
{
    ...
    p -> symbol = s[i++];
}

另请注意,复杂度std::basic_string::length()为 O(1)。使用临时变量没有意义len。你还做了很多多余的事情,比如分配0front然后检查if (front == 0)。它可能看起来像这样:

WORD::WORD(std::string s)
{
    length = s.length();
    if (length == 0)
    {
        front = NULL
        return;
    }

    front = new alpha_numeric;
    alpha_numeric *p = front;

    int i = 0;
    while(i < length - 1)
    {
        p->symbol = s[i++];
        p->next = new alpha_numeric;
        p = p->next;
    }
    p->symbol = s[i];
    p->next = NULL;
}
于 2012-06-06T19:34:57.650 回答
1

你从来没有设置长度,所以这就是为什么它是垃圾。正如 luthien256 ahd LihO 指出的那样,您的 while 循环也是错误的,并且 if (front == 0) 测试没有任何意义。

最后,p -> symbol = s[i++];不需要。只需增加 i。

试试这个:

class alpha_numeric //node
{
public:
char symbol; //data in node
alpha_numeric *next;//points to next node
};

class WORD
{
public:
WORD(); //front of list initially set to Null
WORD(const WORD& other);
WORD(string s); //***EXPLICIT VALUE CONSTRUCTOR
bool IsEmpty(); //done
int Length() { return length; }
alpha_numeric *Front() { return front; }
void Add(char); //done
//void Insert(WORD bword, int position);
//void operator=(char *s);

friend ostream & operator<<(ostream & out, const WORD& w);//done

private:
alpha_numeric *front; //points to the front node of a list
int length;
};

WORD::WORD(string s)
{
  front = 0;
  int i = 0;
  int len = s.length();
  length = len;
  if (length == 0) {
    front = NULL;
    return;
  }
  front = new alpha_numeric;
  alpha_numeric *p = front;

  while(i < len)
  {
      p -> symbol = s[i];
      if (i != len - 1) {
        p -> next = new alpha_numeric;
        p = p -> next;
      }
      else
        p -> next = NULL;
      ++i;
  }
}


int main() {
  WORD you("you");
  alpha_numeric* front = you.Front();
  while(front != NULL) {
    cout<<(front->symbol)<<endl;
    front = front->next;
  }
  cout<<you.Length()<<endl;
  return 0;
}
于 2012-06-06T19:35:36.887 回答
0

您的 while 循环不太可能正在执行。我相信:

while(s[i] <= len)

应该

while(i < len)

于 2012-06-06T19:34:28.370 回答
0

试试这个:

WORD::WORD(string s)
{
  int i;
  int len = s.length();

  front = new alpha_numeric;
  alpha_numeric *p = front;

  for(i = 0; i < len; i++)
  {
        p -> symbol = s[i];
        p -> next = (i == len - 1) ? 0 : new alpha_numeric;
        p = p -> next;
  }
}
于 2012-06-06T19:41:13.487 回答