简体   繁体   English

从文件c ++导入并插入排序

[英]import from file c++ & insertion sort

I am working on a project that needs to do this: 我正在做一个需要这样做的项目:

In C++ create a class that is a doubly linked list of integers. 在C ++中,创建一个类,该类是整数的双链表。

This class should have the following: -Normal insertion functions(push_back, push_front) -Normal deletion functions(pop_back, pop_front) -3 Sorting functions -Insertion Sort -Merge Sort -Bubble Sort 此类应具有以下内容:-常规插入函数(push_back,push_front)-常规删除函数(pop_back,pop_front)-3排序函数-插入排序-合并排序-气泡排序

The sorting methods should print out the processing time(down to either micro or milliseconds), and the sort used(eg after bubble sort finishes, Bubble Sort- 100ms). 排序方法应打印出处理时间(低至毫秒或毫秒)和使用的排序(例如,气泡排序完成后,气泡排序-100ms)。

Create a program that read's in a bunch of numbers from file and creates your linked list(create three lists to for each run). 创建一个程序,从文件中读取一堆数字,然后创建您的链表(每次运行创建三个表)。 The program should then sort with each method. 然后,程序应使用每种方法进行排序。 Show results for three runs, one with a list of 100000 numbers, one with 10000, one with 1000 numbers. 显示三轮的结果,一轮列出100000个数字,一轮列出10000,一轮列出1000个数字。

I am stuck on trying to import my first file (file1.txt) that has 100000 integers and saving them into my doubly linked list. 我一直试图导入具有100000个整数的第一个文件(file1.txt),并将其保存到我的双向链接列表中。 Any advice on how to do that would be so great. 关于如何做到这一点的任何建议都将如此出色。 Next, I am trying to work on the insertion sort algorithm, and what I have so far is insertion sort for an array and not doubly linked list. 接下来,我正在尝试插入排序算法,到目前为止,我所要做的是对数组而不是双重链表的插入排序。 If you could scan through my code and give me any advice that would be great. 如果您可以浏览我的代码并给我任何建议,那将是很好的。 I have never coded in C++, only in java, and this assignment is challenging for me! 我从来没有用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;
}

After successfully opening the file, you need to add a loop doing: 成功打开文件后,您需要添加一个循环来执行以下操作:

read a number from the file into temp variable
firstList.pushlast(temp)

Regarding sorting, I believe you are expected to move the nodes in the list (instead of copying values between the nodes - the way its done in array) to make the list sorted. 关于排序,我相信您应该移动列表中的节点(而不是在节点之间复制值-数组中的方式)以使列表排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM