【发布时间】:2018-09-22 13:53:08
【问题描述】:
所以我(大部分)理解带指针的链表的概念。但是,我不确定如何创建结构对象的链表,该链表在方法中传递。我需要另一个指针来指向结构上的指针吗? 我的结构包含指针:
// define a structure to hold bid information
struct Bid {
string bidId; // unique identifier
string title;
string fund;
double amount;
Bid *next;
Bid() {
amount = 0.0;
next = NULL;
}
};
我将结构对象传递给此方法以附加新的投标结构:
void LinkedList::Append(Bid bid) {
// FIXME (3): Implement append logic
//reference bid being passed??? Bid struct has pointers, do we need another pointer?
Bid *currNode = &bid;
//set node's next pointer to NULL (end)
currNode->next = NULL;
//if list is empty
if (head == NULL) {
head = currNode;
tail = currNode;
}
else {
tail->next = currNode;
tail = currNode;
}
}
【问题讨论】:
-
我不明白你的确切问题是什么
-
你不需要传递结构。你可以只传递一个指向它的指针。
-
我正在尝试访问正在传递的结构对象的指针,并用它创建一个链表。这就是我习惯的做法
ListAppend(list, newNode) { if (list->head == 0) { // List empty list->head = newNode list->tail = newNode } else{ list->tail->next = newNode list->tail = newNode } } -
你有什么理由不使用
std::forward_list- 你的是另一个重新实现,用“C with objects”风格编写,手动内存管理极易出错。这是一道作业题——请注明。
标签: c++ data-structures linked-list