user123456098
user123456098

Reputation: 180

print_list function only prints the first input of the user in the linked list

//Good day. This is just a part of my source code. My main predicament is that the print_list function only prints the first input of the user in the linked list. I can't seem to pin point the problem as the logic of the function seems right. The insert_list function seems to work fine too but I'm not that sure.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

//this is the node

typedef struct node
{
        char name[61];
        int month;
        int day;
        int year;
        struct node *next;
}node;

//this is the list

typedef struct list
{
        node *head;
        node *tail;
}list;

//this creates the node by accepting the user's input and puts them in the node

node *create_node()
{
        int x;
        node *data = (node*) malloc(sizeof(node));
        printf("Name: ");
        fgets(data->name, 61, stdin);
        printf("Birthdate (mm/dd/yyyy): ");
        scanf("%d%*[/]%d%*[/]%d", &data->month, &data->day, &data->year);
        getchar();
        if ((data->month)==0||(data->month)>=13||(data->day)<=0||(data->day)>=32||(data->year)<=1977||(data->year)>=3001)
        {
                while ((data->month)==0||(data->month)>=13||(data->day)<=0||(data->day)>=32||(data->year)<=1977||(data->year)>=3001)
                {
                        printf("Invalid Input.\n");
                        printf("Please Enter a Valid Birthdate(mm/dd/yyyy): \n");
                        scanf("%d%*[/]%d%*[/]%d", &data->month, &data->day, &data->year);
                        getchar();
                }
        }
        printf("******************************************************************\n");
        for (x=0; x<=strlen(data->name); x++)
        {
            if (data->name[x]=='\n')
            {
                    data->name[x]='\0';
            }
    }
    printf("Birthday reminder for %s is added.\n", data->name);
    return data;
}


list *create_list(list *plist)
{
        plist->head = NULL;
        plist->tail = NULL;
        return plist;
}

//this inserts the node in the list

list *insert_list(list *plist, node *pnode, node *new_node)
{
    if(plist->head==NULL)
    {
            plist->head=new_node;
            new_node->next=NULL;
    }
    else
    {
            new_node->next = NULL;
            pnode->next = new_node;
            plist->tail = new_node;
    }
    return plist;
}

//this prints the list

list *print_list(list *plist)
{
        node *current = plist->head;
        int i;
        for(i=1;current!=NULL;i++)
        {    
            printf("[%d] %s\n",i ,current->name);
            printf("Birth Date: %d/%d/%d\n", current->month, current->day, current->year);
            current=current->next;
    }
}

//this deallocates the list

list *free_list(list *List)
{
    node *current = List->head;
    node *temp = NULL;
    while(current != NULL)
    {
            temp = current;
            current = current->next;
            free(temp);
    }

    List->head = NULL;
    List->tail = NULL;
}

//this is the main

int main(void)
{
        list* List = (list*) malloc(sizeof(list));
        List = create_list(List);
        char x;
        node *data = (node *) malloc(sizeof(node));
        printf("******************************************************************\n");
        printf("                    ADD BIRTHDAY REMINDER FORM\n");
        printf("******************************************************************\n");
        List = insert_list(List, data, create_node(data));
        printf("Would you like to add another(y/n)?\n");
        scanf("%c", &x);
        if (x=='y')
        {
                while (x=='y')
                {
                        if (x=='y')
                        {
                                getchar();
                            printf("******************************************************************\n");
                                node *data = (node *) malloc(sizeof(node));
                                List = insert_list(List, data, create_node(data));
                                printf("Would you like to add another(y/n)?\n");
                                scanf("%c", &x);
                        }
                }
        }
    print_list(List);
    free(List);
    return 0;

}

//this code is ready for compilling

Upvotes: 0

Views: 806

Answers (3)

M. Shiina
M. Shiina

Reputation: 609

I don't understand why the second argument pnode is needed for insert_list. If you just want to print all elements in a list, I think the following modification can fix your problem:

First, add a line plist->tail=new_node into the first if-clause of insert_list. Second, change the second argument of insert_list from data to List->tail in main.

And there is another unimportant thing I would like to point out. Do you really need the condition if (x == 'y) in main? I don't know why it is there.

Upvotes: 1

Matthias
Matthias

Reputation: 8180

I do not understand, what do you intend by pnodein insert_list, and I guess, there is the error. Probably, you mean something like previous node. However, the tail is already the previous node. In addition, you use an empty node there, even you alloc memory for a node in create_node. Maybe, the following code would be more appropriate:

list *insert_list(list *plist, node *new_node)
{
    if(plist->head==NULL)
    {
            plist->head=new_node;
            plist->tail=new_node;
            new_node->next=NULL;

    }
    else
    {
            new_node->next = NULL;
            plist->tail->next = new_node;
            plist->tail = new_node;
    }
    return plist;
}

Upvotes: 1

Tom&#225;š Š&#237;ma
Tom&#225;š Š&#237;ma

Reputation: 864

Are you sure you pasted your actual source? in List = insert_list(List, data, create_node(data)); you call create_node(data) but you function node *create_node() takes zero arguments. I dont understand the purpose of node *pnode argument in `list *insert_list(list *plist, node *pnode, node *new_node)

The print function seems legit to me. Try compiling with -Wall -Wextra -Werror for extra error detection. `

Upvotes: 1

Related Questions