'Cannot create linked list in C using information in structure array
I'm basically trying to create a linked list / queue out of the data in a struct array, but it refuses to work.
Here's the code, tried allocating memory to front but to no avail, the output is just the while loop infinitely looping.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node
{
char ID[12];
long arrtime;
double priority;
struct node *next;
} request;
void main()
{
request *front;
request *rear;
request *new = (request *)malloc(sizeof(request));
request *arr = (request *)malloc(100 * sizeof(request));
for (int k = 0; k < 4; k++)
{
printf("ID:: ");
scanf("%s", arr[k].ID);
printf("Arrival time:: ");
scanf("%ld", &arr[k].arrtime);
printf("Priority::");
scanf("%lf", &arr[k].priority);
}
for (int k = 0; k < 4; k++)
{ printf("\n %s %ld %lf\n", arr[k].ID, arr[k].arrtime, arr[k].priority);
}
strcpy(new->ID, arr[0].ID);
new->arrtime = arr[0].arrtime;
new->priority = arr[0].priority;
front = new;
rear = front;
for (int i = 1; i < 4; i++)
{
strcpy(new->ID, arr[i].ID);
new->arrtime = arr[i].arrtime;
new->priority = arr[i].priority;
rear->next = new;
rear = new;
}
// print test!!
request *walker = front;
printf("\n............QUEUE CONTENT.........\n");
while (walker != NULL)
{
printf("\n%s %ld %.1lf\n", walker->ID, walker->arrtime, walker->priority);
walker = walker->next;
}
return;
}
Solution 1:[1]
You are not initializing new->next = NULL; for each item in the linked list (malloc() does not zero out the allocated memory), and you are not allocating a separate request object for each item in the linked list.
Try this instead:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node
{
char ID[12];
long arrtime;
double priority;
struct node *next;
} request;
int main()
{
request *front = NULL, *rear = NULL;
request *arr = (request *)malloc(4 * sizeof(request));
for (int k = 0; k < 4; ++k)
{
printf("ID:: ");
scanf("%s", arr[k].ID);
printf("Arrival time:: ");
scanf("%ld", &arr[k].arrtime);
printf("Priority::");
scanf("%lf", &arr[k].priority);
}
for (int k = 0; k < 4; ++k)
{
printf("\n %s %ld %lf\n", arr[k].ID, arr[k].arrtime, arr[k].priority);
}
request *new = (request*)malloc(sizeof(request));
strcpy(new->ID, arr[0].ID);
new->arrtime = arr[0].arrtime;
new->priority = arr[0].priority;
new->next = NULL;
front = rear = new;
for (int i = 1; i < 4; ++i)
{
new = (request*)malloc(sizeof(request));
strcpy(new->ID, arr[i].ID);
new->arrtime = arr[i].arrtime;
new->priority = arr[i].priority;
new->next = NULL;
rear->next = new;
rear = new;
}
// print test!!
request *walker = front;
printf("\n............QUEUE CONTENT.........\n");
while (walker != NULL)
{
printf("\n%s %ld %.1lf\n", walker->ID, walker->arrtime, walker->priority);
walker = walker->next;
}
walker = front;
while (walker != NULL)
{
request *next = walker->next;
free(walker);
walker = next;
}
free(arr);
return 0;
}
Alternatively, you don't need to allocate the 1st node outside of the loop, eg:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node
{
char ID[12];
long arrtime;
double priority;
struct node *next;
} request;
int main()
{
request *front = NULL, *rear = NULL;
request *arr = (request *)malloc(4 * sizeof(request));
for (int k = 0; k < 4; ++k)
{
printf("ID:: ");
scanf("%s", arr[k].ID);
printf("Arrival time:: ");
scanf("%ld", &arr[k].arrtime);
printf("Priority::");
scanf("%lf", &arr[k].priority);
}
for (int k = 0; k < 4; ++k)
{
printf("\n %s %ld %lf\n", arr[k].ID, arr[k].arrtime, arr[k].priority);
}
request **ptr = &front;
for (int i = 0; i < 4; ++i)
{
request *new = (request*)malloc(sizeof(request));
strcpy(new->ID, arr[i].ID);
new->arrtime = arr[i].arrtime;
new->priority = arr[i].priority;
new->next = NULL;
*ptr = rear = new;
ptr = &(rear->next);
}
// print test!!
request *walker = front;
printf("\n............QUEUE CONTENT.........\n");
while (walker != NULL)
{
printf("\n%s %ld %.1lf\n", walker->ID, walker->arrtime, walker->priority);
walker = walker->next;
}
walker = front;
while (walker != NULL)
{
request *next = walker->next;
free(walker);
walker = next;
}
free(arr);
return 0;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
