characters in the linked list

advertisements

I am trying to create a linked list which will take user name, age and ssn number as a input and print the output in list format. I am getting some errors so could not get [input?].

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

struct person
{
    char *name;
    int age;
    char *ssn;
};

struct node
{
    struct person * person;
    struct node * next;
} *head, *element;

void insert (struct person *new_person)
{
    element->person = new_person;
    element->next = head;
    head = element;
}

void display (struct node *ll)
{
    if(ll == NULL)
        printf("empty list");

    while(ll != NULL)
    {
        printf("%s %d  %s ", ll->person->name, ll->person->age, ll->person->ssn);
        ll = ll->next;

        if(ll != NULL)
            printf("->");
    }
}

main()
{
    int total_no_person, i, page;

    printf("enter the total number of person \t");
    scanf("%d", &total_no_person);

    struct node * temp = (struct node *) malloc(sizeof(struct node));
    struct person * new_person;

    char *pname = NULL;
    char *pssn = NULL;

    head = NULL;

    for(i = 0; i < total_no_person; i++)
    {
        pname = (char *) malloc(100);
        pssn = (char *) malloc(100);

        struct person * newly;

        printf("enter the %dth person's name \t", i + 1);
        scanf("%s", &pname);
        newly[i].name = pname;

        printf("enter %dth person's age \t", i + 1);
        scanf("%d", &page);
        newly[i].age = page;

        printf("enter %dth person's ssn \t", i + 1);
        scanf("%s", &pssn);
        newly[i].ssn = pssn;

        new_person = newly;
        insert(new_person);
    }
    temp = head;
    display(temp);

}


There's .... a lot wrong here.

First thing that jumps out:

struct person *newly;
...
newly[i].name=pname;

newly is a person pointer. You never allocate a person, and then try and access it like it's a local struct (multiple times) as ... an array?

struct person *newly = malloc(sizeof(struct person));

is what you're looking for. You then would pass it to your insert function:

insert(newly);

new_person is redundant and doesn't do anything. Same with your node

You've also never allocated the head of the list itself. Your insert assumes there's a head ... that isn't there. You should set element to NULL, and check for that because if it's NULL ... it's your first insert to the list. (edit: Erm, well, actually head and ... reading it again I'm not sure what you're trying to do with element)

Honestly - I would suggest a bit of googling, or a beginner's C book. We can point out all the problems in your code, but without you having an understanding of what you're actually using you aren't going to benefit.

EDIT: With that being said, I guess it's reasonable to post a working example, salvaging as much of the original code as possible.

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

struct person
{
    char *name;
    int age;
    char *ssn;
};

/* Note: because head and tail are global they
   are initialized to NULL automatically */
struct node
{
    struct person *person;
    struct node *next;
} *head, *tail;

void insert(struct person *new_person)
{
    /* allocate a new node */
    struct node *node = malloc(sizeof(struct node));

    /* assign the person to the node */
    node->person = new_person;
    node->next = NULL;

    if (head == NULL)
    {
         /* Since head is NULL, we are inserting for the first time.
            Set the head and tail to point at our new node */

        head = node;
        tail = node;
    }
    else
    {
        /* the tail is the last node in our list. We attach the new
           node to its next, then repoint the tail to our new node */
        tail->next = node;
        tail = node;
    }
}

void display()
{
    if(head == NULL)
    {
        printf("empty list\n");
    }
    else
    {
        struct node *current = head;
        while(current != NULL)
        {
            printf("%s %d  %s ", current->person->name,
                                 current->person->age,
                                 current->person->ssn);
            current = current->next;
            if(current != NULL)
                printf("->");
        }
        printf("\n");
    }
}

main()
{

    int total_no_person,i;

    printf("enter the total number of person \t");
    scanf("%d",&total_no_person);

    for(i=0;i<total_no_person;i++)
    {
        /* allocate a new person, then allocate its members */
        struct person *newly = malloc(sizeof(struct person));
        newly->name = malloc(100);
        newly->ssn = malloc(100);

        printf("enter the %dth person's name \t",i+1);
        scanf("%s", newly->name);

        printf("enter %dth person's age \t",i+1);
        scanf("%d", &newly->age);

        printf("enter %dth person's ssn \t",i+1);
        scanf("%s", newly->ssn);

        insert(newly);
    }

    display();
}

The one extra bit I've left out is the part where you could overflow the input buffer with scanf - http://www.crasseux.com/books/ctutorial/String-overflows-with-scanf.html