Please answer in C
A doubly linked list is a list in which each entry contains a pointer to the preceding entry in the list as well as a pointer to the next entry in the list. Define the appropriate structure for the doubly linked list. Write a user space program that implements a doubly linked list and prints out the elements of the list. Develop insert_entry() and remove_entry() functions for this list. The link list should store information on runners at a track meet that are on lanes 1 through 8. Information such as bib number, name, school, qualifier time, etc. must be stored.
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct runner {
int bib;
char name[100];
};
struct node
{
struct runner data;
struct node *prev;
struct node *next;
};
struct node *head;
void insert_entry(struct runner item) {
struct node *ptr, *temp;
ptr = (struct node *) malloc(sizeof(struct node));
if (ptr == NULL)
return;
ptr->data = item;
if (head == NULL)
{
ptr->next = NULL;
ptr->prev = NULL;
head = ptr;
}
else
{
temp = head;
while (temp->next != NULL)
{
temp = temp->next;
}
temp->next = ptr;
ptr ->prev = temp;
ptr->next = NULL;
}
}
void remove_entry() {
struct node *ptr;
if (head == NULL)
return;
if (head->next == NULL)
{
head = NULL;
free(head);
}
else
{
ptr = head;
if (ptr->next != NULL)
{
ptr = ptr -> next;
}
ptr -> prev -> next = NULL;
free(ptr);
}
}
void display() {
struct node *ptr;
printf("...Runners...\n\n");
ptr = head;
while (ptr != NULL)
{
printf("BIB Number: %d\t Name: %s\n", ptr->data.bib,ptr->data.name);
ptr = ptr->next;
}
}
int main () {
struct runner a;
a.bib = 1;
strcpy(a.name,"Michael");
insert_entry(a);
a.bib = 2;
strcpy(a.name,"Ramos");
insert_entry(a);
a.bib = 3;
strcpy(a.name,"Messi");
insert_entry(a);
a.bib = 4;
strcpy(a.name,"Benzema");
insert_entry(a);
a.bib = 5;
strcpy(a.name,"Cristiano");
insert_entry(a);
a.bib = 6;
strcpy(a.name,"Ronaldhino");
insert_entry(a);
a.bib = 7;
strcpy(a.name,"Ambape");
insert_entry(a);
a.bib = 8;
strcpy(a.name,"Neymar");
insert_entry(a);
display();
return 0;
}
OUTPUT:
Get Answers For Free
Most questions answered within 1 hours.