We assume that a linked list class exists for processing Integer data, called CLinkedList_Integer, with nodes storing integer data called CNode_Integer. This might be defined in a header file as follows:
class CNode_Integer
{
private:
CN ode_I nteger * oNext;
CNode_Integer * oPrevious;
int nData;
public:
CNode_Integer ();
~CNode_Integer();
int GetData();
void SetData(int nData);
CNode_Integer * GetNext();
CNode_Integer * GetPrevious();
void SetNext(CNode_Integer * oNext);
void SetPrevious(CNode_Integer * oPrevious);
};
class CLinkedList_Integer
{
privat e:
C Node_Integer * oHead;
public:
CLinkedList_I ntege r();
~CLinkedList_Integer();
CNod e_Integer * InsertNode(CNode_Integer * oNode);
CNode_Integer * AddNode(CNode_Integer * oNode);
CNode_Integer * SwapNodes(CNode_Integer * oNodeLeft, CNode_Integer * oNodeRight);
int CompareNodes(CNode_Integer * oNodeLeft, CNode_Integer * oNodeRight);
CNode_Integer * RemoveNode(CNode_Integer * oNode);
};
The 4 functions that we will need for our sorting are:
* InsertNode - traverses the list and inserts the node at the appropriate place;
* AddNode - adds a node to the head of the list;
* SwapNodes - swaps two nodes;
* CompareNodes - returns strcmp compatible codes -1, 0,
The last function in the list will return -1 if oNodeLeft is 'less' than oNodeRight, 0 if they are equal, and 1 if oNodeLeft is 'more' than oNodeRight. This mimics the string library, string.h, where the same codes are returned for strcmp and strcmpi string comparison.
For more details , Please do visit the source site :
http://computer-programmi ng-tu torials.suite101.com/article.c fm/tw o_ways_to_sort_a_c_linked_list
Answered by
Yash
, an ibibo Master,
at
9:27 AM on July 14, 2008