我已经将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 id 排序 c++ 程序 我的 读取 应用 文件      更新时间:2024-09-26

很抱歉我不得不写所有不必要的东西。否则它不会接受所有这些代码。

我已经将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();
};

链接列表.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((->链表((递归。main((正在调用main((,链表((也在调用main(和链表((

if (choice == 1){
linklist();
main();
}
else if (choice == 2){
Binary_Search_Tree();
main();
}

我会把它重写成一个简单的东西,比如:

while(true) {
linklist();
}

在linklist((函数中,删除对main((和linklist(

}  else if (choice == 5) {
exit(0); // Exit application
}

我还想为函数"linklist(("找到一个更合适的名称

在相当于链表的C++STL库中,有std::list(https://en.cppreference.com/w/cpp/container/list)-如果你真的需要使用链表,那么最好使用哪个?或者这只是你想学习如何编写自己的链表的一个练习吗?

也就是说,链表的优点是插入和删除时间恒定,但随机访问性能非常差。考虑到CPU缓存,因为元素可能不在内存中的线性块上,缓存未命中可能会导致性能不佳,但这是一个大主题,我在这里不谈。

对于您的应用程序,我根本不会使用链表,我只会使用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