Thread: Doubly linked list is only creating one node.

  1. #1
    Registered User
    Join Date
    Mar 2019

    Doubly linked list is only creating one node.

    Hey guys, trying to create a program which add/store records of patients to a node using doubly linked list. When the program executes, I'm only able to create one record. The program doesn't allow me add anymore patient and when I tried to display the record the program output different characters to the screen instead of the record.

    #define STRING_MAX 40
    typedef struct Patient{
        char patientName[STRING_MAX];
        int patientAge;
        int patientRegion;
        struct Patient *prev;
        struct Patient *next;
    struct Patient *head, *tail = NULL;
    struct Patient *current = NULL;
    void addPatient()
    {   char *patientName;
        int patientAge;
        int patientRegion;
        struct Patient *patient = (Patient *)malloc(sizeof(Patient));
        patient->patientAge = patientAge;
        patient->patientRegion = patientRegion;
        patient->prev = NULL;
        patient->next = NULL;
        if(head == NULL){//if head node is null, then get user input to create a record
            printf("Enter patient Name: \n");
            scanf("%s", patientName);
            printf("Enter patient Age: \n");
            scanf("%d", &patientAge);
            printf("Enter patient Region: \n");
            scanf("%d", &patientRegion);
            while(patientRegion > 10) //If the region is greater than ten, prompt user to enter a number less than 10
                printf("Invalid region\n");
                printf("Please enter region between (1 - 10): ");
                scanf("%d", &patientRegion);
            head = patient;
    void displayPatient(){
        struct Patient *current = head;
        if(head == NULL) {
            printf("\nList is empty\n\n");
        printf("Patient Records: \n");
        while(current != NULL) {
            printf("%s", current->patientName);
            printf("%d", current->patientAge);
            printf("%d", current->patientRegion);
            current = current->next;

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    It looks like you're only handling the case where head == NULL in addPatient, so it makes sense why your linked list is only ever creating one node: you're only adding a patient when the linked list is empty. If you handle the case where the linked list is not empty, then you can add more patients to the linked list.

    Your adding of the patient is also incorrect: you need to read the patient details in order to set them on the patient node: what you're doing now is setting the node's data members to uninitialised variables. I would move that interactive I/O to before you check for head == NULL. Note that you also need to set tail for the head == NULL case.

    Additionally, I would get rid of the global variable named current: the "current" node usually only makes sense in a local context, which in fact is what you're doing in displayPatient. (I would also rename displayPatient to displayPatients or displayPatientList since it displays all the patient records, not just a particular one.)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating a temp node for a doubly linked list
    By Bakenshake in forum C Programming
    Replies: 1
    Last Post: 01-28-2015, 03:08 PM
  2. Deleting the Last Node in a Doubly Linked List
    By tdoan35 in forum C Programming
    Replies: 0
    Last Post: 12-18-2011, 03:49 AM
  3. Creating Doubly Linked List Help!!
    By newbiecprogram in forum C Programming
    Replies: 3
    Last Post: 11-29-2010, 12:00 PM
  4. Doubly Linked List Node
    By DarkDot in forum C++ Programming
    Replies: 28
    Last Post: 04-16-2007, 12:13 AM
  5. Deleting node from doubly linked list
    By filler_bunny in forum C++ Programming
    Replies: 5
    Last Post: 08-24-2003, 12:03 AM

Tags for this Thread