There a a multitude of things to address in this code.
First, you're leaking memory in create_node
. That function should be designed to dynamically allocate an event, prepare it with the passed parameters, and return it to the caller as an address to be assigned to a pointer on the caller-side:
Event* create_node(int processid, int arrivaltime, int cputime)
{
Event *ptr = malloc(sizeof(*ptr));
if (ptr == NULL)
{
perror("Failed to allocate Event.");
exit(EXIT_FAILURE);
}
ptr->processid = processid;
ptr->arrivaltime = arrivaltime;
ptr->cputime = cputime;
ptr->next = NULL;
return ptr;
}
Next, your add_event
function should take an event pointer as the list head, and ideally the address of said-pointer to allow it to be modified on initial-insert. Two schools of thought prevail for insertion functions, (1) always return the list head, and (2) use a pointer-to-pointer for the list head parameter, and modify it via dereference as needed. I prefer the latter, as it makes the code very simple. Even more so in your case since new event creation is done somewhere else.
// link an event on to the end of our linked list.
void add_event(Event** pp, Event* event)
{
while (*pp)
pp = &(*pp)->next;
*pp = event;
}
Once those are taken care of, the source in main()
also becomes significantly simpler, taking into account I've also fixed the improper timeSlice
variable, which was char
and should have been int
. More importantly, the logic you had would have a blank list head node, which buys you nothing. You can use a pointer, then call our add_event
function and pass its address. It will properly insert the node.
The final product looks like this (with minor mods):
#include <stdio.h>
#include <stdlib.h>
typedef struct event
{
struct event *next;
int processid;
int arrivaltime;
int cputime;
} Event;
Event* create_node(int processid, int arrivaltime, int cputime);
void add_event(Event** head, Event *event);
void print_node(const Event* node);
void print_eventlist(const Event *eventlist);
int main(int argc, char* argv[])
{
int sourcenum,a,b,c,i,timeslice;
Event* events = NULL;
if(argc != 3)
{
printf("Proper usage is: main.c sources time-slice\n");
exit(EXIT_FAILURE);
}
// TODO: these really should be using strtol() for better
// error detection. left as exercise for author.
sourcenum = atoi(argv[1]);
timeslice = atoi(argv[2]);
for(i = 0; i < sourcenum ; i++)
{
printf("please enter the process-id, arrival-time and cpu-time.\n");
if (scanf("%d %d %d",&a, &b, &c) == 3)
{
add_event(&events, create_node(a,b,c));
print_eventlist(events);
}
}
return 0;
}
// link an event on to the end of a linked list.
void add_event(Event** pp, Event* event)
{
while (*pp)
pp = &(*pp)->next;
*pp = event;
}
Event* create_node(int processid, int arrivaltime, int cputime)
{
Event *ptr = malloc(sizeof(*ptr));
if (ptr == NULL)
{
perror("Failed to allocate Event.");
exit(EXIT_FAILURE);
}
ptr->processid = processid;
ptr->arrivaltime = arrivaltime;
ptr->cputime = cputime;
ptr->next = NULL;
return ptr;
}
void print_node(const Event* node)
{
if (node != NULL)
{
printf("process-id = %d\n",node->processid);
printf("Arrival Time = %d\n",node->arrivaltime);
printf("CPU time = %d\n",node->cputime);
}
else
{
printf("NULL\n");
}
}
void print_eventlist(const Event* currentPtr)
{
puts("==========================================");
while(currentPtr != NULL)
{
print_node(currentPtr);
puts("==========================================");
currentPtr = currentPtr->next;
}
puts("NULL");
puts("==========================================");//list #%d\n",i);
}
Sample Output
Obtained from a command line requesting 5 entries.
please enter the process-id, arrival-time and cpu-time.
1 1 1
==========================================
process-id = 1
Arrival Time = 1
CPU time = 1
==========================================
NULL
==========================================
please enter the process-id, arrival-time and cpu-time.
2 2 2
==========================================
process-id = 1
Arrival Time = 1
CPU time = 1
==========================================
process-id = 2
Arrival Time = 2
CPU time = 2
==========================================
NULL
==========================================
please enter the process-id, arrival-time and cpu-time.
3 3 3
==========================================
process-id = 1
Arrival Time = 1
CPU time = 1
==========================================
process-id = 2
Arrival Time = 2
CPU time = 2
==========================================
process-id = 3
Arrival Time = 3
CPU time = 3
==========================================
NULL
==========================================
please enter the process-id, arrival-time and cpu-time.
4 4 4
==========================================
process-id = 1
Arrival Time = 1
CPU time = 1
==========================================
process-id = 2
Arrival Time = 2
CPU time = 2
==========================================
process-id = 3
Arrival Time = 3
CPU time = 3
==========================================
process-id = 4
Arrival Time = 4
CPU time = 4
==========================================
NULL
==========================================
please enter the process-id, arrival-time and cpu-time.
5 5 5
==========================================
process-id = 1
Arrival Time = 1
CPU time = 1
==========================================
process-id = 2
Arrival Time = 2
CPU time = 2
==========================================
process-id = 3
Arrival Time = 3
CPU time = 3
==========================================
process-id = 4
Arrival Time = 4
CPU time = 4
==========================================
process-id = 5
Arrival Time = 5
CPU time = 5
==========================================
NULL
==========================================
Anyway, hope it gives you some good ideas.