1

Hey I’m trying to create a SINGLY Linked List program that has 2 structs in it.

There is a struct that holds an Employee’s Information; like this

typedef struct{
    int ID; //Employee ID, randomly generated 4 digit integer
    char *firstName[25]; //Employee's first name
    char *lastName[35]; //Employess's last name
    char status; //AM/PM status, if valueof char:1=AM Employee,0=PM Employee
}EMPLOYEE;

Then there is a Linked List struct, which contains the Employee Information Struct AND a reference to a Linked List struct (I called it next); it looks like this:

struct LinkedList{
    EMPLOYEE employee;
    struct LinkedList *next;
};

In the main() function, I have to create TWO Linked List head node, because I have to have TWO Linked Lists, one for AM employees and one for PM employees, so I made this:

void main() {
    struct LinkedList *AMHead = NULL; //I made this NULL for now, they will be allocated in create a LinkedListNode function.
    struct LinkedList *PMHead = NULL;
}

So now is where I am having difficulty. Then there is a function that creates a Linked List Node, it can’t have parameters. It will use memory allocation to create the node, then I will ask user for employee information and initialize the employee struct portion of the Linked List node with the given info. I will then set the next pointer to NULL, and return a pointer to this new node. Here is what I wrote so far:

void createLinkedListNode(){
    struct LList *node = (struct LList)*malloc(sizeof(struct LList)); 
    //node created by allocating memory to struct


    //prompt for all info required in employee struct
    printf("Enter ID:\n");
    printf("Enter first name:\n");
    printf("Enter last name:\n");
    printf("Enter status:\n");
};

So my questions are, do I have to create 2 nodes? One for the AM list and another for the PM list, or can I use the same node for both. And then how do I initialize the employee struct and store the info read. And did I create the structs and the head nodes correctly because I have 2 linked lists in a singly linked list program. Anything I should fix or any way I can make the code beter?

5
  • "do I have to create 2 nodes". You need to clarify your question. As it is, the answer is "it depends". It depends on how the AM and PM lists are used. It depends on whether employees can move from AM to PM. It depends on how employee is determined to be an AM or PM. etc. There's no rule that says you have to have one list and no rule that says you have to have two. It depends on the context. Commented Apr 13, 2015 at 3:29
  • Yes employees are allowed to move from AM to PM, they are determined from their assigned char value. Commented Apr 13, 2015 at 4:13
  • In that case probably create one node and put it into the appropriate list. When the status changes then move it. Note that this isn't the only "correct" way to do things. For example, you could just put all the employees in a single list and then read the status each time when you need to operate on an employee. I'm not saying that is the best way or even the right way to do it. The point is that your question really has no correct answer. One needs to consider the entire problem to determine the tradeoffs and only then is there any chance of making a considered design choice. Commented Apr 13, 2015 at 4:18
  • 1
    use fgets to read input, something like fgets(node->firstName, sizeof(node->firstName), stdin); Simplify and use only one of EMPLOYEE and LList Commented Apr 13, 2015 at 4:27
  • char *firstName[25]; --> char firstName[25]; Commented Apr 13, 2015 at 8:28

1 Answer 1

0

Co-incidentally I am also practicing singly-linked list now. Let me try to answer based on what I understood from your question and what I think I have learnt till now.

You asked, "Do I have to create two nodes ?" I guess your question was "Do I have two create two linked-lists ?"

If there are more than two employees then you will have to create more than two nodes. No options.

However, if you meant "Do I have two create two linked-lists ?". The answer is no if the AM list and PM list are identical. If AM list and PM list are identical, then yes you can use only ONE list.

However if AM and PM list are not identical then you need to use two lists. Because, as your linked-list is a singly linked-list, it cannot have two next pointers, which is the problem. Since any node in you list can only point to ONE other node (or NULL), that single node cannot point to a different node based on whether you are considering the node to be in AM or PM.

For ex: It means you cant make a node pointing to node with employee.id == 25 in AM list point to node with employee.id == 30 in PM list. So, I guess you need two list in this case.

So, you see it depends. Both on the nature of your lists and nodes I guess.

To initialize the employee struct and store the info read:

Once you have allocated memory for new node with below statement:

struct LList *node = malloc(sizeof(struct LList)); 

Next you can use the -> operator to store the data into the node.

For example, to store ID:

int id;
printf("Enter ID:\n");
scanf("%d",&id)
node->ID = id;

Similarly, you can store other data (lastname, firstname) using the -> operator.

For the correctness part, I can see one issue.

You have defined a node as:

struct LinkedList{
EMPLOYEE employee;
struct LinkedList *next;
};

However you have created a node with:

struct LList *node = (struct LList)*malloc(sizeof(struct LList)); 

However there is no LList struct. You need to change that to:

struct LinkedList *node = malloc(sizeof(struct LinkedList)); 

I don't think typecasting is necessary for malloc().

I might have made mistakes in this analysis, so please free to comment or edit the answer if there are any mistakes above.

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

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.