为什么我的打印功能不起作用?链表

Why is my print function not working? Linked list

本文关键字:链表 不起作用 功能 我的 打印 为什么      更新时间:2023-10-16

我正在做一个课堂作业,我必须创建一个代表大数字的链表,但我无法让打印功能工作。

这是代码:

页眉

class List
{
private:
struct node
{
int data;
node* next;
};
node* head;
node* temp;
node* curr;
public: 
List();
void addNode(std::string digits);
void printList();
};

构造 函数

List::List()
{
head = NULL;
curr = NULL;
temp = NULL;
}

创建列表的函数

void List::addNode(string digits)
{
int o = 1;
int j = 0;
int i = 0;
node *n;
node *head;
node *temp;
//=================================================================================//
n = new node;
if (digits[0]=='-')
{
n -> data = -1;
head = n;
temp = n;
}
else
{
n -> data = 1;
head = n;
temp = n;
}
//================================================================================//
if ((head->data) == -1)
{
while (o < digits.length())
{
n = new node;
n->data = stoi(digits.substr(o,1));
temp -> next = n;
temp = temp -> next;
o++;
}
}
else
{
while(i < digits.length())
{
n = new node;
n->data = stoi(digits.substr(i,1));
temp -> next = n;
temp = temp -> next;
i++;
}
}

我一直在尝试实现的打印功能,它没有给出输出(空白(:

void List::printList()
{
node* curr = head;
while(curr != NULL)
{
cout<<curr -> data<<" ";
curr = curr -> next;
}
}

当我在addNode函数中使用此代码时,列表打印正常:

if ((head -> data) == -1)
{
while(j < digits.length())
{
cout<<head -> data<<" ";
head = head -> next;
j++;
}
}
else
{
while(j<=digits.length())
{
cout<<head -> data<<" ";
head = head -> next;
j++;
}
}

对于初学者来说,这些数据成员

node* temp;
node* curr;

是多余的。如果需要,您可以在类的成员函数中使用类似的局部变量代替它们。

函数addNode处理局部变量head,而不是具有相同名称的数据成员。

void List::addNode(string digits)
{
int o = 1;
int j = 0;
int i = 0;
node *n;
node *head;
//…

此外,您忘记将最后一个节点旁边的数据成员设置为nullptr

如果第二次调用成员函数,则会出现内存泄漏。

为一个字符调用标准函数std::stoi

n->data = stoi(digits.substr(i,1));

效率低下。

该类可以如下所示,如下面的演示程序所示。您将需要自己添加其他必需的成员函数(例如复制构造函数或析构函数(。

#include <iostream>
#include <string>
class List
{
private:
struct node
{
int data;
node *next;
} *head = nullptr;
public: 
List() = default;
void addNode( const std::string &digits );
std::ostream & printList( std::ostream &os = std::cout ) const;
};

void List::addNode( const std::string &digits )
{
if ( !digits.empty() && 
!( digits.size() == 1 && ( digits[0] == '-' || digits[0] == '+') ) )
{
node **current = &head;
while ( *current )
{
node *tmp = *current;
*current = ( *current )->next;
delete tmp;
}
std::string::size_type i = 0;
*current = new node { digits[i] == '-' ? -1 : 1, nullptr };
if ( digits[i] == '-' || digits[i] == '+' ) ++i;
for ( ; i < digits.size(); i++ )
{
current = &( *current )->next;
*current = new node { digits[i] - '0', nullptr };
}
}
}
std::ostream & List::printList( std::ostream &os ) const
{
if ( head != nullptr )
{
if ( head->data == -1 ) os << '-'; 
for ( node *current = head->next; current != nullptr; current = current->next )
{
os << current->data;
}
}
return os;
}
int main() 
{
List lst;
lst.addNode( "-123456789" );
lst.printList() << 'n';
lst.addNode( "987654321" );
lst.printList() << 'n';
return 0;
}

程序输出为

-123456789
987654321