Reputation: 39
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?
Upvotes: 1
Views: 2500
Reputation: 2720
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.
Upvotes: 0