gpt4 book ai didi

C++:我的链表尾部没有改变?我似乎无法让节点正确指向彼此?

转载 作者:行者123 更新时间:2023-11-27 23:36:49 28 4
gpt4 key购买 nike

我正在尝试创建一个包含节点的链表并遍历这些节点。但是,我的链表尾部保持不变,不会更新。

主要内容:

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include "stockclass.h"

using namespace std;

int main()
{

LinkedList myList;

ifstream file("stocks.txt");


string line;
while (getline(file, line))
{
stringstream ss(line);
string symbol;
string numstr;

getline(ss, symbol, ',');

getline(ss, numstr, ',');
stringstream priceSS(numstr);
double price = 0;
priceSS >> price;

getline(ss, numstr, ',');
stringstream sharesSS(numstr);
int numOfShares = 0;
sharesSS >> numOfShares;

stock myStock(symbol, price, numOfShares);

Node newNode(myStock);

myList.add_node(newNode);

cout << myList.head << ", " << myList.tail << endl;
cout << myStock.symbol << ", " << myStock.price << ", " << myStock.numOfStocks << endl;

};

MyList.print();
return 0;

}

stocks.txt 文件

GOOG,250.00,380
APPL,180.00,400
MEIS,46.00,67

.cpp文件

#include <iostream>
#include "stockclass.h"
#include <string>

using namespace std;


stock::stock() {

symbol = "";
price = 0.00;
numOfStocks = 0;
};

stock::stock(string sym, double pri, int num) {

symbol = sym;
price = pri;
numOfStocks = num;


};

stock::printStock() {

cout << symbol << ", " << price << ", " << numOfStocks;

}




Node::Node(stock myStock) {

next = NULL;
s = myStock;

};

void Node::printNode() {

cout << s.symbol << ", " << s.price << ", " << s.numOfStocks << endl;

};


LinkedList::LinkedList() {

head = NULL;
tail = NULL;

};

void LinkedList::add_node(Node& n) {

if (head == NULL) {
head = &n;
tail = &n;

} else {
tail->next = &n;
tail = &n;
n.next = NULL;
};

};

void LinkedList::print() {

head->printNode();
//Node *tempPointer = tail;

if (tail != NULL) {

tail->printNode();
tail = tail->next;

};
};

.h文件

using namespace std;
#include <string>
#include <iostream>


class stock {

public:

friend ostream& operator<<(ostream &out, stock s);
string symbol;
double price;
int numOfStocks;

stock();
stock(string sym, double pri, int num);
printStock();


};

class Node {

public:
Node *next;
stock s;
Node();
Node(stock myStock);
void printNode();

};

class LinkedList {

public:

Node *head;
Node *tail;

LinkedList();
void add_node(Node& n);
void print();
static void display(Node *head);

};

这是我尝试使用 linkedlist.print() 时的输出;

https://imgur.com/a/gUo8eFl

linkedlist.print() 输出在 Please insert number choice: 3 之后

不确定我需要修复什么......尾地址在所有 3 个 while 循环中都是相同的。我不知道为什么它没有正确更新。

最佳答案

您应该在 LinkedList 中分配新节点类(class)。为列表分配节点不是调用者的责任。

此外,执行您的 print()方法甚至根本不会尝试遍历列表。

简而言之,你的设计LinkedList类全错了。试试这个:

#include <string>
#include <iostream>

class stock {
public:
friend std::ostream& operator<<(std::ostream &out, const stock &s);

string symbol;
double price;
int numOfStocks;

stock();
stock(string sym, double pri, int num);

void printStock();
};

class Node {
public:
Node *next;
stock s;

Node(const stock &myStock);

void printNode();
};

class LinkedList {
public:
Node *head;
Node *tail;

LinkedList();
~LinkedList();

void add_node(const stock &s);
void print();

static void display(Node *head);
};
#include <iostream>
#include <string>
#include "stockclass.h"

using namespace std;

stock::stock() {
symbol = "";
price = 0.00;
numOfStocks = 0;
}

stock::stock(string sym, double pri, int num) {
symbol = sym;
price = pri;
numOfStocks = num;
}

void stock::printStock() {
cout << symbol << ", " << price << ", " << numOfStocks;
}

Node::Node(const stock &myStock) {
next = NULL;
s = myStock;
}

void Node::printNode() {
cout << s.symbol << ", " << s.price << ", " << s.numOfStocks << endl;
}

LinkedList::LinkedList() {
head = NULL;
tail = NULL;
}

LinkedList::~LinkedList() {
Node *n = head;
while (n) {
Node *next = n->next;
delete n;
n = next;
}
}

void LinkedList::add_node(const stock &s) {
Node *n = new Node(s);
if (!head)
head = n;
if (tail)
tail->next = n;
tail = n;
}

void LinkedList::print() {
Node *n = head;
while (n) {
n->printNode();
n = n->next;
}
}
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include "stockclass.h"

using namespace std;

int main() {
LinkedList myList;

ifstream file("stocks.txt");

string line;
while (getline(file, line)) {
istringstream ss(line);
string symbol;
string numstr;

getline(ss, symbol, ',');

getline(ss, numstr, ',');
istringstream priceSS(numstr);
double price = 0;
priceSS >> price;

getline(ss, numstr, ',');
istringstream sharesSS(numstr);
int numOfShares = 0;
sharesSS >> numOfShares;

stock myStock(symbol, price, numOfShares);
myList.add_node(myStock);

cout << myList.head << ", " << myList.tail << endl;
cout << myStock.symbol << ", " << myStock.price << ", " << myStock.numOfStocks << endl;
}

MyList.print();
return 0;
}

到目前为止,这还不是一个完整的实现,但它可以帮助您入门。例如,您需要向 LinkedList 添加一个复制构造函数和一个复制赋值运算符。类,根据 Rule of 3 .并实现你的 operator<<对于 stock .等等。

关于C++:我的链表尾部没有改变?我似乎无法让节点正确指向彼此?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58617150/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com