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

  • 本文关键字:节点 链表 C++ 改变 c++ linked-list
  • 更新时间 :
  • 英文 :


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

main:

#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://i.stack.imgur.com/v0OdT.jpg

linkedlist.print((输出在"请插入数字选项:3 "之后

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

您应该在LinkedList类内部分配新节点。调用方不负责为列表分配节点。

此外,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;
}

到目前为止,这还不是一个完整的实现,但它会让你开始。例如,您需要根据规则3向LinkedList类添加一个复制构造函数和一个复制赋值运算符。并为stock实现您的operator<<。等等。

相关内容

  • 没有找到相关文章

最新更新