从文本文件中将整数读入一个简单的链表。然后对整数列表进行冒泡排序并读出到另一个文件中。现在我正在阅读主要内容,但我试图重载提取运算符以将其读入,但我不知道该怎么做。我的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;
}
};