little queue help with function

This is a discussion on little queue help with function within the C++ Programming forums, part of the General Programming Boards category; I have wrote most of my Enqueue function but need help on a small part of it. struct Node {Node* ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    14

    little queue help with function

    I have wrote most of my Enqueue function but need help on a small part of it.

    struct Node
    {Node* nextPtr; Node* previousPtr; Message data;}

    class Priority
    private: {Node* frontPtr; Node* rearPtr; int count;}

    Here is my function:
    Code:
    void PriorityQ::Enqueue(Message msg)
    {
    if (IsFull())
    throw FullPQ();
    else
    {
    
    Node* temp1;
    temp1 = new Node; // creat new node
    temp1->data = msg; //data in node will be the message
    temp1->nextPtr = NULL;
    temp1->previousPtr = NULL;
    
    
    if ( (frontPtr == NULL) && (rearPtr == NULL) )
    {
    temp1->nextPtr = frontPtr; //sets the node
    frontPtr = temp1;
    rearPtr = frontPtr;
    }
    else
    {
    int num = 0;
    Node* curPtr;
    Node* temp;
    curPtr = frontPtr;
    temp=curPtr;
    
    // Figure out the entry that should the value should be inserted at
    while ((curPtr != NULL) && (msg.GetPriority() <= curPtr->data.GetPriority()))
    {
    temp=curPtr;
    curPtr = curPtr->nextPtr;
    num++;
    }
    
    //correct if entry should be after the end of the list
    if(curPtr == NULL)
    {
    curPtr=temp;
    
    //new entry will be last entry
    rearPtr=temp1;
    }
    
    if(msg.GetPriority() > curPtr->data.GetPriority())
    {
    //insert temp1 before the curPtr node
    temp1->previousPtr=curPtr->previousPtr;
    temp1->nextPtr=curPtr;
    curPtr->previousPtr=temp1;
    
    while(curPtr != NULL)
    {
    
    }
    
    //Not sure if above is correct but if need to insert entry before the node that
    //curPtr is pointing to and it is pointing to an entry(its not at the front), need
    //to make sure to point to previousPtr. But if curPtr does not point to a previous node,
    //then it becomes the new pointer(ie. change frontPtr to temp.
    //there are about 4 lines missing
    }
    
    else
    {
    // insert temp1 after the curPtr node
    temp1->nextPtr=curPtr->nextPtr;
    temp1->previousPtr=curPtr; //previous of new node will be current
    curPtr->nextPtr=temp1;
    
    //Above is correct, now: the node that the curPtr points to has a previousPtr
    //that is pointing to curPtr. Make sure, the node curPrt points to is not NULL.
    //may or may not be a node after curPtr
    //there are about 2 lines missing for this
    while(curPtr != NULL)
    //not sure here
    
    }
    }
    
    // Increment the counter
    count++;
    }
    }
    And here is the specific part I am struggling on, just a few lines that need to be wrote:
    Code:
    if(msg.GetPriority() > curPtr->data.GetPriority())
    {
    //insert temp1 before the curPtr node
    temp1->previousPtr=curPtr->previousPtr;
    temp1->nextPtr=curPtr;
    curPtr->previousPtr=temp1;
    
    while(curPtr != NULL)
    {
    
    }
    
    //Not sure if above is correct but if need to insert entry before the node that
    //curPtr is pointing to and it is pointing to an entry(its not at the front), need
    //to make sure to point to previousPtr. But if curPtr does not point to a previous node,
    //then it becomes the new pointer(ie. change frontPtr to temp.
    //there are about 4 lines missing
    }
    
    else
    {
    // insert temp1 after the curPtr node
    temp1->nextPtr=curPtr->nextPtr;
    temp1->previousPtr=curPtr; //previous of new node will be current
    curPtr->nextPtr=temp1;
    
    //Above is correct, now: the node that the curPtr points to has a previousPtr
    //that is pointing to curPtr. Make sure, the node curPrt points to is not NULL.
    //may or may not be a node after curPtr
    //there are about 2 lines missing for this
    
    while(curPtr != NULL)
    //not sure here
    
    }
    Last edited by Salem; 03-11-2011 at 01:20 AM. Reason: Restored!

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,230
    Okay, I'm not sure how you are expecting to get to that last `else', but I only looked at the source for long enough to read it.

    In any event, since you are building your priority queue as a doubly linked list, you only need to understand how to insert a node between two nodes in a doubly linked list to do this "enqueue" operation successfully, and it looks as if you have that figured out. Do you understand how to do that? Can you explain it with words?

    Also, where exactly did you get this code? If this interface is from a school assignment it makes me want to track down your teacher and smack him.

    Soma

  3. #3
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Kindly specify your problem..... so that we could look over that... I am not talking about the code but the problem...

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    14
    Quote Originally Posted by Mr.777 View Post
    Kindly specify your problem..... so that we could look over that... I am not talking about the code but the problem...
    I have. I commented, in the bottom code part, what I am trying to do in the if statement and else statement. For the else, I have while loop but confused on what statement will be in it. I tried to explain in the comments.

  5. #5
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    What i've looked so far is msg is not any pointer but just the part of the node and you can't get it like this..... You should do
    Code:
    Node * temp1=new Node();
    temp1->rear_Pointer=NULL;
    temp1->front_Pointer=NULL;
    temp1->data=msg;
    So now,
    while(temp1->data.GetPriority()){//and so on}
    And you commented that there are two lines missing... What do you really want to do now... I've the idea of enqueue but first, i don't know the location of your pointers, second, don't know many of your functions outputs and blah blah.....
    So either put the whole code and tell what do you want to do now.... SO that we could look over that... Thanks...

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,496
    First post restored!!!
    Do NOT delete posts as soon as you have the answer, it makes the whole tread trash.
    If you can't deal with this, then don't post.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lost on Queues - Help GREATLY appreciated.
    By TetsuoShima in forum C Programming
    Replies: 3
    Last Post: 07-13-2010, 05:35 AM
  2. Help with FIFO QUEUE
    By jackfraust in forum C++ Programming
    Replies: 23
    Last Post: 04-03-2009, 08:17 AM
  3. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21