Consider the following data structure: typedef struct Node_t Node_t; struct Node
ID: 3575648 • Letter: C
Question
Consider the following data structure: typedef struct Node_t Node_t; struct Node t {float value; Node_t *next; Also consider the following diagram showing one possible configuration of nodes using the data structure defined above: Assuming the data-structure declaration in the box above is already given in of some C file, write the remaining C code necessary in order to create with dynamic memory the structure shown in the diagram. Some marks will be given for the quality of your answer. The next page (page 13) is available for your work/answer if needed.Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Node_t Node_t;
//Structure declaration
struct Node_t
{
//to store data
float value;
//for next pointer position
Node_t *next;
};
//Function prototype
Node_t *InsFront(Node_t*, float);
Node_t *InsBack(Node_t *, float);
Node_t *InsPos(Node_t *, float,int);
Node_t *DelNode(Node_t *, float);
Node_t *SrchUpdate(Node_t *, float);
void Display(Node_t *);
// Main Program
int main()
{
Node_t *start = NULL;
int choice, insopn, p;
float data;
//Loops until 5 is entered
do
{
printf(" ### Linked List Operations ### ");
printf(" 1 - Insertion 2 - Deletion 3 - Update 4 - Display 5 - Exit ");
printf(" Enter Your Choice? ");
fflush(stdin);
scanf("%d", &choice);
switch(choice)
{
//Insert operation
case 1:
//Clear buffer
fflush(stdin);
printf(" Insertion at: 1 -> Front 2 -> Back 3 -> Position Enter ? ");
scanf("%d",&insopn);
fflush(stdin);
printf(" Enter a number to Insert?");
scanf("%f", &data);
//Insert front position
if( insopn == 1)
start = InsFront(start, data);
//Insert back position
else if(insopn == 2)
start = InsBack(start, data);
//Insert specified position
else if(insopn == 3)
{
fflush(stdin);
printf(" Enter the Position ? ");
scanf("%d", &p);
start = InsPos(start, data, p);
}
break;
//Delete operation
case 2:
fflush(stdin);
printf(" Enter the value of the Node to be deleted ? ");
scanf("%f", &data);
start = DelNode(start, data);
break;
//Search and Update operation
case 3:
fflush(stdin);
printf(" Enter the value of the Node to be Searched ? ");
scanf("%f", &data);
start = SrchUpdate(start, data);
break;
//Display
case 4:
printf(" Contents of Linked List is ");
Display(start);
break;
//Exit
case 5:
printf(" Terminating Thanks ");
break;
default:
printf(" Invalid Choice !!! Try Again !! ");
break;
}
}while(choice != 5);
}
//Insert at front
Node_t *InsFront(Node_t *st, float data)
{
Node_t *temp;
//Creates a new node
temp = (Node_t *)malloc(sizeof(Node_t));
//Error handling if memory is not allocated
if( temp == NULL)
{
printf(" Out of Memory !! Overflow !!!");
return(st);
}
else
{
//Stores data in the node
temp -> value = data;
//next becomes null to indicate end
temp -> next = st;
printf(" Node has been inserted at Front Successfully !!");
return(temp);
}
}
//Insert at back position
Node_t *InsBack(Node_t *st, float data)
{
Node_t *temp,*t;
//Creates a new node
temp = (Node_t *)malloc(sizeof(Node_t));
//Error handling if memory is not allocated
if( temp == NULL)
{
printf(" Out of Memory !! Overflow !!!");
return(st);
}
else
{
//Stores data in a node
temp->value = data;
//next becomes null to indicate end
temp->next = NULL;
if(st == NULL)
return(temp);
else
{
t = st;
//Moves till end
while( t->next != NULL)
t = t->next;
t->next = temp;
printf(" Node has been inserted at Back Successfully !!");
return(st);
}
}
}
//Insert at a specified position
Node_t *InsPos(Node_t *st, float data, int pos)
{
Node_t *temp,*t,*prev;
int cnt;
//Creates a new node
temp = (Node_t *)malloc(sizeof(Node_t));
//Error handling if memory is not allocated
if( temp == NULL)
{
printf(" Out of Memory !! Overflow !!!");
return(st);
}
else
{
//Stores data in node
temp -> value = data;
//next becomes null to indicate end
temp -> next = NULL;
if(pos == 1) /* Front Insertion */
{
temp -> next = st;
return(temp);
}
else
{
t = st; cnt = 1;
//Moves till the specified position
while( t != NULL && cnt != pos)
{
prev = t;
t = t -> next;
cnt++;
}
if(t) /* valid Position Insert new node*/
{
prev -> next = temp;
temp -> next = t;
}
else
printf(" Invalid Position !!!");
printf(" Node has been inserted at given Position Successfully !!");
return(st);
}
}
}
//Delete a specified node
Node_t *DelNode(Node_t *st, float data)
{
Node_t *t,*prev;
//Error handling if list is empty
if( st == NULL)
{
printf(" Underflow!!!");
return(st);
}
else
{
t = st;
//Front Deletion
if(st -> value == data)
{
st = st -> next;
t -> next = NULL;
free(t);
return(st);
}
else
{
//Moves till specified data or end
while( t != NULL && t -> value != data)
{
prev = t;
t = t -> next;
}
if(t) /* node to be deleted found*/
{
prev -> next = t -> next;
t -> next = NULL;
free(t);
}
else
printf(" Invalid value !!!");
return(st);
}
}
}
//Search a specified data in list and update it
Node_t *SrchUpdate(Node_t *st, float data)
{
Node_t *t;
//Error handling if list is empty
if( st == NULL)
{
printf(" Empty List !!");
return(st);
}
else
{
t = st;
//Moves till specified data or end
while( t != NULL && t -> value != data)
{
t = t -> next;
}
if(t) /* node to be Updated found*/
{
printf(" Node with value %.4f found in the List ! ", data);
printf(" Enter the new value ");
scanf("%f", &t->value);
}
else
printf(" Invalid Value !!!");
return(st);
}
}
//Displays the linked list data
void Display(Node_t *st)
{
Node_t *t;
if( st == NULL)
printf("Empty List ");
else
{
t = st;
printf("Start -> ");
while(t)
{
printf("[%.4f] -> ", t->value);
t = t->next;
}
printf(" Null ");
}
}
Output:
### Linked List Operations ###
1 - Insertion
2 - Deletion
3 - Update
4 - Display
5 - Exit
Enter Your Choice? 1
Insertion at:
1 -> Front
2 -> Back
3 -> Position
Enter ? 1
Enter a number to Insert?100.23
Node has been inserted at Front Successfully !!
### Linked List Operations ###
1 - Insertion
2 - Deletion
3 - Update
4 - Display
5 - Exit
Enter Your Choice? 1
Insertion at:
1 -> Front
2 -> Back
3 -> Position
Enter ? 1
Enter a number to Insert?23.56
Node has been inserted at Front Successfully !!
### Linked List Operations ###
1 - Insertion
2 - Deletion
3 - Update
4 - Display
5 - Exit
Enter Your Choice? 4
Contents of Linked List is
Start -> [23.5600] -> [100.2300] -> Null
### Linked List Operations ###
1 - Insertion
2 - Deletion
3 - Update
4 - Display
5 - Exit
Enter Your Choice? 1
Insertion at:
1 -> Front
2 -> Back
3 -> Position
Enter ? 2
Enter a number to Insert?56.45
Node has been inserted at Back Successfully !!
### Linked List Operations ###
1 - Insertion
2 - Deletion
3 - Update
4 - Display
5 - Exit
Enter Your Choice? 4
Contents of Linked List is
Start -> [23.5600] -> [100.2300] -> [56.4500] -> Null
### Linked List Operations ###
1 - Insertion
2 - Deletion
3 - Update
4 - Display
5 - Exit
Enter Your Choice? 1
Insertion at:
1 -> Front
2 -> Back
3 -> Position
Enter ? 3
Enter a number to Insert?2
Enter the Position ? 89.99
Invalid Position !!! Node has been inserted at given Position Successfully !!
### Linked List Operations ###
1 - Insertion
2 - Deletion
3 - Update
4 - Display
5 - Exit
Enter Your Choice? 4
Contents of Linked List is
Start -> [23.5600] -> [100.2300] -> [56.4500] -> Null
### Linked List Operations ###
1 - Insertion
2 - Deletion
3 - Update
4 - Display
5 - Exit
Enter Your Choice? 2
Enter the value of the Node to be deleted ? 23.56
### Linked List Operations ###
1 - Insertion
2 - Deletion
3 - Update
4 - Display
5 - Exit
Enter Your Choice? 4
Contents of Linked List is
Start -> [100.2300] -> [56.4500] -> Null
### Linked List Operations ###
1 - Insertion
2 - Deletion
3 - Update
4 - Display
5 - Exit
Enter Your Choice? 3
Enter the value of the Node to be Searched ? 56.45
Node with value 56.4500 found in the List !
Enter the new value
200.23
### Linked List Operations ###
1 - Insertion
2 - Deletion
3 - Update
4 - Display
5 - Exit
Enter Your Choice? 4
Contents of Linked List is
Start -> [100.2300] -> [200.2300] -> Null
### Linked List Operations ###
1 - Insertion
2 - Deletion
3 - Update
4 - Display
5 - Exit
Enter Your Choice? 5
Terminating Thanks
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.