Array of linked lists in C
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
0
down vote
favorite
This is from an exercise I've done:
We want to implement a priority queue system to plan the execution of the system processes. An array of MAXPRI represents the priorities of the system, where 0 is the highest priority and MAXPRI-1 is the lowest priority. Each position i in the array will contain a dynamic linked list where the first element of this is the priority i process that must be executed (provided there are no processes available in the previous i-1 positions). Implement the following operations:
Create. Initialize array.
AddProcess. Given a priority and a process identifier, it adds the process to the end of the corresponding list.
ExecuteProcess. Remove from the list the most priority process. If there are no processes to execute, it will be indicated with a warning message.
Search. Given a process identifier it returns the priority of this one. If the id of the process does not exist, -1 will be returned.
Display. Go through the structure to show the existing processes that are available for execution sorted by priority (highest first).
main.c, linkedListArray.c, linkedListArray.h
#include "linkedListArray.h"
void Create(Node **queue)
for (int i = 0; i < MAXPRI; i++)
queue[i] = NULL;
void Push(Node **queue, int pri, int id)
Node *new_node = malloc(sizeof(Node));
if (new_node != NULL)
new_node->id = id;
new_node->next = NULL;
if (queue[pri] == NULL)
queue[pri] = new_node;
else
Node *aux = queue[pri];
while (aux->next != NULL)
aux = aux->next;
aux->next = new_node;
void Execute(Node **queue)
bool end = false;
int i = 0;
while (!end && i < MAXPRI)
if (queue[i] != NULL)
Node *aux = queue[i];
queue[i] = queue[i]->next;
free(aux);
end = true;
i++;
if (!end)
printf("There are no processesn");
int Search(Node **queue, unsigned int id)
int pri = -1;
Node *aux;
for (int i = 0; i < MAXPRI && pri == -1; ++i)
aux = queue[i];
while (aux != NULL && pri == -1)
if (aux->id == id)
pri = i;
else
aux = aux->next;
return pri;
void Output(Node *head)
for (Node *current = head; current != NULL; current = current->next)
if (current->next != NULL)
printf ("%d -> ", current->id);
else
printf ("%d", current->id);
void Display(Node **queue)
for (int i = 0; i < MAXPRI; i++)
printf ("Priority queue %d: ", i);
Output(queue[i]);
putchar('n');
putchar('n');
c linked-list
add a comment |Â
up vote
0
down vote
favorite
This is from an exercise I've done:
We want to implement a priority queue system to plan the execution of the system processes. An array of MAXPRI represents the priorities of the system, where 0 is the highest priority and MAXPRI-1 is the lowest priority. Each position i in the array will contain a dynamic linked list where the first element of this is the priority i process that must be executed (provided there are no processes available in the previous i-1 positions). Implement the following operations:
Create. Initialize array.
AddProcess. Given a priority and a process identifier, it adds the process to the end of the corresponding list.
ExecuteProcess. Remove from the list the most priority process. If there are no processes to execute, it will be indicated with a warning message.
Search. Given a process identifier it returns the priority of this one. If the id of the process does not exist, -1 will be returned.
Display. Go through the structure to show the existing processes that are available for execution sorted by priority (highest first).
main.c, linkedListArray.c, linkedListArray.h
#include "linkedListArray.h"
void Create(Node **queue)
for (int i = 0; i < MAXPRI; i++)
queue[i] = NULL;
void Push(Node **queue, int pri, int id)
Node *new_node = malloc(sizeof(Node));
if (new_node != NULL)
new_node->id = id;
new_node->next = NULL;
if (queue[pri] == NULL)
queue[pri] = new_node;
else
Node *aux = queue[pri];
while (aux->next != NULL)
aux = aux->next;
aux->next = new_node;
void Execute(Node **queue)
bool end = false;
int i = 0;
while (!end && i < MAXPRI)
if (queue[i] != NULL)
Node *aux = queue[i];
queue[i] = queue[i]->next;
free(aux);
end = true;
i++;
if (!end)
printf("There are no processesn");
int Search(Node **queue, unsigned int id)
int pri = -1;
Node *aux;
for (int i = 0; i < MAXPRI && pri == -1; ++i)
aux = queue[i];
while (aux != NULL && pri == -1)
if (aux->id == id)
pri = i;
else
aux = aux->next;
return pri;
void Output(Node *head)
for (Node *current = head; current != NULL; current = current->next)
if (current->next != NULL)
printf ("%d -> ", current->id);
else
printf ("%d", current->id);
void Display(Node **queue)
for (int i = 0; i < MAXPRI; i++)
printf ("Priority queue %d: ", i);
Output(queue[i]);
putchar('n');
putchar('n');
c linked-list
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
This is from an exercise I've done:
We want to implement a priority queue system to plan the execution of the system processes. An array of MAXPRI represents the priorities of the system, where 0 is the highest priority and MAXPRI-1 is the lowest priority. Each position i in the array will contain a dynamic linked list where the first element of this is the priority i process that must be executed (provided there are no processes available in the previous i-1 positions). Implement the following operations:
Create. Initialize array.
AddProcess. Given a priority and a process identifier, it adds the process to the end of the corresponding list.
ExecuteProcess. Remove from the list the most priority process. If there are no processes to execute, it will be indicated with a warning message.
Search. Given a process identifier it returns the priority of this one. If the id of the process does not exist, -1 will be returned.
Display. Go through the structure to show the existing processes that are available for execution sorted by priority (highest first).
main.c, linkedListArray.c, linkedListArray.h
#include "linkedListArray.h"
void Create(Node **queue)
for (int i = 0; i < MAXPRI; i++)
queue[i] = NULL;
void Push(Node **queue, int pri, int id)
Node *new_node = malloc(sizeof(Node));
if (new_node != NULL)
new_node->id = id;
new_node->next = NULL;
if (queue[pri] == NULL)
queue[pri] = new_node;
else
Node *aux = queue[pri];
while (aux->next != NULL)
aux = aux->next;
aux->next = new_node;
void Execute(Node **queue)
bool end = false;
int i = 0;
while (!end && i < MAXPRI)
if (queue[i] != NULL)
Node *aux = queue[i];
queue[i] = queue[i]->next;
free(aux);
end = true;
i++;
if (!end)
printf("There are no processesn");
int Search(Node **queue, unsigned int id)
int pri = -1;
Node *aux;
for (int i = 0; i < MAXPRI && pri == -1; ++i)
aux = queue[i];
while (aux != NULL && pri == -1)
if (aux->id == id)
pri = i;
else
aux = aux->next;
return pri;
void Output(Node *head)
for (Node *current = head; current != NULL; current = current->next)
if (current->next != NULL)
printf ("%d -> ", current->id);
else
printf ("%d", current->id);
void Display(Node **queue)
for (int i = 0; i < MAXPRI; i++)
printf ("Priority queue %d: ", i);
Output(queue[i]);
putchar('n');
putchar('n');
c linked-list
This is from an exercise I've done:
We want to implement a priority queue system to plan the execution of the system processes. An array of MAXPRI represents the priorities of the system, where 0 is the highest priority and MAXPRI-1 is the lowest priority. Each position i in the array will contain a dynamic linked list where the first element of this is the priority i process that must be executed (provided there are no processes available in the previous i-1 positions). Implement the following operations:
Create. Initialize array.
AddProcess. Given a priority and a process identifier, it adds the process to the end of the corresponding list.
ExecuteProcess. Remove from the list the most priority process. If there are no processes to execute, it will be indicated with a warning message.
Search. Given a process identifier it returns the priority of this one. If the id of the process does not exist, -1 will be returned.
Display. Go through the structure to show the existing processes that are available for execution sorted by priority (highest first).
main.c, linkedListArray.c, linkedListArray.h
#include "linkedListArray.h"
void Create(Node **queue)
for (int i = 0; i < MAXPRI; i++)
queue[i] = NULL;
void Push(Node **queue, int pri, int id)
Node *new_node = malloc(sizeof(Node));
if (new_node != NULL)
new_node->id = id;
new_node->next = NULL;
if (queue[pri] == NULL)
queue[pri] = new_node;
else
Node *aux = queue[pri];
while (aux->next != NULL)
aux = aux->next;
aux->next = new_node;
void Execute(Node **queue)
bool end = false;
int i = 0;
while (!end && i < MAXPRI)
if (queue[i] != NULL)
Node *aux = queue[i];
queue[i] = queue[i]->next;
free(aux);
end = true;
i++;
if (!end)
printf("There are no processesn");
int Search(Node **queue, unsigned int id)
int pri = -1;
Node *aux;
for (int i = 0; i < MAXPRI && pri == -1; ++i)
aux = queue[i];
while (aux != NULL && pri == -1)
if (aux->id == id)
pri = i;
else
aux = aux->next;
return pri;
void Output(Node *head)
for (Node *current = head; current != NULL; current = current->next)
if (current->next != NULL)
printf ("%d -> ", current->id);
else
printf ("%d", current->id);
void Display(Node **queue)
for (int i = 0; i < MAXPRI; i++)
printf ("Priority queue %d: ", i);
Output(queue[i]);
putchar('n');
putchar('n');
c linked-list
asked Mar 1 at 11:32
Julio
1
1
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
1
down vote
An
if
clause ofOutput
is only executed once, on the very first node. You may avoid this logic altogether by making it explicit:if (!head)
return;
printf("%d", head->id);
for (head = head->next; head; head = head->next)
printf(" -> %d", head->id);A missing
printf("n");
seems like a bug (I hope it is a copy-paste error).
An early
return
helps to avoid unnecessary variables. ConsiderSearch
:for (int i = 0; i < MAXPRI; i++)
for (Node * aux = queue[i]; aux; aux = aux->next)
if (aux->id == id)
return i;
}
return -1;A perk benefit is that now you can rename meaningless
i
to a more reasonablepri
.Similarly, if
Execute
returns as soon as a process is found, you wouldn't needbool end
.I strongly recommend to factor the
while (aux->next != NULL)
aux = aux->next;
loop into a function (say,
get_tail
), and streamlinePush
:Node * node = new_node(id);
Node * tail = get_tail(pri);
if (tail)
tail->next = node;
else
queue[pri] = node;Using extra memory may significantly reduce the run time. In your case, the
struct list
Node * head;
Node * tail;
;would reduce looking for tail complexity from linear to $O(1)$.
linkedListArray.h
shall only export the names defined inlinkedListArray.c
. The#include
files required forlinkedListArray.c
compilation should be included directly inlinkedListArray.c
.
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
An
if
clause ofOutput
is only executed once, on the very first node. You may avoid this logic altogether by making it explicit:if (!head)
return;
printf("%d", head->id);
for (head = head->next; head; head = head->next)
printf(" -> %d", head->id);A missing
printf("n");
seems like a bug (I hope it is a copy-paste error).
An early
return
helps to avoid unnecessary variables. ConsiderSearch
:for (int i = 0; i < MAXPRI; i++)
for (Node * aux = queue[i]; aux; aux = aux->next)
if (aux->id == id)
return i;
}
return -1;A perk benefit is that now you can rename meaningless
i
to a more reasonablepri
.Similarly, if
Execute
returns as soon as a process is found, you wouldn't needbool end
.I strongly recommend to factor the
while (aux->next != NULL)
aux = aux->next;
loop into a function (say,
get_tail
), and streamlinePush
:Node * node = new_node(id);
Node * tail = get_tail(pri);
if (tail)
tail->next = node;
else
queue[pri] = node;Using extra memory may significantly reduce the run time. In your case, the
struct list
Node * head;
Node * tail;
;would reduce looking for tail complexity from linear to $O(1)$.
linkedListArray.h
shall only export the names defined inlinkedListArray.c
. The#include
files required forlinkedListArray.c
compilation should be included directly inlinkedListArray.c
.
add a comment |Â
up vote
1
down vote
An
if
clause ofOutput
is only executed once, on the very first node. You may avoid this logic altogether by making it explicit:if (!head)
return;
printf("%d", head->id);
for (head = head->next; head; head = head->next)
printf(" -> %d", head->id);A missing
printf("n");
seems like a bug (I hope it is a copy-paste error).
An early
return
helps to avoid unnecessary variables. ConsiderSearch
:for (int i = 0; i < MAXPRI; i++)
for (Node * aux = queue[i]; aux; aux = aux->next)
if (aux->id == id)
return i;
}
return -1;A perk benefit is that now you can rename meaningless
i
to a more reasonablepri
.Similarly, if
Execute
returns as soon as a process is found, you wouldn't needbool end
.I strongly recommend to factor the
while (aux->next != NULL)
aux = aux->next;
loop into a function (say,
get_tail
), and streamlinePush
:Node * node = new_node(id);
Node * tail = get_tail(pri);
if (tail)
tail->next = node;
else
queue[pri] = node;Using extra memory may significantly reduce the run time. In your case, the
struct list
Node * head;
Node * tail;
;would reduce looking for tail complexity from linear to $O(1)$.
linkedListArray.h
shall only export the names defined inlinkedListArray.c
. The#include
files required forlinkedListArray.c
compilation should be included directly inlinkedListArray.c
.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
An
if
clause ofOutput
is only executed once, on the very first node. You may avoid this logic altogether by making it explicit:if (!head)
return;
printf("%d", head->id);
for (head = head->next; head; head = head->next)
printf(" -> %d", head->id);A missing
printf("n");
seems like a bug (I hope it is a copy-paste error).
An early
return
helps to avoid unnecessary variables. ConsiderSearch
:for (int i = 0; i < MAXPRI; i++)
for (Node * aux = queue[i]; aux; aux = aux->next)
if (aux->id == id)
return i;
}
return -1;A perk benefit is that now you can rename meaningless
i
to a more reasonablepri
.Similarly, if
Execute
returns as soon as a process is found, you wouldn't needbool end
.I strongly recommend to factor the
while (aux->next != NULL)
aux = aux->next;
loop into a function (say,
get_tail
), and streamlinePush
:Node * node = new_node(id);
Node * tail = get_tail(pri);
if (tail)
tail->next = node;
else
queue[pri] = node;Using extra memory may significantly reduce the run time. In your case, the
struct list
Node * head;
Node * tail;
;would reduce looking for tail complexity from linear to $O(1)$.
linkedListArray.h
shall only export the names defined inlinkedListArray.c
. The#include
files required forlinkedListArray.c
compilation should be included directly inlinkedListArray.c
.
An
if
clause ofOutput
is only executed once, on the very first node. You may avoid this logic altogether by making it explicit:if (!head)
return;
printf("%d", head->id);
for (head = head->next; head; head = head->next)
printf(" -> %d", head->id);A missing
printf("n");
seems like a bug (I hope it is a copy-paste error).
An early
return
helps to avoid unnecessary variables. ConsiderSearch
:for (int i = 0; i < MAXPRI; i++)
for (Node * aux = queue[i]; aux; aux = aux->next)
if (aux->id == id)
return i;
}
return -1;A perk benefit is that now you can rename meaningless
i
to a more reasonablepri
.Similarly, if
Execute
returns as soon as a process is found, you wouldn't needbool end
.I strongly recommend to factor the
while (aux->next != NULL)
aux = aux->next;
loop into a function (say,
get_tail
), and streamlinePush
:Node * node = new_node(id);
Node * tail = get_tail(pri);
if (tail)
tail->next = node;
else
queue[pri] = node;Using extra memory may significantly reduce the run time. In your case, the
struct list
Node * head;
Node * tail;
;would reduce looking for tail complexity from linear to $O(1)$.
linkedListArray.h
shall only export the names defined inlinkedListArray.c
. The#include
files required forlinkedListArray.c
compilation should be included directly inlinkedListArray.c
.
answered Mar 1 at 19:15
vnp
36.5k12991
36.5k12991
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f188594%2farray-of-linked-lists-in-c%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password