简体   繁体   English

从文本文件中将整数读入一个简单的链表。 然后对整数列表进行冒泡排序并读出到另一个文件

[英]Read integers into a simple linked list from text file. Then bubblesort the list of integers and read out to another file

Read integers into a simple linked list from text file.从文本文件中将整数读入一个简单的链表。 Then bubblesort the list of integers and read out to another file.然后对整数列表进行冒泡排序并读出到另一个文件。 Right now I am reading into the main but I am trying to overload the extraction operator to read it in and I am not sure how to go about that.现在我正在阅读主要内容,但我试图重载提取运算符以阅读它,但我不知道如何去做。 My Bubblesort function is also causing alot of issues.我的Bubblesort功能也引起了很多问题。 Its telling me the function cannot be overloaded and the node identifier is undeclared amongst other things.它告诉我该函数不能重载,并且节点标识符未声明等。 Any help would be greatly appreciated任何帮助将不胜感激

Main file主文件

#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <fstream>
#include "bubble.h"

using namespace std;
struct nodeType
{
    int info;
    nodeType* link;
};

node *head_ptr = NULL;
void Display();
void list_clear(nodeType*& head_ptr);
void list_copy(const nodeType* source_ptr, nodeType*&head_ptr, nodeType*&tail_ptr);
Bubblesort();


int main()
{
    ifstream datld;
    ofstream outld;

    Bubble D3;

    datld.open ("infile2.txt");
    if (!datld)
    {
        cout << "failure to open data.txt" << endl;
        system ("pause");
        return 1;
    }

    datld >> D3;

    while(datld)
    {
        cout << D3<< endl;
        datld >> D3;
    }

    system("pause");
    return 0;

    Bubblesort();
}


void Bubblesort()
{
    node* curr = head_ptr;
    int count = 0;
    while(curr!=NULL)
    {
        count++;
        curr = curr->NEXT;
    }
    for(int i = count ; i > 1 ; i-- )
    {
        node *temp, *swap1;
        swap1 = HEAD;
        for(int j = 0 ; j < count-1 ; j++ )
        {
            if(swap1->DATA > swap1->NEXT->DATA)
            {
                node *swap2 = swap1->NEXT;
                swap1->NEXT = swap2->NEXT;
                swap2->NEXT = swap1;
                if(swap1 == HEAD)
                {
                    HEAD = swap2;
                    swap1 = swap2;
                }
                else
                {
                    swap1 = swap2;
                    temp->NEXT = swap2;
                }
            }
            temp = swap1;
            swap1 = swap1->NEXT;
        }
    }
}


void list_clear(nodeType*& head_ptr)
//Library facilities used:cstdlib
{
    nodeType * removeptr;
    while(head_ptr!=NULL)
    {
        removeptr=head_ptr;
        head_ptr=head_ptr->link;
        delete removeptr;
    }
}

void list_copy(const nodeType* source_ptr, nodeType*&head_ptr, nodeType*&tail_ptr)
{
    nodeType* temp;// to allocate new nodes
    head_ptr=NULL;
    tail_ptr=NULL;

    if(source_ptr==NULL)
        return;

    head_ptr=new nodeType;
    head_ptr->link=NULL;
    head_ptr->info=source_ptr->info;
    tail_ptr=head_ptr;
    source_ptr=source_ptr->link;

    while(source_ptr!=NULL)
    {
        temp = new nodeType;
        temp->link=NULL;
        temp->info =source_ptr-> info;
        tail_ptr->link=temp;
        tail_ptr = tail_ptr->link;
        source_ptr = source_ptr->link;
    }
}

Header file头文件

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>
using namespace std;

class Bubble
{
private:
    int manynodes;
public:
    Bubble() { }

    void Bubblesort();

    friend ostream &operator<<( ostream &output, const Bubble &D )
    {
        output << D.manynodes;
        return output;
    }

    friend istream &operator>>( istream  &input, Bubble &D )
    {
        input >> D.manynodes;
        return input;
    }
};
  • Your extraction operator looks fine.您的提取操作员看起来不错。 I don't know if it really does what you need, but that's another issue.我不知道它是否真的满足您的需求,但这是另一个问题。
  • You are declaring function Bubblesort() twice: first in the header file as void Bubblesort() , then in the main file as just Bubblesort() (this should at least give you a warning that it is considered to mean int Bubblesort() ).您将函数Bubblesort()声明两次:首先在头文件中作为void Bubblesort() ,然后在主文件中作为Bubblesort() (这至少应该给您一个警告,它被认为是int Bubblesort() ) . You cannot overload a function just on the return value, hence the error.您不能仅在返回值上重载函数,因此会出现错误。
  • Indeed, you are using a type called node in several places, but you have not declared nor defined it anywhere.确实,您在多个地方使用了一个名为node的类型,但是您没有在任何地方声明或定义它。

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

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