*In C please* Write a program: (a) Typedef a structure that can be used to repre
ID: 3548133 • Letter: #
Question
*In C please*
Write a program:
(a) Typedef a structure that can be used to represent a node in a linked list. The
structure info_node has the following components:
1. job_title (25 characters)
2. hourly_wage
3. a pointer to a struct info_node
(b) Write a print_list which prints out the job title and hourly_wage of each node in
the linked list it is passed.
(c) Write an add_node which adds a new node to the end of the list. It is passed the
list and the job title and wage to be added.
To Test the functions write a driver which:
#1 Uses the add_node to add the following data into nodes
Job Title Hourly Wage
Programmer 32.35
Analyst 25.80
Technician 17.50
Clerk 12.00
Manager 53.58
#2 Then calls print_list to show the above nodes were added.
Here is the main program (driver):
/*You need to add the function definitions after main*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct node_s { /* define node structure */
char job_title[25];
double hourly_wage;
struct node_s *next;
} node_t;
/* PROTOTYPES OF FUNCTIONS */
void print_list(node_t *list);
void add_node(node_t **head, char *title, double hwage);
int main() /* start of main */
{
node_t *list;
list = NULL;
/* add data to the list and print after each addition to the list */
add_node(&list, "Programmer", 32.35);
print_list(list);
add_node(&list, "Analyst", 25.80);
print_list(list);
add_node(&list, "Technician", 17.50);
print_list(list);
add_node(&list, "Clerk", 12.00);
print_list(list);
add_node(&list, "Manager", 53.58);
print_list(list);
return(0);
} /* end of main */
/* YOU MUST ADD THE FUNCTION DEFINITIONS */
Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct node_s { /* define node structure */
char job_title[25];
double hourly_wage;
struct node_s *next;
} node_t;
/* PROTOTYPES OF FUNCTIONS */
void print_list(node_t *list);
void add_node(node_t **head, char *title, double hwage);
int main() /* start of main */
{
node_t *list;
list = NULL;
/* add data to the list and print after each addition to the list */
add_node(&list, "Programmer", 32.35);
print_list(list);
add_node(&list, "Analyst", 25.80);
print_list(list);
add_node(&list, "Technician", 17.50);
print_list(list);
add_node(&list, "Clerk", 12.00);
print_list(list);
add_node(&list, "Manager", 53.58);
print_list(list);
system("pause");
return(0);
} /* end of main */
/* YOU MUST ADD THE FUNCTION DEFINITIONS */
void print_list(node_t *list)
{
node_t *current;
current = list;
while(current != NULL){
printf("Job Title: %s Hourly Wage: %0.2f ", current->job_title, current->hourly_wage);
current = current->next;
}
printf(" ");
}
void add_node(node_t **head, char *title, double hwage)
{
node_t* curr = *head;
node_t* prev = NULL;
while (curr != NULL)
{
prev = curr;
curr = curr->next;
}
node_t* ptr = (node_t*) malloc (sizeof(node_t));
strcpy(ptr->job_title, title);
ptr->hourly_wage = hwage;
ptr->next = curr;
if (prev == NULL)
{
*head = ptr;
}
else
{
prev->next = ptr;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.