Ordered Chain queue

Source: Internet
Author: User

Ordered Chain queue


  1. Compile header files

    Structqueue

    {

    Intnum; // represents data

    Inthigh; // priority 1111

    Structqueue * pNext; // store the address of the next node

    };

    Typedef structqueueQueue; // simplifies the queue

    Queue * init (Queue * queueHead); // Initialization

    Queue * EnQueue (Queue * queueHead, intnum, inthigh); // enter the Queue

    Queue * DeQueue (Queue * queueHead, Queue * pOut ); //

    Queue * freeall (Queue * queueHead); // clear

    Void sort (Queue * queueHead); // Queue with priority

    Voidprintfall (Queue * queueHead); // print all data, recursively

    Queue * insertEnQueue (Queue * queueHead, intnum, inthigh );

    1. 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;

      }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.