Code for queue implementation
# Include "Queue. h"
# Include
# Include
// Initialization
Queue * init (Queue * queueHead)
{
ReturnNULL;
}
// Enter the team
Queue * EnQueue (Queue * queueHead, intnum, inthigh)
{
// Allocate memory
Queue * pnewnode = (Queue *) malloc (sizeof (Queue ));
Pnewnode-> num = num;
Pnewnode-> high = high;
Pnewnode-> pNext = NULL;
If (queueHead = NULL)
{
QueueHead = pnewnode;
}
Else
{
Queue * p = queueHead;
While (p-> pNext! = NULL)
{
P = p-> pNext;
}
// Determine the position to insert
// Insert, Which is Insert at the end
P-> pNext = pnewnode;
}
ReturnqueueHead;
}
// Team out
Queue * DeQueue (Queue * queueHead, Queue * pOut)
{
If (queueHead = NULL)
{
ReturnNULL;
}
Else
{
// This is equivalent
POut-> num = queueHead-> num;
POut-> high = pOut-> high;
Queue * pTemp = queueHead;
// Record the address to be deleted
QueueHead = queueHead-> pNext;
// Release a node
Free (pTemp );
ReturnqueueHead;
}
}
/// Queue by priority
// Void sort (Queue * queueHead)
//{
// If (queueHead = NULL | queueHead-> pNext = NULL)
//{
// Return;
//}
// Else
//{
// For (Queue * p1 = queueHead; p1! = NULL; p1 = p1-> pNext)
//{
// For (Queue * p2 = queueHead; p2! = NULL; p2 = p2-> pNext)
//{
// If (p1-> high> p2-> high)
//{
// Queue temp;
// Temp. num = p1-> num;
// P1-> num = p2-> num;
// P2-> num = temp. num;
//
// Temp. high = p1-> high;
// P1-> high = p2-> high;
//// Exchange node data
// P2-> high = temp. high;
//}
//}
//}
//}
//}
// Print all data, recursively
Voidprintfall (Queue * queueHead)
{
If (queueHead = NULL)
{
Return;
}
Else
{
Printf ("% d, % d, % p, % p \ n", queueHead-> num, queueHead-> high, queueHead, queueHead-> pNext );
Printfall (queueHead-> pNext );
}
}
Queue * insertEnQueue (Queue * queueHead, intnum, inthigh)
{
// Allocate memory
Queue * pnewnode = (Queue *) malloc (sizeof (Queue ));
Pnewnode-> num = num;
Pnewnode-> high = high;
// The node is empty.
If (queueHead = NULL)
{
Pnewnode-> pNext = NULL;
QueueHead = pnewnode;
ReturnqueueHead;
}
Else
{
// Insert the header
If (pnewnode-> high> queueHead-> high)
{
// Insert the header
Pnewnode-> pNext = queueHead;
// Point to this node
QueueHead = pnewnode;
ReturnqueueHead;
}
Else
{
// Header Node
Queue * p = queueHead;
While (p-> pNext! = NULL)
{
P = p-> pNext;
}
// Loop to the end
If (pnewnode-> high <= p-> high)
{
P-> pNext = pnewnode;
Pnewnode-> pNext = NULL;
ReturnqueueHead;
}
Else
{
Queue * p1, * p2;
P1 = p2 = NULL; // avoid wild pointer
P1 = queueHead; // header Node
While (p1-> pNext! = NULL)
{
P2 = p1-> pNext;
If (p1-> high> = pnewnode-> high & p2-> high High)
{
Pnewnode-> pNext = p2;
P1-> pNext = pnewnode; // insert
Break;
}
P1 = p1-> pNext;
}
ReturnqueueHead;
}
}
}
}
3. Compile the main function
# Include "Queue. h"
# Include
# Include
Intmain (intargc, char * argv [])
{
// Create a header Node
Queue * phead = NULL;
// Initialization
Phead = init (phead );
Phead = insertEnQueue (phead, 1, 1 );
Printfall (phead );
Phead = insertEnQueue (phead, 2, 12 );
Printfall (phead );
Phead = insertEnQueue (phead, 3, 3 );
Printfall (phead );
Phead = insertEnQueue (phead, 4, 14 );
Printfall (phead );
Phead = insertEnQueue (phead, 5, 5 );
Printfall (phead );
Phead = insertEnQueue (phead, 6, 16 );
Printfall (phead );
Phead = insertEnQueue (phead, 6, 0 );
Printfall (phead );
Phead = insertEnQueue (phead, 7, 0 );
Printfall (phead );
Phead = insertEnQueue (phead, 8, 0 );
Printfall (phead );
Phead = insertEnQueue (phead, 9, 1 );
Printfall (phead );
Phead = insertEnQueue (phead, 10, 0 );
Printfall (phead );
Phead = insertEnQueue (phead, 11, 16 );
Printfall (phead );
Phead = insertEnQueue (pheactive, 111, 19 );
Printfall (phead );
Printf ("print the sorted chain queue: \ n ");
Printfall (phead );
Getchar ();
Return 0;
}