从文本文件中将整数读入一个简单的链表。然后bubblesort整数列表并读出另一个文件。现在我正在阅读主要内容,但我正在尝试重载提取操作符以将其读入并且我不确定如何去做。我的Bubblesort
功能也导致了很多问题。它告诉我函数不能被重载,节点标识符在其他事情中是未声明的。任何帮助将不胜感激
主档
#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;
}
}
标头文件
#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;
}
};
答案 0 :(得分:0)
Bubblesort()
两次:首先在头文件中为void Bubblesort()
,然后在主文件中只为Bubblesort()
(这至少会给您一个警告,它是被认为是int Bubblesort()
)。你不能仅仅在返回值上重载函数,因此错误。node
的类型,但您尚未在任何地方声明或定义它。