0

I have a struct called employee in my program with the following definition:

struct employee {
int id;
int age;
struct *employee next;
};

How would I take in input from the user, create a struct from their input, and create a singly linked list using pointers? I'm having a lot of issues figuring out how to do this dynamically. In Java, this is easily done through the constructor but how would you do this in C?

EDIT: Assuming the input is simply two ints (id and age).

4
  • I have written a blog post describing implementation of linked lists cyberlingo.blogspot.in/2014/11/linked-lists.html Commented Apr 2, 2015 at 15:21
  • @karma_geek That's a little too complicated for what I need to do. I'm new to C so this is still rather challenging for me. Commented Apr 2, 2015 at 15:25
  • Ok, so first you need to understand the basics of dynamic allocation- how to use malloc, free etc. Also, you must know how pointers work. Your question is very broad, any particular problems that you are facing? Commented Apr 2, 2015 at 15:27
  • I am using scan f to accept two integers (as long as both ints aren't 0) with the idea of creating a struct out of them. I also want these structs to be a linked list. So say the user inputs the following: (9, 3) and (3, 2). I would create two structs with the first one (9,3) having a next pointer to the second one (3, 2). Commented Apr 2, 2015 at 15:35

2 Answers 2

1

This is how you create a new employee struct. You are dynamically allocating the memory using malloc function.

 struct employee *new_employee = (struct employee*)malloc(sizeof(struct employee));

Now, we need to fill the data into this newly created employee field:

new_employee -> id = input_id;
new_employee -> age = input_age;

As for the next pointer, it is usually given NULL value. This is to prevent the next pointer from pointing to any arbitrary memory location.

new_employee -> next = NULL;

Finally, we have to link the list. To do that, you have to point the next pointer of the previous employee field to the current employee field (ex:as you mentioned in comment, first one (9,3) having a next pointer to the second one (3, 2))

Since it is a singly linked list, we cannot backtrack. So, there are two methods to access the previous field.

First is to maintain a pointer that points to the last field of the link list.

Second is to traverse the entire list till the end, and when you reach the last element, change its next pointer.

Implementation of the second method:

 node *temp = *start;
    if(temp!=NULL)
    {
            while(temp -> next)
                temp = temp -> next;
            temp -> next = new_employee;
     }

Hope it helps!

Sign up to request clarification or add additional context in comments.

Comments

1

Note : don't give me fish teach me how to fish

This is an example :

struct node {
int value;
struct *node next;
};

How to create new Node pointer dynamically ?

node* aux = (node*) malloc(sizeof(node));

How to get user input ( safely ) ?

char line[256];
int i;
if (fgets(line, sizeof(line), stdin)) {
    if (1 == sscanf(line, "%d", &i)) {
        /* i can be safely used */
    }
}

How to create a linked list's head ?

/* This will be the unchanging first node */
struct node *root;      
/* Now root points to a node struct */
root = (struct node *) malloc( sizeof(struct node) ); 
/* The node root points to has its next pointer equal to a null pointer 
   set */
root->next = 0;  
/* By using the -> operator, you can modify what the node,
   a pointer, (root in this case) points to. */
root->value = 5; 

How to add new node to the linked list ?

/* This won't change, or we would lose the list in memory */
    struct node *root;       
    /* This will point to each node as it traverses the list */
    struct node *conductor;  

    root = malloc( sizeof(struct node) );  
    root->next = 0;   
    root->value = 12;
    conductor = root; 
    if ( conductor != 0 ) {
        while ( conductor->next != 0)
        {
            conductor = conductor->next;
        }
    }
    /* Creates a node at the end of the list */
    conductor->next = malloc( sizeof(struct node) );  

    conductor = conductor->next; 

    if ( conductor == 0 )
    {
        printf( "Out of memory" );
        return 0;
    }
    /* initialize the new memory */
    conductor->next = 0;         
    conductor->value = 42;

Now you must be able to solve the problem easily .

Happy Coding :D

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.