如何在C++中打印出以下实现的链接列表
How do you print out a linked list of the following implementation in C++?
我刚刚完成了制作链表的程序,也应该打印出内容。 它编译正确,似乎可以制作链表,但似乎没有打印出任何东西。 关于我在这里做错了什么的任何建议?
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
struct DeltaTimerNode
{
int timerInterval;
DeltaTimerNode *next;
DeltaTimerNode(int tempTimeInt, DeltaTimerNode *tempNext = NULL)
{
timerInterval = tempTimeInt;
next = tempNext;
}
};
DeltaTimerNode *deltaTimerList = NULL;
void insert(int deltaTimerValue)
{
deltaTimerList = new DeltaTimerNode (deltaTimerValue, deltaTimerList);
}
int main()
{
int tickTime;
char choice;
vector<int> rawTimers;
//int i = 0; Originally used for tracking something. Moved logic to different function.
do
{
cout<< "Input timer value (not delta timer)." << endl;
cin >> tickTime; //Input regular value of timer, not the delta time. That will be converted automatically.
rawTimers.push_back(tickTime);
//i++;
cout<< "Are there more timer values? Input y for yes, n for no."<<endl;
cin >> choice;
}
while(choice == 'y');
sort (rawTimers.begin(), rawTimers.end());
DeltaTimerNode *deltaTimerList = NULL;
for (int j = 0; j < rawTimers.size(); j++) //for loop populates list.
{
if (j == 0)
{
insert (rawTimers[0]);
}
else
{
insert (rawTimers[j] - rawTimers[j-1]);
}
}
DeltaTimerNode *ptr = deltaTimerList;
while (ptr != NULL)
{
cout << ptr -> timerInterval << " "; //should print out stuff here.
ptr = ptr -> next;
}
return 0;
}
您声明了一个局部变量deltaTimerList
并遮蔽了 gloval 变量deltaTimerList
。删除有害声明
DeltaTimerNode *deltaTimerList = NULL;
从main()
.
另请注意,您应该通过delete
销毁您通过new
创建的任何内容。
首先,您需要删除结构中的默认构造函数,并且不需要insert()函数和全局变量。只需创建一个新节点,并在每次使用节点时将指针指定为新节点即可。我希望你明白这个想法,这应该有效:
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
struct DeltaTimerNode
{
int timerInterval;
DeltaTimerNode *next;
};
int main()
{
int tickTime;
char choice;
vector<int> rawTimers;
do
{
cout << "Input timer value (not delta timer)." << endl;
cin >> tickTime; //Input regular value of timer, not the delta time. That will be converted automatically.
rawTimers.push_back(tickTime);
cout << "Are there more timer values? Input y for yes, n for no." << endl;
cin >> choice;
} while (choice == 'y');
sort(rawTimers.begin(), rawTimers.end());
DeltaTimerNode deltaTimerList;
DeltaTimerNode* head = &deltaTimerList;
for (int j = 0; j < rawTimers.size(); j++) //for loop populates list.
{
if (j == 0)
{
head->timerInterval = rawTimers[j];
head->next = new DeltaTimerNode();
head = head->next;
}
else
{
head->timerInterval = rawTimers[j] - rawTimers[j - 1];
head->next = new DeltaTimerNode();
head = head->next;
}
}
head = &deltaTimerList;
while (head->next != NULL)
{
cout << head->timerInterval << " "; //should print out stuff here.
head = head->next;
}
return 0;
}
相关文章:
- C++ 实现模板单例类时出现链接错误
- 如何在C / C++中正确实现链接列表而不会使程序崩溃
- 是否应该链接带有实现的头文件?
- 如何使用 c++ 实现 WMI 类的链接?
- 在 h 和 cpp 文件中单独声明和实现模板有时有效,有时会出现链接器错误
- C 无需使用匿名名称空间即可实现内部链接
- 实现双链接列表,其中数据存储在堆上
- 内部源代码 - 在链接列表实现中插入元素上的删除会破坏整个应用程序
- 转换为链接切割树的C 实现
- 在单个链接列表中实现复制构造函数C
- 覆盖 CPP 中的链接以指向模拟实现
- 使用链接列表在C 中实现堆栈
- 优先级队列实现为单一链接,无法更新插入的指针转换
- 在C 中实现链接列表类的临界功能
- 链接的列表在类C 中实现,显示分段故障
- 具有 C 链接和C++实现的不透明结构
- 如何在链接列表类中实现分配运算符
- 点产品计算链接列表实现
- 自定义排序实现会导致链接错误
- 我可以将 Node.js C++ 扩展与 Node 的内部 OpenSSL 实现链接吗?