简体   繁体   中英

Error while printing the linked list

I created the linked list to store employee id and name.

When I tried to print it, it shows only id not an employee name and i also want to exit the program when the user enter -1 and not asking the name its should simply exit the program and display the id and name i am currently using devC++ for compiling my code

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
    int id;
    char name[20];
    struct node *next;
};

struct node *create()
{
    struct node *p, *r, *n;
    int s, k;
    char name[20];
    s = sizeof(struct node);
    printf("Linked List\n");
    printf("Enter id:");
    scanf("%d", &k);
    printf("Enter name:");
    scanf("%s", name);
    p = r = NULL;
    while(k!=-1)
    {
        n = (struct node *)malloc(s);
        n->id = k;
        n->next = NULL;
        if(r == NULL)
            r = n;
        else
            p->next=n;
        p=n;
        printf("Enter the Id or-1 to stop:");
        scanf("%d", &k);
        printf("Enter the name ");
        scanf("%s", name);
    }
    return(r);      
}

void display(struct node *r)
{
     printf("\nId             Name \n");
     while(r != NULL)
     {
         printf("\n %d", r->id);
         printf("\n %s", r->name);
         r = r->next;    
     }
}

int main()
{
    struct node *ptr;
    ptr = create();
    display(ptr);
}

You actually read in the name variable, but you don't move it in your structure. That said, you could directly read into the structure you allocate, but the tricky part is taking care of not overflowding your buffer when the user input is too big (more than 19 chars).

This could look like this:

#include<stdio.h>
#include<stdlib.h>
struct node {
    int id;
    char name[20];
    struct node *next;
};

struct node *create(void) {
    struct node *p, *r;
    printf("Linked List\n");
    p = r = NULL;
    while (1) {
        int id;
        printf("Enter the Id or-1 to stop:");
        scanf("%d", &id);

        if (id == -1)
            break; // user asked to leave

        struct node *n = malloc(sizeof(*n));
        // if (n == NULL) exit(-1); as you prefere...
        n->id = id;
        printf("Enter the name ");
        // careful of buffer overflow here
        scanf("%19s%*[^\n]", n->name);
        n->next = NULL;

        if (r == NULL)
            r = n;
        else
            p->next = n;
        p = n;
    }
    return r;
}

void delete(struct node *r) {
    while (r != NULL) {
        struct node *n = r;
        r = r->next;
        free(n);
    }
}

void display(const struct node *r) {
    printf("\nId             Name \n");
    while (r != NULL) {
        printf("%d         %s\n", r->id, r->name);
        r = r->next;
    }
}

int main(int argc, char **argv) {
    struct node *ptr = create();
    display(ptr);
    delete(ptr);
    return 0;
}

As a bonus, I also added the free part so that you don't leak.

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