Answer to Question #249679 in C for siri

Question #249679

1. Let a linked list consists of n number of nodes, where each node consists of an unique character that represents the grades of the students (O, E, A, B, C ), and pointer to the next node. Write the C code to group the students having the same grade in consecutive places and also finally all the nodes should be in sorting order as per their grade value. (O->O->E->E->E->A->B->B->C->C->C)


1
Expert's answer
2021-10-12T00:37:23-0400
#include <stdio.h>
#include <stdlib.h>
struct Node {
    int data;
    struct Node* next;
};
void sort(struct Node** head_ref,
                  struct Node* newN)
{
    struct Node* current;
    if (*head_ref == NULL
        || (*head_ref)->data
               >= newN->data) {
        newN->next = *head_ref;
        *head_ref = newN;
    }
    else {
        current = *head_ref;
        while (current->next != NULL
               && current->next->data < newN->data) {
            current = current->next;
        }
        newN->next = current->next;
        current->next = newN;
    }
}
struct Node* newNode(int new_data)
{
    struct Node* newN
= (struct Node*)malloc(
sizeof(struct Node));
 
    newN->data = new_data;
    newN->next = NULL;
 
    return newN;
}
 
void printList(struct Node* head)
{
    struct Node* temp = head;
    while (temp != NULL) {
        printf("%d  ", temp->data);
        temp = temp->next;
    }
}
 


int main()
{
    struct Node* head = NULL;
    struct Node* newN = newNode(5);
    sort(&head, newN);
    newN = newNode(12);
    sort(&head, newN);
    newN = newNode(8);
    sort(&head, newN);
    newN = newNode(6);
    sort(&head, newN);
    newN = newNode(3);
    sort(&head, newN);
    newN = newNode(10);
    sort(&head, newN);
    printf("\nCreated Linked List\n");
    printf("\nO->O->E->E->E->A->B->B->C->C->C\n");
    printList(head);
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS