我已经将一个 csv 文件读入我的 C++ 程序并对其应用链表。我想使用链接列表对 csv 中的订单 ID 列进行排序
I have read a csv file into my c++ program and applied linked list to it. I want to sort the order id column in the csv using linked list
对不起,我不得不写所有不必要的东西。否则它不会接受所有这些代码。
我已将一个 csv 文件读入我的 C++ 程序并对其应用链表。我想使用链表对 csv 中的订单 ID 列进行排序。
main.cpp
#include"iostream"
#include"string"
#include"fstream"
#include"node.h"
#include"linkedlist.h"
#include"bst.h"
#include"tree.h"
#include"string"
using namespace std;
BST tree;
linkedlist l;
Tree t;
void linklist();
void Binary_Search_Tree();
int main(){
int choice;
cout << "\nMenu:" << endl;
cout << "1 Linked List " << endl;
cout << "2 Binary Tree " << endl;
cin >> choice;
if (choice == 1){
linklist();
main();
}
else if (choice == 2){
Binary_Search_Tree();
main();
}
system("pause");
return 0;
}
void linklist(){
int choice;
string country;
string itemtype;
string saleschannel;
string orderdate;
string orderid;
string shipdate;
cout << "\nMenu:" << endl;
cout << "1 Add Node in Linked List " << endl;
cout << "2 Display in Linked List " << endl;
cout << "3 Search in Linked List " << endl;
cout << "4 Sort the Linked List " << endl;
cout << "5 Exit" << endl;
cin >> choice;
ifstream file("DataSet.csv");
if (choice == 1){
if (file.fail()){
cout << "File is not open:" << endl;
} else{
file.is_open();
while (!file.eof()){
getline(file, country, ',');
getline(file, itemtype, ',');
getline(file, saleschannel, ',');
getline(file, orderdate, ',');
getline(file, orderid, ',');
getline(file, shipdate, '\n');
l.insert(country, itemtype, saleschannel, orderdate, orderid, shipdate);
}
file.close();
}
linklist();
} else if (choice == 2){
l.display();
linklist();
} else if (choice == 3){
string id;
cout << "\nEnter The Order ID:" << endl;
cin.ignore();
getline(cin, id);
auto start = chrono::steady_clock::now();
l.search(id);
auto end = chrono::steady_clock::now();
cout << "Progress time in nanoseconds : " << chrono::duration_cast<chrono::nanoseconds> (end - start).count() << "ns " << endl;
cout << "Progress time in microseconds : " << chrono::duration_cast<chrono::microseconds>(end - start).count() << " µs " << endl;
cout << "Progress time in milliseconds : " << chrono::duration_cast<chrono::milliseconds>(end - start).count() << " ms " << endl;
cout << "progress time in seconds : " << chrono::duration_cast<chrono::seconds> (end - start).count() << " sec" << endl;
linklist();
} else if(choice==4){
l.sort(country);
l.display();
} else if (choice == 5){
main();
}
}
linkedlist.h
#pragma once
#include"iostream"
#include"node.h"
#include"string"
using namespace std;
class linkedlist{
private:
node*head;
node*tail;
public:
linkedlist();
void insert(string, string, string, string, string, string);
void search(string);
void sort(string);
void display();
};
linkedlist.cpp
#include"iostream"
#include"linkedlist.h"
#include"string"
using namespace std;
linkedlist::linkedlist(){
head = NULL;
tail = NULL;
}
void linkedlist::insert(string country, string item, string sales, string orderdate, string orderid, string shipdate){
if (head == NULL){
node*newnode = new node(country, item, sales, orderdate, orderid, shipdate);
head = newnode;
tail = newnode;
}
else {
node*newnode = new node(country, item, sales, orderdate, orderid, shipdate);
tail->setnext(newnode);
tail = newnode;
}
}
void linkedlist::display(){
node*temp;
temp = head;
while (temp != NULL){
cout << "Country Name Is:" << temp->getcountry() << endl;
cout << "Item Type Is:" << temp->getitemtype() << endl;
cout << "Channel Sales Is:" << temp->getsaleschanel() << endl;
cout << "Order Date Is:" << temp->getorderdate() << endl;
cout << "Order Id Is:" << temp->getorderid() << endl;
cout << "Ship Date Is:" << temp->getshipdate() << endl;
temp = temp->getnext();
}
}
void linkedlist::search(string orderid){
node*temp;
temp = head;
while (temp != NULL){
if (temp->getorderid() == orderid){
break;
} else{
temp = temp->getnext();
}
}
cout << "Country Name Is:" << temp->getcountry() << endl;
cout << "Item Type Is:" << temp->getitemtype() << endl;
cout << "Chanel Sales Is:" << temp->getsaleschanel() << endl;
cout << "Order Date Is:" << temp->getorderdate() << endl;
cout << "Order Id Is:" << temp->getorderid() << endl;
cout << "Ship Date Is:" << temp->getshipdate() << endl;
}
我已将一个 csv 文件读入我的 C++ 程序并对其应用链表。我想使用链表对 csv 中的订单 ID 列进行排序。
很抱歉我不得不写下所有这些。否则它不会接受所有这些代码。
我只会对您的程序提供一些反馈。
我假设其目的不是使调用流向 main()-> linklist() 递归。 main()是调用main(),linklist()也是调用main()和linklist()
if (choice == 1){
linklist();
main();
}
else if (choice == 2){
Binary_Search_Tree();
main();
}
我会将其重写为简单的内容,例如:
while(true) {
linklist();
}
并且在 linklist() 函数中删除对 main() 和 linklist() 的所有调用,并且在选项 5 上只是:
} else if (choice == 5) {
exit(0); // Exit application
}
我也会尝试为函数找到一个更合适的名称'linklist()'
在相当于链表的 C++ STL 库中有 std::list (https://en.cppreference.com/w/cpp/container/list) - 最好使用 - 如果你确实需要使用 linked-list - 或者这只是一个练习,您想学习如何编写自己的 linked-list?
也就是说,linked-list的优点是常量插入和删除,但随机访问性能很差。考虑到 CPU 缓存,因为元素可能不在内存中的线性块上, cache-misses 可能会导致性能不佳,但这是一个很大的话题,我不会在这里讨论。
对于您的申请,我根本不会使用 linked-list,我只会使用 std::vector (https://en.cppreference.com/w/cpp/container/vector)
只需使用 std::sort (https://en.cppreference.com/w/cpp/algorithm/sort) 对列表中的元素进行排序。
我估计在 ~100000 个元素下你可能不会有任何性能问题,只需使用 std::vector 和 std::sort
对不起,我不得不写所有不必要的东西。否则它不会接受所有这些代码。
我已将一个 csv 文件读入我的 C++ 程序并对其应用链表。我想使用链表对 csv 中的订单 ID 列进行排序。
main.cpp
#include"iostream"
#include"string"
#include"fstream"
#include"node.h"
#include"linkedlist.h"
#include"bst.h"
#include"tree.h"
#include"string"
using namespace std;
BST tree;
linkedlist l;
Tree t;
void linklist();
void Binary_Search_Tree();
int main(){
int choice;
cout << "\nMenu:" << endl;
cout << "1 Linked List " << endl;
cout << "2 Binary Tree " << endl;
cin >> choice;
if (choice == 1){
linklist();
main();
}
else if (choice == 2){
Binary_Search_Tree();
main();
}
system("pause");
return 0;
}
void linklist(){
int choice;
string country;
string itemtype;
string saleschannel;
string orderdate;
string orderid;
string shipdate;
cout << "\nMenu:" << endl;
cout << "1 Add Node in Linked List " << endl;
cout << "2 Display in Linked List " << endl;
cout << "3 Search in Linked List " << endl;
cout << "4 Sort the Linked List " << endl;
cout << "5 Exit" << endl;
cin >> choice;
ifstream file("DataSet.csv");
if (choice == 1){
if (file.fail()){
cout << "File is not open:" << endl;
} else{
file.is_open();
while (!file.eof()){
getline(file, country, ',');
getline(file, itemtype, ',');
getline(file, saleschannel, ',');
getline(file, orderdate, ',');
getline(file, orderid, ',');
getline(file, shipdate, '\n');
l.insert(country, itemtype, saleschannel, orderdate, orderid, shipdate);
}
file.close();
}
linklist();
} else if (choice == 2){
l.display();
linklist();
} else if (choice == 3){
string id;
cout << "\nEnter The Order ID:" << endl;
cin.ignore();
getline(cin, id);
auto start = chrono::steady_clock::now();
l.search(id);
auto end = chrono::steady_clock::now();
cout << "Progress time in nanoseconds : " << chrono::duration_cast<chrono::nanoseconds> (end - start).count() << "ns " << endl;
cout << "Progress time in microseconds : " << chrono::duration_cast<chrono::microseconds>(end - start).count() << " µs " << endl;
cout << "Progress time in milliseconds : " << chrono::duration_cast<chrono::milliseconds>(end - start).count() << " ms " << endl;
cout << "progress time in seconds : " << chrono::duration_cast<chrono::seconds> (end - start).count() << " sec" << endl;
linklist();
} else if(choice==4){
l.sort(country);
l.display();
} else if (choice == 5){
main();
}
}
linkedlist.h
#pragma once
#include"iostream"
#include"node.h"
#include"string"
using namespace std;
class linkedlist{
private:
node*head;
node*tail;
public:
linkedlist();
void insert(string, string, string, string, string, string);
void search(string);
void sort(string);
void display();
};
linkedlist.cpp
#include"iostream"
#include"linkedlist.h"
#include"string"
using namespace std;
linkedlist::linkedlist(){
head = NULL;
tail = NULL;
}
void linkedlist::insert(string country, string item, string sales, string orderdate, string orderid, string shipdate){
if (head == NULL){
node*newnode = new node(country, item, sales, orderdate, orderid, shipdate);
head = newnode;
tail = newnode;
}
else {
node*newnode = new node(country, item, sales, orderdate, orderid, shipdate);
tail->setnext(newnode);
tail = newnode;
}
}
void linkedlist::display(){
node*temp;
temp = head;
while (temp != NULL){
cout << "Country Name Is:" << temp->getcountry() << endl;
cout << "Item Type Is:" << temp->getitemtype() << endl;
cout << "Channel Sales Is:" << temp->getsaleschanel() << endl;
cout << "Order Date Is:" << temp->getorderdate() << endl;
cout << "Order Id Is:" << temp->getorderid() << endl;
cout << "Ship Date Is:" << temp->getshipdate() << endl;
temp = temp->getnext();
}
}
void linkedlist::search(string orderid){
node*temp;
temp = head;
while (temp != NULL){
if (temp->getorderid() == orderid){
break;
} else{
temp = temp->getnext();
}
}
cout << "Country Name Is:" << temp->getcountry() << endl;
cout << "Item Type Is:" << temp->getitemtype() << endl;
cout << "Chanel Sales Is:" << temp->getsaleschanel() << endl;
cout << "Order Date Is:" << temp->getorderdate() << endl;
cout << "Order Id Is:" << temp->getorderid() << endl;
cout << "Ship Date Is:" << temp->getshipdate() << endl;
}
我已将一个 csv 文件读入我的 C++ 程序并对其应用链表。我想使用链表对 csv 中的订单 ID 列进行排序。
很抱歉我不得不写下所有这些。否则它不会接受所有这些代码。
我只会对您的程序提供一些反馈。
我假设其目的不是使调用流向 main()-> linklist() 递归。 main()是调用main(),linklist()也是调用main()和linklist()
if (choice == 1){
linklist();
main();
}
else if (choice == 2){
Binary_Search_Tree();
main();
}
我会将其重写为简单的内容,例如:
while(true) {
linklist();
}
并且在 linklist() 函数中删除对 main() 和 linklist() 的所有调用,并且在选项 5 上只是:
} else if (choice == 5) {
exit(0); // Exit application
}
我也会尝试为函数找到一个更合适的名称'linklist()'
在相当于链表的 C++ STL 库中有 std::list (https://en.cppreference.com/w/cpp/container/list) - 最好使用 - 如果你确实需要使用 linked-list - 或者这只是一个练习,您想学习如何编写自己的 linked-list?
也就是说,linked-list的优点是常量插入和删除,但随机访问性能很差。考虑到 CPU 缓存,因为元素可能不在内存中的线性块上, cache-misses 可能会导致性能不佳,但这是一个很大的话题,我不会在这里讨论。
对于您的申请,我根本不会使用 linked-list,我只会使用 std::vector (https://en.cppreference.com/w/cpp/container/vector) 只需使用 std::sort (https://en.cppreference.com/w/cpp/algorithm/sort) 对列表中的元素进行排序。 我估计在 ~100000 个元素下你可能不会有任何性能问题,只需使用 std::vector 和 std::sort