A K
A K

Reputation: 183

add function is not any new node or display function is showing none but the first node info only

enter code hereIn the main function I am calling the add function as well as the display function to see all the present nodes and their stored data into the linked list. But every time it's only showing me the first node value not the other nodes. I can't find any bug into my code. Can any body help...

This is my code:-

struct student* add(struct student*);
struct student* search(struct student*);
struct student* modify(struct student*);
void display(struct student* head);
struct student
{
    int roll;
    char name[50];
    float percentage;
    struct student *address;
};
int nodes=0;//nodes variable keeps the data of the total no of nodes present in to the inked list.
int main()
{
    struct student *head;
    int choice;
    char mychoice='y';
    head=NULL;
    do
    {
        printf("Enter 1 to add a node.\nEnter 2 to display all existing record.\nEnter 3 to search a record.\nEnter 4 to modify an existing record.\nEnter your choice: ");
        scanf("%d",&choice);
        switch(choice)
        {
            case 1:
                head=add(head);
                display(head);
                //break;
            case 2:
                display(head);
                break;
            case 3:
                head=search(head);
                break;
            case 4:
                head=modify(head);
                break;
            default:
                printf("Enter any of the correct input.\n");
                break;
        }
        printf("\nDo you want to continue? [Y/N]");
            scanf(" %c",&mychoice);
    }while(mychoice=='y'||mychoice=='Y');
    return 0;
}

struct student* add(struct student* head)
{
    struct student *node,*temp;
    node=(struct student*)malloc(sizeof(struct student));
    temp=head;
    printf("Enter the name of the student: ");
    scanf("%s",&(node->name));
    printf("Enter the roll of the student: ");
    scanf("%d",&(node->roll));
    printf("Enter the percentage: ");
    scanf("%f",&(node->percentage));
    node->address=NULL;
    if(head==NULL) // Implies an empty list.
        head=node;
    else
    {
        temp=head;
        while(temp!=NULL)//Traversing to the last node.
            temp=temp->address;
        temp=node;
    }
    nodes++;
    return head;
}

struct student* search(struct student* head)
{
    int m;
    printf("Enter the no of node you wanna search: ");
    scanf("%d",&m);
    if(m>nodes)
        printf("%dth node does not exist.",m);
    else
        {
            for(i=0;i<m;i++)// Traversing node to node to go to the mth node.
                temp=temp->address;
            printf("\nThe name of the student is: %s\nThe roll no of the student is: %d \nThe percentage of the student is: %5.2f \n\n",temp->name,temp->roll,temp->percentage);            
        }
    return head;
}

struct student* modify(struct student* head)
{
    int m,i;
    struct student *temp;
    temp=head;
    printf("Enter the index no of node you wanna change: ");
    scanf("%d",&m);
    if(m>nodes)
        printf("%dth node does not exist.",m);
    else
        {
            for(i=0;i<m;i++)// Traversing node to node to go to the mth node.
                temp=temp->address;
            printf("Enter the new name of the student: ");
            scanf("%s",&(temp->name));
            printf("Enter the new roll of the student: ");
            scanf("%d",&(temp->roll));
            printf("Enter the new percentage: ");
            scanf("%f",&(temp->percentage));
        }
    return head;
}

void display(struct student* head)
{
    struct student *temp;
    temp=head;
    while(temp!=NULL)
    {
        printf("\nThe name of the student is: %s\nThe roll no of the student is: %d \nThe percentage of the student is: %5.2f \n\n",temp->name,temp->roll,temp->percentage);
        temp=temp->address;
    }
}

Upvotes: 0

Views: 59

Answers (1)

4386427
4386427

Reputation: 44329

For the add function your problem is here:

    temp=head;
    while(temp!=NULL)//Traversing to the last node.
        temp=temp->address;
    temp=node;

You never make the currently last node point to the new node. What you do is to assign the new node to temp and then return. When you return temp goes out of scope and the new node is lost.

To insert a new node, you'll need to update address of the last node. In pseudo code you need to do:

// Pseudo code to insert a new tail node
current_last_node->address = new_node;

Try something like this:

    temp=head;
    while(temp->address!=NULL)//Traversing to the last node.
        temp=temp->address;
    temp->address=node;

BTW: Calling the pointer to the next element of the list for address is not normal. The convention is to use the name next.

Two other comments:

1) Since you want to add to the end of the list it's often a good idea to have a tail pointer for better performance.

2) You should collect head, nodes and tail into a struct.

Something like:

struct student_list
{
    struct student *head;
    struct student *tail;
    int nodes;
};

int main()
{
    struct student_list list = {NULL, NULL, 0};
    add(&list);
    ...
    ...
}

Then the add function would be:

void add(struct student_list* list)
{
    struct student *node;
    node=malloc(sizeof *node);

    // Read data into node

    node->address=NULL;

    if(list->head==NULL) // Implies an empty list.
    {
        list->head=node;
    }
    else
    {
        list->tail->address=node;
    }
    list->tail=node;
    list->nodes++;
}

Now there is no loop in the function so adding elements are O(1), i.e. better performance.

Upvotes: 2

Related Questions