I'm sorry I had to write all of the unnecessary stuff. It wouldn't accept all this code otherwise.
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.
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;
}
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.
I'm sorry I had to write all of this. It wouldn't accept all this code otherwise.
I will just give some feedback about your program.
I assume the intention was not make the call flow to main()-> linklist() recursive. The main() is calling main(), and linklist() is also calling main() and linklist()
if (choice == 1){
linklist();
main();
}
else if (choice == 2){
Binary_Search_Tree();
main();
}
I would rewrite this to something simple like:
while(true) {
linklist();
}
And in linklist() function remove all calls to main() and linklist(), and on choice 5 just:
} else if (choice == 5) {
exit(0); // Exit application
}
I would also try to find a better suitable name for the function 'linklist()'
In the C++ STL library equivalent to a linked list there is std::list ( https://en.cppreference.com/w/cpp/container/list ) - which is preferable to use - if the case is that you really need to use a linked-list - or is this just an exercise where you want to learn how to write your own linked-list?
That said, the advantage of a linked-list is constant time insert and delete, but very poor random access performance. Taking into account CPU cache, as the elements may not be on a linear chunk in memory, cache-misses may lead to poor performance, however this a big topic, that I will not get into here.
For your application I would not use a linked-list at all, I would just use std::vector ( https://en.cppreference.com/w/cpp/container/vector ) Just use std::sort ( https://en.cppreference.com/w/cpp/algorithm/sort ) to sort the elements in the list. I estimate that under ~100000 elements you will probably not have any performance issues, just using std::vector and std::sort
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.