9.文件中有一組整數(shù),要求排序后輸出到另一個(gè)文件中
答案:
#i nclude
#i nclude
using namespace std;
void Order(vector& data)//bubble sort
{
int count = data.size() ;
int tag = false ; //設(shè)置是否需要繼續(xù)冒泡的標(biāo)志位
for ( int i = 0 ; i < count ; i++)
{
for ( int j =0 ; j < count - i - 1 ; j++)
{
if ( data[j] > data[j+1])
{
tag = true ;
int temp = data[j] ;
data[j] = data[j+1] ;
data[j+1] = temp ;
}
}
if ( !tag )
break ;
}
}
void main( void )
{
vectordata;
ifstreamin("c:\\data.txt");
if ( !in)
{
cout<<"file error!";
exit(1);
}
int temp;
while (!in.eof())
{
in>>temp;
data.push_back(temp);
}
in.close(); //關(guān)閉輸入文件流
Order(data);
ofstream out("c:\\result.txt");
if ( !out)
{
cout<<"fileerror!";
exit(1);
}
for ( i = 0 ; i < data.size() ; i++)
out< 10. 鏈表題:一個(gè)鏈表的結(jié)點(diǎn)結(jié)構(gòu)
struct Node
{
int data ;
Node *next ;
};
typedef struct Node Node ;
(1)已知鏈表的頭結(jié)點(diǎn)head,寫一個(gè)函數(shù)把這個(gè)鏈表逆序 ( Intel)
Node * ReverseList(Node*head) //鏈表逆序
{
if ( head == NULL || head->next == NULL )
returnhead;
Node *p1 = head ;
Node *p2 = p1->next ;
Node *p3 =p2->next ;
p1->next = NULL ;
while ( p3 != NULL )
{
p2->next = p1 ;
p1 = p2 ;
p2 = p3 ;
p3 = p3->next ;
}
p2->next = p1 ;
head = p2 ;
return head ;
}
|
|
||
|
|