Question
QUESTION 1. Consider the following linked list node structure: struct node{ int data; struct node *next; }; Implement the following basic functions of the linear
QUESTION 1.
Consider the following linked list node structure:
struct node{
int data;
struct node *next;
};
Implement the following basic functions of the linear linked list structure:
- Check empty list
int is_empty( struct node *header)
{
if(header==NULL)
return 1; //TRUE
else
return 0; //FALSE
}
- Create a node
struct node * newnode(int d)
{
struct node *temp;
temp=(struct node *) malloc(sizeof(node));
temp->data =d;
temp->next=NULL;
return temp;
}
- Insert a node into front/back/middle of LL
struct node * insertFront(struct node *header, int d)
{
struct node *temp;
temp=newnode(d);
temp->next =header;
header=temp;
return header;
}
struct node * insertBack(struct node *header, int d)
{
struct node *temp, *headertemp;
temp=newnode(d);
if(header==NULL)
{
header=temp;
return header;
}
headertemp=header;
while(headertemp->next!=NULL)
headertemp =headertemp->next;
headertemp->next=temp;
return header;
}
void insertAfter(struct node *afterNode, int d)
{
struct node *temp;
temp=newnode(d);
temp->next=afterNode->next;
afterNode->next=temp;
}
- Delete a node from front/back/middle of LL
struct node * deleteFront(struct node *header)
{
struct node *temp;
if(header==NULL)
return header;
temp=header;
header= header->next;
free(temp);
return header;
}
struct node * deleteBack(struct node *header)
{
struct node *temp, *headertemp;
if(header==NULL)
return header;
headertemp=header;
while(headertemp->next->next!=NULL)
headertemp =headertemp->next;
temp=headertemp->next;
headertemp->next=NULL;
free(temp);
return header;
}
void deleteAfter(struct node *afterNode)
{
struct node *temp;
if(afterNode->next==NULL || afterNode==NULL)
return;
temp =afterNode->next;
afterNode->next=temp->next;
free(temp);
}
Task 1: Write down a complete C/C++ program to test your linear linked list implementation. Additionally, write another function which will be used to list the linked list content. Complete your implementation using the following code:
header = insertBack(header,2);
header = insertBack(header,4);
header = insertBack(header,6);
DisplayList(header);
header = insertFront(header,1);
DisplayList(header);
insertAfter(header->next->next,5);
DisplayList(header);
header = deleteFront(header);
DisplayList(header);
header = deleteBack(header);
DisplayList(header);
deleteAfter(header->next);
DisplayList(header);
Task 2: Write a function that finds the average of the elements of a linked list. Use the following prototype. Modify your linked list data field to be a float and insert initial floats (1.5, 2.5, 3.5, 4.5) to test your function.
float averagelist (struct node *);
Task 3: Write a function that counts and returns 'A' grades which are stored in the linked list. Use the following prototype. Modify your linked list data field to be a character and insert initial characters (A,B,A,C,D) to test your function.
int countA (struct node *);
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started