Google Answers Logo
View Question
 
Q: Programing -> DataStructures -> DblLinkedList ->C++ Quick sort on a linked list ( No Answer,   1 Comment )
Question  
Subject: Programing -> DataStructures -> DblLinkedList ->C++ Quick sort on a linked list
Category: Computers > Programming
Asked by: tabalas-ga
List Price: $10.00
Posted: 28 Aug 2006 09:44 PDT
Expires: 27 Sep 2006 09:44 PDT
Question ID: 760182
I have a Double  Linked list structure
typedef struct _tagOneRawDataItem
{
	string  mData1;
        ..
        ..
        OtherData oData2;
        ..
        ..
	// Pointers
	struct _tagOneRawDataItem *pPrev;
	struct _tagOneRawDataItem *pNext;
} OneRawDataItem;

I would like a C++ program/solution  that can quick sort the
datastructure with the following restrictions:
1. Swapping of data is not acceptable ( As the data could get complicated)
   or node-swapping should be implemented using pointer indirections only.
2. The linked list should be sorted by the element - mData1 (primary key)
3. Usage of temp variables should be kept to the minimum

Regards
BK
Answer  
There is no answer at this time.

Comments  
Subject: Re: Programing -> DataStructures -> DblLinkedList ->C++ Quick sort on a linked list
From: xielong-ga on 14 Sep 2006 02:03 PDT
 
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;
}

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy