Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. I need a two level priority queue

I need a two level priority queue

Scheduled Pinned Locked Moved C / C++ / MFC
data-structurescareer
8 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 10395751
    wrote on last edited by
    #1

    Hey, I need a C code which builds the printer job queue. However, the printer job queue is built as a two level priority queue.

    _ 1 Reply Last reply
    0
    • U User 10395751

      Hey, I need a C code which builds the printer job queue. However, the printer job queue is built as a two level priority queue.

      _ Offline
      _ Offline
      _Superman_
      wrote on last edited by
      #2

      Looks like a single linked list. Here is a program for a single link list - Singly linked list[^] You will need to modify the program slightly to cater to your requirement. The given program only uses an integer variable. Instead, you will need to make 2 checks on the userType and the pages.

      «_Superman_»  _I love work. It gives me something to do between weekends.

      _Microsoft MVP (Visual C++) (October 2009 - September 2013)

      Polymorphism in C

      U 1 Reply Last reply
      0
      • _ _Superman_

        Looks like a single linked list. Here is a program for a single link list - Singly linked list[^] You will need to modify the program slightly to cater to your requirement. The given program only uses an integer variable. Instead, you will need to make 2 checks on the userType and the pages.

        «_Superman_»  _I love work. It gives me something to do between weekends.

        _Microsoft MVP (Visual C++) (October 2009 - September 2013)

        Polymorphism in C

        U Offline
        U Offline
        User 10395751
        wrote on last edited by
        #3

        I used this code.

        void enqueue(char type, int p){
        struct node *newPtr, *prevPtr, *presentPtr,*tempPresent;

        //Allocating memory for new member
        newPtr = malloc(sizeof(struct node));
        
        //Checking allocation. If there is not room for new member in RAM then it gives an error
        if(newPtr != NULL){
        	newPtr->pages = p;
        	newPtr->userType = type;
        	newPtr->nextPtr = NULL;
        	
        	prevPtr = NULL;
        	presentPtr = front;
        	
        	while(presentPtr != NULL && type > presentPtr->userType){
        		prevPtr = presentPtr;
        		presentPtr = presentPtr->nextPtr;
        	}
        	//Checking if new member is the first element of the linked list or not
        	if(prevPtr == NULL){
        		newPtr->nextPtr = front;
        		front = newPtr;
        	}
        	else{
        		prevPtr->nextPtr=newPtr;
        		newPtr->nextPtr=presentPtr;
        	}
        }//If allocation fails throws a message to the user
        else{
        	printf("System out of memory!");
        }
        

        }

        This code moves along first priority. But when I add a page check in this loop then it goes infinite loop. What will be the solution?

        while(presentPtr != NULL && type > presentPtr->userType){
        prevPtr = presentPtr;
        presentPtr = presentPtr->nextPtr;
        }

        _ 1 Reply Last reply
        0
        • U User 10395751

          I used this code.

          void enqueue(char type, int p){
          struct node *newPtr, *prevPtr, *presentPtr,*tempPresent;

          //Allocating memory for new member
          newPtr = malloc(sizeof(struct node));
          
          //Checking allocation. If there is not room for new member in RAM then it gives an error
          if(newPtr != NULL){
          	newPtr->pages = p;
          	newPtr->userType = type;
          	newPtr->nextPtr = NULL;
          	
          	prevPtr = NULL;
          	presentPtr = front;
          	
          	while(presentPtr != NULL && type > presentPtr->userType){
          		prevPtr = presentPtr;
          		presentPtr = presentPtr->nextPtr;
          	}
          	//Checking if new member is the first element of the linked list or not
          	if(prevPtr == NULL){
          		newPtr->nextPtr = front;
          		front = newPtr;
          	}
          	else{
          		prevPtr->nextPtr=newPtr;
          		newPtr->nextPtr=presentPtr;
          	}
          }//If allocation fails throws a message to the user
          else{
          	printf("System out of memory!");
          }
          

          }

          This code moves along first priority. But when I add a page check in this loop then it goes infinite loop. What will be the solution?

          while(presentPtr != NULL && type > presentPtr->userType){
          prevPtr = presentPtr;
          presentPtr = presentPtr->nextPtr;
          }

          _ Offline
          _ Offline
          _Superman_
          wrote on last edited by
          #4

          Its probably because of an incorrect condition check. Please post the code with the page check that you put in.

          «_Superman_»  _I love work. It gives me something to do between weekends.

          _Microsoft MVP (Visual C++) (October 2009 - September 2013)

          Polymorphism in C

          U 1 Reply Last reply
          0
          • _ _Superman_

            Its probably because of an incorrect condition check. Please post the code with the page check that you put in.

            «_Superman_»  _I love work. It gives me something to do between weekends.

            _Microsoft MVP (Visual C++) (October 2009 - September 2013)

            Polymorphism in C

            U Offline
            U Offline
            User 10395751
            wrote on last edited by
            #5

            I add this one.

            while(presentPtr != NULL && type > presentPtr->userType){
            prevPtr = presentPtr;
            presentPtr = presentPtr->nextPtr;
            }

            if(type == presentPtr->userType){
            while(presentPtr != NULL && p > presentPtr->pages){
            prevPtr = presentPtr;
            presentPtr = presentPtr->nextPtr;
            }
            }

            _ 1 Reply Last reply
            0
            • U User 10395751

              I add this one.

              while(presentPtr != NULL && type > presentPtr->userType){
              prevPtr = presentPtr;
              presentPtr = presentPtr->nextPtr;
              }

              if(type == presentPtr->userType){
              while(presentPtr != NULL && p > presentPtr->pages){
              prevPtr = presentPtr;
              presentPtr = presentPtr->nextPtr;
              }
              }

              _ Offline
              _ Offline
              _Superman_
              wrote on last edited by
              #6

              Logic looks OK. I guess you need to single step through the code using the debugger and find out the issue.

              «_Superman_»  _I love work. It gives me something to do between weekends.

              _Microsoft MVP (Visual C++) (October 2009 - September 2013)

              Polymorphism in C

              U 2 Replies Last reply
              0
              • _ _Superman_

                Logic looks OK. I guess you need to single step through the code using the debugger and find out the issue.

                «_Superman_»  _I love work. It gives me something to do between weekends.

                _Microsoft MVP (Visual C++) (October 2009 - September 2013)

                Polymorphism in C

                U Offline
                U Offline
                User 10395751
                wrote on last edited by
                #7

                I tried but didn't find the problem. Do you have an idea?

                1 Reply Last reply
                0
                • _ _Superman_

                  Logic looks OK. I guess you need to single step through the code using the debugger and find out the issue.

                  «_Superman_»  _I love work. It gives me something to do between weekends.

                  _Microsoft MVP (Visual C++) (October 2009 - September 2013)

                  Polymorphism in C

                  U Offline
                  U Offline
                  User 10395751
                  wrote on last edited by
                  #8

                  Finded it, thanks for your help! :)

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups