Question: May I ask for your help in giving me an idea for a C function that can sort my nodes based on their priority number through the linked list? I have my code attached below, but not the text file needed for this. I can provide a screenshot of it.   Code: #include #include #include // Struct to hold each member related to my variable. typedef struct { char variable[10]; int Burst_time; int Wait_time; int Turn_time; int priority; } element_t; // Struct that acts as my node in a linked list. typedef struct priority_node { element_t data; struct priority_node *next; } node_t; // Assigning the functions here that will be used throughout main. node_t *insert_node (int priority); void display_list (); // Here is my main function. int main(void) { int i = 0; char line[25]; // Place that stores all of my text. char *variable, *priority, *Burst_time, *Wait_time; int elem, sum1 = 0, sum2 = 0, priority2; element_t elem_struct; node_t *head = NULL, *temporary; // Creating a file pointer to read stuff from the file named known 'elements.txt' // File looks like this: // Line 1: P1 2 6 14 // Line 2: P2 4 12 3 // Line 3: P3 0 3 1 // Line 4: P4 1 10 15 FILE *fptr; fptr = fopen ("elements.txt", "r"); // Looping until end of the file is reached. while (!feof(fptr)) { fgets(line, 25, fptr); // Reading in a line. // Appending each string within file to the correct pointer------ variable = strtok(line, " "); priority = strtok(NULL, " "); Burst_time = strtok(NULL, " "); Wait_time = strtok(NULL, " ");//--------------------------------- printf("Variable: %s\t\t", variable); // Testing from here to printf("Priority: %s\t\t", priority); // | printf("Burst time: %s\t\t", Burst_time); // | printf("Waiting time: %s\n", Wait_time);// v here sum1 = atoi(Burst_time) + atoi(Wait_time); // Assigning each element to the member node ----- strcpy(elem_struct.variable, variable); elem_struct.priority = atoi(priority); elem_struct.Burst_time = atoi(Burst_time); elem_struct.Wait_time = atoi(Wait_time); elem_struct.Turn_time = sum1;// ------------------------------ // Insertion function is placed here temporary = insert_node(elem_struct.priority); temporary -> next = head; head = temporary; // Just random stuff that will be used later------------------- sum1 = 0; sum2 += sum1; // Placeholder for total sum of waiting time. i++; // ------------------------------------------------------- } fclose(fptr); // Closing file pointer to prevent errors. // Printing the linked list here to show what got inserted in it. display_list(head); return0; } // Here is my print function for printing out the linked list void display_list (node_t *Head) { node_t *temporary; temporary = Head; printf("Variable Priority Burst Time Waiting Time Turnaround time\n"); while (temporary != NULL) { printf("Var: %s\t", temporary -> data.variable); printf("Pr: %d\t", temporary -> data.priority); printf("Bt: %d\t", temporary -> data.Burst_time); printf("Wt: %d\t", temporary -> data.Wait_time); printf("Turn: %d\n", temporary -> data.Turn_time); temporary = temporary -> next; } } // Here is my insertion function node_t *insert_node (int priority) { node_t *newPtr = (node_t *)malloc(sizeof(node_t)); newPtr -> data.priority = priority; newPtr -> next = NULL; return newPtr; } /* All I need now is a function that can sort my nodes based on their priority number/member */

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Question:

May I ask for your help in giving me an idea for a C function that can sort my nodes based on their priority number through the linked list? I have my code attached below, but not the text file needed for this. I can provide a screenshot of it.

 

Code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Struct to hold each member related to my variable.
typedef struct {
char variable[10];
int Burst_time;
int Wait_time;
int Turn_time;
int priority;
} element_t;

// Struct that acts as my node in a linked list.
typedef struct priority_node {
element_t data;
struct priority_node *next;
} node_t;


// Assigning the functions here that will be used throughout main.
node_t *insert_node (int priority);
void display_list ();


// Here is my main function.
int main(void) {
int i = 0;
char line[25]; // Place that stores all of my text.
char *variable, *priority, *Burst_time, *Wait_time;
int elem, sum1 = 0, sum2 = 0, priority2;
element_t elem_struct;
node_t *head = NULL, *temporary;

// Creating a file pointer to read stuff from the file named known 'elements.txt'

// File looks like this:
// Line 1: P1 2 6 14
// Line 2: P2 4 12 3
// Line 3: P3 0 3 1
// Line 4: P4 1 10 15
FILE *fptr;
fptr = fopen ("elements.txt", "r");

// Looping until end of the file is reached.
while (!feof(fptr)) {

fgets(line, 25, fptr); // Reading in a line.

// Appending each string within file to the correct pointer------
variable = strtok(line, " ");
priority = strtok(NULL, " ");
Burst_time = strtok(NULL, " ");
Wait_time = strtok(NULL, " ");//---------------------------------
printf("Variable: %s\t\t", variable); // Testing from here to
printf("Priority: %s\t\t", priority); // |
printf("Burst time: %s\t\t", Burst_time); // |
printf("Waiting time: %s\n", Wait_time);// v here

sum1 = atoi(Burst_time) + atoi(Wait_time); // Assigning each element to the member node -----
strcpy(elem_struct.variable, variable);
elem_struct.priority = atoi(priority);
elem_struct.Burst_time = atoi(Burst_time);
elem_struct.Wait_time = atoi(Wait_time);
elem_struct.Turn_time = sum1;// ------------------------------

// Insertion function is placed here
temporary = insert_node(elem_struct.priority);
temporary -> next = head;
head = temporary;

// Just random stuff that will be used later-------------------
sum1 = 0;
sum2 += sum1; // Placeholder for total sum of waiting time.
i++; // -------------------------------------------------------
}

fclose(fptr); // Closing file pointer to prevent errors.

// Printing the linked list here to show what got inserted in it.
display_list(head);

return0;
}


// Here is my print function for printing out the linked list
void display_list (node_t *Head)
{
node_t *temporary;
temporary = Head;

printf("Variable Priority Burst Time Waiting Time Turnaround time\n");

while (temporary != NULL) {
printf("Var: %s\t", temporary -> data.variable);
printf("Pr: %d\t", temporary -> data.priority);
printf("Bt: %d\t", temporary -> data.Burst_time);
printf("Wt: %d\t", temporary -> data.Wait_time);
printf("Turn: %d\n", temporary -> data.Turn_time);

temporary = temporary -> next;
}
}


// Here is my insertion function
node_t *insert_node (int priority) {
node_t *newPtr = (node_t *)malloc(sizeof(node_t));
newPtr -> data.priority = priority;
newPtr -> next = NULL;
return newPtr;
}

/* All I need now is a function that can sort my nodes based on their priority number/member */
I Blackboard
CHE 2421_TH X
Bb Microsoft W X
Ascii Table
O Repl.it - CS
O Repl.it - CS_ X
A Core Team
CO Lesson_2_(BI X
I Learning_Ce x
ь Му Questioг X
+
A repl.it/@christospappas/CS2413Project1Ver2#elements.txt
christospappas / CS_2413_Project1_V...
Run
9 Upgrade
8+ Share
+
elements.txt
Console
Shell
Files
1
P1 2 6 14
> clang-7 -pthread -lm -o main main.c
> ./main
© main.c
2
P2 4 12 3
P3 0 3 1
P4 1 10 15
3
Variable: Pl
Priority: 2
Burst time: 6
Waiting time: 14
O elements.txt
4
Variable: P2
Priority: 4
Burst time: 12
Waiting time: 3
O main
Variable: P3
Priority: 0
Burst time: 3
Waiting time: 1
Variable: P4
Priority: 1
Burst time: 10
Waiting time: 15
Variable
Priority
Burst Time
Waiting Time
Turnaround time
Var:
Pr: 1
Bt: 0
Wt: 0
Turn: 0
Var:
Pr: 0
Bt: 0
Wt: 0
Turn: 0
Var:
Pr: 4
Bt: 0
Wt: 0
Turn: 0
Var:
Pr: 2
Bt: 0
Wt: 0
Turn: 0
?
Transcribed Image Text:I Blackboard CHE 2421_TH X Bb Microsoft W X Ascii Table O Repl.it - CS O Repl.it - CS_ X A Core Team CO Lesson_2_(BI X I Learning_Ce x ь Му Questioг X + A repl.it/@christospappas/CS2413Project1Ver2#elements.txt christospappas / CS_2413_Project1_V... Run 9 Upgrade 8+ Share + elements.txt Console Shell Files 1 P1 2 6 14 > clang-7 -pthread -lm -o main main.c > ./main © main.c 2 P2 4 12 3 P3 0 3 1 P4 1 10 15 3 Variable: Pl Priority: 2 Burst time: 6 Waiting time: 14 O elements.txt 4 Variable: P2 Priority: 4 Burst time: 12 Waiting time: 3 O main Variable: P3 Priority: 0 Burst time: 3 Waiting time: 1 Variable: P4 Priority: 1 Burst time: 10 Waiting time: 15 Variable Priority Burst Time Waiting Time Turnaround time Var: Pr: 1 Bt: 0 Wt: 0 Turn: 0 Var: Pr: 0 Bt: 0 Wt: 0 Turn: 0 Var: Pr: 4 Bt: 0 Wt: 0 Turn: 0 Var: Pr: 2 Bt: 0 Wt: 0 Turn: 0 ?
Expert Solution
Step 1

Note: Assuming 0 is the highest priority. If you want to take 4 as the highest priority change if(temp1->data.priority>temp2->data.priority) in the sort function to if(temp1->data.priority<temp2->data.priority). In the output, it is showing the garbage value for burst, turn around time, waiting time because in the insert you are just initializing the priority variable.

 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Struct to hold each member related to my variable.
typedef struct {
char variable[10];
int Burst_time;
int Wait_time;
int Turn_time;
int priority;
} element_t;

// Struct that acts as my node in a linked list.
typedef struct priority_node {
element_t data;
struct priority_node *next;
} node_t;


// Assigning the functions here that will be used throughout main.
node_t *insert_node (int priority);
void display_list ();

node_t *sort(node_t *Head);


// Here is my main function.
int main(void) {
int i = 0;
char line[25]; // Place that stores all of my text.
char *variable, *priority, *Burst_time, *Wait_time;
int elem, sum1 = 0, sum2 = 0, priority2;
element_t elem_struct;
node_t *head = NULL, *temporary;

// Creating a file pointer to read stuff from the file named known 'elements.txt'

// File looks like this:
// Line 1: P1 2 6 14
// Line 2: P2 4 12 3
// Line 3: P3 0 3 1
// Line 4: P4 1 10 15
FILE *fptr;
fptr = fopen ("C:\\Users\\dell\\OneDrive\\Desktop\\elements.txt", "r");

// Looping until end of the file is reached.
while (!feof(fptr)) {

fgets(line, 25, fptr); // Reading in a line.

// Appending each string within file to the correct pointer------
variable = strtok(line, " ");
priority = strtok(NULL, " ");
Burst_time = strtok(NULL, " ");
Wait_time = strtok(NULL, " ");//---------------------------------
printf("Variable: %s\t\t", variable); // Testing from here to
printf("Priority: %s\t\t", priority); // |
printf("Burst time: %s\t\t", Burst_time); // |
printf("Waiting time: %s\n", Wait_time);// v here

sum1 = atoi(Burst_time) + atoi(Wait_time); // Assigning each element to the member node -----
strcpy(elem_struct.variable, variable);
elem_struct.priority = atoi(priority);
elem_struct.Burst_time = atoi(Burst_time);
elem_struct.Wait_time = atoi(Wait_time);
elem_struct.Turn_time = sum1;// ------------------------------

// Insertion function is placed here
temporary = insert_node(elem_struct.priority);
temporary -> next = head;
head = temporary;

// Just random stuff that will be used later-------------------
sum1 = 0;
sum2 += sum1; // Placeholder for total sum of waiting time.
i++; // -------------------------------------------------------
}

fclose(fptr); // Closing file pointer to prevent errors.

// Printing the linked list here to show what got inserted in it.
display_list(head);

head = sort(head);
display_list(head);

return 0;
}


// Here is my print function for printing out the linked list
void display_list (node_t *Head)
{
node_t *temporary;
temporary = Head;

printf("Variable Priority Burst Time Waiting Time Turnaround time\n");

while (temporary != NULL) {
printf("Var: %s\t", temporary -> data.variable);
printf("Pr: %d\t", temporary -> data.priority);
printf("Bt: %d\t", temporary -> data.Burst_time);
printf("Wt: %d\t", temporary -> data.Wait_time);
printf("Turn: %d\n", temporary -> data.Turn_time);

temporary = temporary -> next;
}
}


// Here is my insertion function
node_t *insert_node (int priority) {
node_t *newPtr = (node_t *)malloc(sizeof(node_t));
newPtr -> data.priority = priority;
newPtr -> next = NULL;
return newPtr;
}

node_t *sort(node_t *Head)
{
    node_t *temp1,*temp2;
    for(temp1=Head;temp1->next!=NULL;temp1=temp1->next)
    {
        for(temp2=temp1->next;temp2!=NULL;temp2=temp2->next)
        {
            if(temp1->data.priority>temp2->data.priority)
            {
                element_t d = temp1->data;
                temp1->data = temp2->data;
                temp2->data = d;
            }
        }
    }
    return Head;
}

trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Linked List Representation
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education