The code:
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
//OtherData
struct _tagOtherData
{
string s ;
long i ;
};
typedef struct _tagOneRawDataItem
{
string mData1;
_tagOtherData otherdata ;
// Pointers
struct _tagOneRawDataItem *pPrev;
struct _tagOneRawDataItem *pNext;
//some fuctions
_tagOneRawDataItem ();
_tagOneRawDataItem ( string data ) ;
//less-than if the item is less than the other returns true
bool operator < ( _tagOneRawDataItem const &other ) ;
} OneRawDataItem;
typedef struct _tagDLinkRaw
{
OneRawDataItem *mpHead ; //point to the head of the link
int mSize ; // the link's size
//some fuctions
_tagDLinkRaw ( int size ) ;
//get the ith item
OneRawDataItem * Getith ( int i ) ;
//swip swip the ith item with the jth .
void Swip ( int i , int j ) ;
//QuickSort fuction
void Qsort ( int lo , int hi ) ;
void Qsort () ;
//print
void print ();
} DLinkRaw ;
_tagOneRawDataItem::_tagOneRawDataItem ()
{
mData1 = "" ;
pPrev = NULL ;
pNext = NULL ;
}
_tagOneRawDataItem::_tagOneRawDataItem ( string data )
{
mData1 = data ;
pPrev = NULL ;
pNext = NULL ;
}
bool _tagOneRawDataItem::operator < ( _tagOneRawDataItem const &other )
{
return ( this->mData1 < other.mData1 ) ;
}
_tagDLinkRaw::_tagDLinkRaw ( int size )
{
mSize = size ;
string key = "" ;
mpHead = new OneRawDataItem ;
if ( 0 == size ) return ;
cout<<"Please input the key of the 1st item :" ;
cin>>key ;
mpHead->pPrev = mpHead ;
mpHead->pNext = new OneRawDataItem ( key ) ;
mpHead->pNext->pPrev = mpHead ;
if ( 1 == size ) return ;
for ( size = 1 ; size < mSize ; size++ ) {
cout<<"Please input the key of the "<< size + 1 <<"th item :" ;
cin>>key ;
mpHead->pNext->pPrev = new OneRawDataItem ( key ) ;
mpHead->pNext->pPrev->pNext = mpHead->pNext ;
mpHead->pNext->pPrev->pPrev = mpHead ;
mpHead->pNext = mpHead->pNext->pPrev ;
}
}
OneRawDataItem * DLinkRaw::Getith ( int i )
{
if ( ( i < 1 ) || ( i > mSize ) ) {
return NULL ;
}
OneRawDataItem *p = mpHead ;
for ( int j = 0 ; j != i ; j++ ) {
p = p->pNext ;
}
return p ;
}
void DLinkRaw::print()
{
for ( int i = 1 ; i <= mSize ; i++ ) {
cout << "The " << i << "th item :" << Getith ( i )->mData1 <<endl ;
}
}
void DLinkRaw::Swip ( int i , int j )
{
OneRawDataItem *p1 , *p2 , *p3 = NULL ;
if ( i == j ) return ;
if ( i > j ) {
int temp = i ;
i = j ;
j = i ;
}
p1 = Getith ( i ) ;
p2 = Getith ( j ) ;
p3 = p2->pNext ;
if ( j != mSize ) {
p2->pNext->pPrev = p1 ;
}
if ( i == ( j -1 ) ) {
p1->pPrev->pNext = p2 ;
p1->pNext = p2->pNext ;
p2->pNext = p1 ;
p2->pPrev = p1->pPrev ;
p1->pPrev = p2 ;
}
else {
p1->pNext->pPrev = p2 ;
p2->pPrev->pNext = p1 ;
p1->pPrev->pNext = p2 ;
p2->pNext = p1->pNext ;
p1->pNext = p3 ;
p3 = p1->pPrev ;
p1->pPrev = p2->pPrev ;
p2->pPrev = p3 ;
}
}
void DLinkRaw::Qsort ()
{
Qsort ( 1 , mSize ) ;
}
void DLinkRaw::Qsort ( int lo , int hi )
{
if ( lo < hi ) {
int i = lo , j = hi , m = ( lo + hi ) /2 ;
do {
while ( *Getith ( i ) < *Getith ( m ) ) i++ ;
while ( *Getith ( m ) < *Getith ( j ) ) j-- ;
if ( i <= j ) {
if ( m == i ) m = j ;
else if ( m == j ) m = i ;
Swip ( i , j ) ;
i++ , j-- ;
}
}while ( i <= j ) ;
Qsort ( lo , j ) ;
Qsort ( i , hi ) ;
}
}
int main(int argc, char *argv[])
{
DLinkRaw dlink ( 8 ) ;
cout<<"Before sorting:"<<endl;
dlink.print();
dlink.Qsort();
cout<<endl<<"After sorting:"<<endl;
dlink.print();
system("PAUSE");
return EXIT_SUCCESS;
} |