[英]import from file c++ & insertion sort
我正在做一個需要這樣做的項目:
在C ++中,創建一個類,該類是整數的雙鏈表。
此類應具有以下內容:-常規插入函數(push_back,push_front)-常規刪除函數(pop_back,pop_front)-3排序函數-插入排序-合並排序-氣泡排序
排序方法應打印出處理時間(低至毫秒或毫秒)和使用的排序(例如,氣泡排序完成后,氣泡排序-100ms)。
創建一個程序,從文件中讀取一堆數字,然后創建您的鏈表(每次運行創建三個表)。 然后,程序應使用每種方法進行排序。 顯示三輪的結果,一輪列出100000個數字,一輪列出10000,一輪列出1000個數字。
我一直試圖導入具有100000個整數的第一個文件(file1.txt),並將其保存到我的雙向鏈接列表中。 關於如何做到這一點的任何建議都將如此出色。 接下來,我正在嘗試插入排序算法,到目前為止,我所要做的是對數組而不是雙重鏈表的插入排序。 如果您可以瀏覽我的代碼並給我任何建議,那將是很好的。 我從來沒有用C ++編寫過代碼,只曾用Java編寫過代碼,這對我來說是一項挑戰!
#include<iostream>
//for time elapsed
#include <chrono>
using namespace std;
template <class T>
class doublylinkedlist
{
private :
struct node
{
T data;
node *prev;
node *next;
};
node *head;
node *tail;
public :
doublylinkedlist()
{
head=tail=NULL;
}
void createlist(T[] , int);
void pushfirst(T);
void pushlast(T);
void pushafter(T,T);
void pop(T);
void displayforward();
void displaybackward();
};
//creating doubly linked list
template<class T>
void doublylinkedlist<T>::createlist(T x[], int n) //n = size
{
node *q;
node *p=new node; //create first node
p->data=x[0];
p->next=NULL;
p->prev=NULL;
for(int i=1;i<n;i++)
{
q=p;
p=p->next=new node;
p->data=x[i];
p->next=NULL;
p->prev=q;
}
tail=p;
}
// Inserting new node at start of doubly linked list
template<class T>
void doublylinkedlist<T>::pushfirst(T item)
{
node *p=new node;
p->data=item;
p->prev=NULL;
head->prev=p;
}
//Inserting new node at last of Double Linkedlist
template<class T>
void doublylinkedlist<T>::pushlast(T item)
{
node *p=new node;
p->data=item;
p->prev=tail;
p->next=NULL;
tail=p;
}
//deleting item from double linked list
template<class T>
void doublylinkedlist<T>::pop(T item)
{
if(head==NULL)
{
cout<<"This list is empty!"<<endl;
return;
}
if(head->data==item)
{
head=head->next;
head->prev=NULL;
return;
}
if(tail->data==item)
{
tail=tail->prev;
tail->next=NULL;
return;
}
node *p=head->next;
while(p!=NULL)
{
if(p->data==item)
break;
p=p->next;
}
if(p==NULL)
{
cout<<item<<"not found "<<endl;
return;
}
(p->prev)->next=p->next;
(p->next)->prev=p->prev;
return;
}
//displaying list elements in forward direction
template<class T>
void doublylinkedlist<T>::displayforward()
{
node *p=head;
cout<<"\n Doubly linked list (Forward)";
while(p!=NULL)
{
cout<<p->data<<"";
p=p->next;
}
}
//displaying list elements in reverse direction
template<class T>
void doublylinkedlist<T>::displaybackward()
{
node *p=tail;
cout<<"\n Doubly linked list (Backward)";
while(p!=NULL)
{
cout<<p->data<<"";
p=p->prev;
}
}
//insertion sort function
void doublylinkedlist<T>::insertionSort(T x[], int n)
{
auto beg = std::chrono::high_resolution_clock::now();
int i, j ,tmp;
for (i = 1; i < n; i++)
{
j = i;
while (j > 0 && x[j - 1] > x[j])
{
tmp = x[j];
x[j] = x[j - 1];
x[j - 1] = tmp;
j--;
}
printArray(x,5);
}
auto end = std::chrono::high_resolution_clock::now();
std::cout << std::chrono::duration_cast<std::chrono::milliseconds>(end - beg).count() << std::endl;
std::cin.get();
return 0;
}
int main()
{
ifstream myfile ("file1.txt");
if (myfile.is_open())
{
//i need to insert the file into the doubly linked list here.
//file.txt is has 100000 integers
doublylinkedlist<int> firstList;
myfile.close();
}
else cout << "Unable to open file";
return 0;
//replace this with input from file
//10000 numbers
doublylinkedlist<int> secondList;
//1000 numbers
doublylinkedlist<int> thirdList;
//example of what to run
firstList.createlist(x,2);
firstList.pushfirst(22);
firstList.pushlast(55);
firstList.pushafter(66,33);
firstList.pop(22);
firstList.pop(55);
firstList.pop(66);
return 0;
}
成功打開文件后,您需要添加一個循環來執行以下操作:
read a number from the file into temp variable
firstList.pushlast(temp)
關於排序,我相信您應該移動列表中的節點(而不是在節點之間復制值-數組中的方式)以使列表排序。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.