Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8650659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:52:40+00:00 2026-06-12T13:52:40+00:00

I have a problem for my 2nd year programming class that involves creating a

  • 0

I have a problem for my 2nd year programming class that involves creating a set of doubly linked lists to represent a hospital, and the doctors and patients in the hospital. A hospital has a list of doctors, and each doctor has a list of patients. My issue is that when i call the “hireDoctor” function to add a doctor to the hospital’s list, somehow the head pointer is getting changed. here’s my code:

    /* adds a Doctor to the Hospital's list of Doctors */
    void Hospital::hireDoctor(Doctor *doc)
    {
        DoctorNode node;
        node.value = *doc;
        DoctorNode* curr;

        if (drListHead == NULL) {    //if doctor list is empty,
            drListHead = &node;    //insert the node at the beginning
            node.next = NULL;
            node.prev = NULL;
        } else {
            curr = drListHead;
            //traverse list until equal or greater (alphabetical) value is found:
            while (curr->value.getLast().compare(node.value.getLast()) < 0 &&
            curr->value.getFirst().compare(node.value.getFirst()) < 0) {
                curr = curr->next;
            }
            if (curr->prev == NULL) {     //if inserting at the beginning of the list
               drListHead = &node;
               node.prev = NULL;
               node.next = curr;
            } else if (curr->next == NULL) { //if the end of the list has been reached
                curr->next = &node;
                node.prev = curr;
                node.next = NULL;
            } else {              //insert the new DoctorNode in the middle:
                curr->next->prev = &node;
                node.next = curr->next;
                curr->next = &node;
                node.prev = curr;
            }
     }

Each node in the list is defined as a struct:

    struct DoctorNode {
      Doctor value;
      DoctorNode *next;
      DoctorNode *prev;
    }

So after once though the hireDoctor function, if i “hire” a doctor named John Smith, I’d expect drListHead to point to John Smith, which seems to be what happens. However, on the second time through the function, hiring Jane Doe, it seems that drListHead is already pointing to Jane Doe upon entering the function. I can’t figure out where it’s getting changed. Any thoughts would be greatly appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T13:52:42+00:00Added an answer on June 12, 2026 at 1:52 pm

    Problem is here:

            if (drListHead == NULL) {    //if doctor list is empty,
                drListHead = &node;    //insert the node at the beginning  <------
                node.next = NULL;
                node.prev = NULL;
            } 
    

    Your node is initialized on the stack and after exit hireDoctor it will point to stack’s address. Next time you call hireDoctor node again points on the same address which is Jane Doe(which is just a coincidence). You need this:

                if (drListHead == NULL) {    //if doctor list is empty,
                    drListHead = new DoctorNode();    //heap allocation 
                    drListHead->next = NULL;
                    drListHead->prev = NULL;
                }
    

    But remember that you must implement freeing of unused to memory.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have problem that i have 3 views, i goto first view to 2nd
I have problem with jqGrid plugin. If I am on e.g. 2nd page, and
I have a problem using DatagramSocket. The problem is that I can't run two
I'm learning Java (2nd year IT student) and I'm having a little problem. With
I have problem with http://abfoodpolicy.com/ . In IE 8 and 9 the right sidebar
I have problem with my query on C, I’m using the oci8 driver. This
I have problem with repopulating form_upload after validation. Other input fields or selectboxes are
I have problem with show or hide form in Window Form Application. I start
I have problem with UIWebView delay when the load image from url. In my
I have problem while loading data into html select when users press or click

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.