简体   繁体   中英

C++ Priority Queue Class with Linked List

I am having two issues with my c++ code (The test file is below):

I can't seem to figure out why its not breaking out of the while loop, when running, its stuck on the loop "7 versus 325".

So it should go into the next node of temp, which would be null, and then jump into the section where it adds it to the end of the queue. But its just looping and looping.

My second issue is with the the function I have commented out, queue1.back, whenever that is ran, it just errors out and gives what appears to be the address, but the .front() function works just fine.

The Test File I am working with is like this:

89 Alex

325 Rob

72 Joy

91 Bob

using namespace std;

class Person
{
    friend class Pqueue;
    public:
        int priority;
        string name;
    };


    class PQueue
    {
        friend class Person;

        private:

        //Structure for my linked list.
        typedef struct node {
            Person data;
            struct node *next;
        }Node, *NodePtr;

        Node *head, *tail;

        public:
        //Prototype Functions
        PQueue(void);              //Initializer function
        bool empty(void);          //Test if empty
        int size(void);            //Return size
        void enqueue(Person *);    //Insert Node
        void dequeue(void);        //Remove Node
        Person* front(void);       //Access Next Node
        Person* back(void);        //Access last node

    };



    PQueue::PQueue()
   {
       head = NULL;
       tail = NULL;
   }



    bool PQueue::empty(){
        return (head == NULL);
    }



    void PQueue::enqueue(Person *myPerson){
        NodePtr np = (NodePtr) malloc(sizeof(Node));
        np->data = *myPerson;
        np->next = NULL;

        if(empty())
        {
             cout << "Making into creating the first node, of the linked list" <<endl;
             head = np;
             tail = np;
         }
         else { //Queue has more the one node
               Node* temp = head;
               if(np->data.priority > temp->data.priority) //If the priority is greater then the rest.
               {
                   head = temp;                            //Saving my head pointer
                   head->data = np->data;                  //Assigning new Data to the head pointer
                   head->next = temp;                      //Assigning the rest of the linked list back into head.
                   cout << "Making into creating the first node again, having to reassign." <<endl;
               }
               else{
                    //Searching where to place the node.
                    while(temp->data.priority > np->data.priority) //Searching if the next priority is higher then the passed.
                    {
                        cout << "Inside the while loop: " << np->data.priority << " versus "<<temp->data.priority <<endl;
                        if(temp->next == NULL)
                            break;
                        temp = temp->next;
                     }

            if(temp->next == NULL && np->data.priority < temp->data.priority) //Inserting at the end.
            {
                cout << "Making into creating the last node" <<endl;
                tail->next = np;
                cout << "Passing the function of creating the last node" <<endl;
            }
            else   //Inserting into the middle of the function.
            {
                cout << "Inserting in the middle of the queue" <<endl;
                np->next = temp->next;
                temp->next = np;
            }
        }
    }
}



void PQueue::dequeue(){
    if(empty()){
        cout << "\nAttempt to remove from an empty list." << endl;
        exit(1);
    }

    Person hold = head->data;
    NodePtr temp = head;
    head=head->next;
    if (head == NULL) tail = NULL;
    free(temp);
}

Person* PQueue::front(){
    //Person &temp = head->next->data;
    //Person &temp = head->data;
    Person &temp = head->data;
    return &temp;
}

Person* PQueue::back(){
    if(empty()){
        cout << "\nNo entries in list." << endl;
        exit(1);
    }
    Person &temp = tail->data;
    return &temp;
}

int main() {
    cout << "Starting main" << endl;
    PQueue queue1; //Creating my queue.
    cout << "Created Queue" << endl;
    Person tempPerson;
    ifstream inFile;
    inFile.open("/tmp/temp");
    cout << "going into while loop" << endl;

    while (inFile >> tempPerson.priority >> tempPerson.name){
        cout << "The priority is " <<  tempPerson.priority << " the name is " << tempPerson.name <<endl;
        queue1.enqueue(&tempPerson);
    }


    //Testing Section, trying to get .front and .back to work.
    Person *testPerson;
    testPerson = queue1.front();
    cout << "The TEST priority is " <<  testPerson->priority << " the TEST name is " << testPerson->name <<endl;
    /**
    Person *tailPerson;
    testPerson = queue1.back();
    cout << "The TEST priority is " <<  tailPerson->priority << " the TEST  name is " << tailPerson->name <<endl;
    **/

    queue1.dequeue();
    queue1.dequeue();
    queue1.dequeue();


    return 0;
}

When you add a new head entry to a non-empty list, you're mistakenly setting the next pointer to point right back at the node it's in, rather than setting it to point at the rest of the linked list like you intended.

head = temp;                            //Saving my head pointer
head->next = temp;                      //Assigning the rest of the linked list back into head.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM