为什么我的打印功能不起作用?链表
Why is my print function not working? Linked list
我正在做一个课堂作业,我必须创建一个代表大数字的链表,但我无法让打印功能工作。
这是代码:
页眉
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
相关文章:
- 禁用地址共享注册表不起作用
- 双链表的自定义实现不起作用(教育)
- 针对静态库的 G++ 链接不起作用
- 为什么我的打印功能不起作用?链表
- 双链表堆栈删除函数不起作用
- 如果使用 QToolTip::showText,则 QToolTip 样式表不起作用
- 使用链表.为什么我的插入功能不起作用?
- 模板外部链接不起作用
- 与LLVM LLD链接不起作用[MACOS]
- 虽然循环在我的链表中不起作用
- 返回值上的 C++ 方法链接不起作用
- 删除链表节点,C++函数不起作用
- 如何获取链表中的所有"next"节点?构建调用字符串不起作用
- Boost Logger静态链接:不起作用
- cocos2d android编译时链接不起作用
- 为什么这个链表不起作用?
- c++中的动态链接不起作用
- 链表不起作用(c++)
- g++:静态链接不起作用
- 生成文件链接不起作用(尽管没有错误消息)