write a program to read follwing integer values into a sing…

write a program to read  follwing integer values into a singular linked list. Print the list.98 8279 9188 9067 789981608986 9467 80 70 89 972. Read the above data FROM the LINKEDLIST. 3. Sort the data. 4. Insert 93 after 99. 5. Print the new linked list

Answer

Program:
To address the given requirements, I will provide a program in the C programming language. The program will follow the steps outlined in the question – reading a series of integer values into a singular linked list, printing the list, sorting the data, inserting a new element, and printing the updated list.

“`c
#include
#include

// Structure for linked list node
typedef struct Node {
int data;
struct Node* next;
} Node;

// Function to print the linked list
void printLinkedList(Node* head) {
Node* current = head;

while (current != NULL) {
printf(“%d “, current->data);
current = current->next;
}
printf(“n”);
}

// Function to insert a new node after a given node
void insertAfter(Node* prevNode, int data) {
if (prevNode == NULL) {
printf(“Previous node cannot be NULL!”);
return;
}

Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->next = prevNode->next;
prevNode->next = newNode;
}

// Function to sort the linked list using bubble sort
Node* sortLinkedList(Node* head) {
int swapped;
Node* ptr1;
Node* lptr = NULL;

// Checking for empty list
if (head == NULL)
return NULL;

do {
swapped = 0;
ptr1 = head;

while (ptr1->next != lptr) {
if (ptr1->data > ptr1->next->data) {
int temp = ptr1->data;
ptr1->data = ptr1->next->data;
ptr1->next->data = temp;
swapped = 1;
}
ptr1 = ptr1->next;
}
lptr = ptr1;
} while (swapped);
return head;
}

int main() {
// Creating the linked list
Node* head = NULL;
Node* temp = NULL;

// Reading the integer values into the linked list
int input[] = {98, 8279, 9188, 9067, 7899, 8160, 8986, 9467, 80, 70, 89, 972, 0};
int i = 0;

// Building the linked list
while (input[i] != 0) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = input[i];
newNode->next = NULL;

if (head == NULL) {
head = newNode;
temp = newNode;
} else {
temp->next = newNode;
temp = temp->next;
}
i++;
}

printf(“Original linked list: “);
printLinkedList(head);

// Sorting the linked list
head = sortLinkedList(head);
printf(“Sorted linked list: “);
printLinkedList(head);

// Inserting 93 after 99
Node* current = head;
while (current != NULL) {
if (current->data == 99) {
insertAfter(current, 93);
break;
}
current = current->next;
}

printf(“Updated linked list: “);
printLinkedList(head);

return 0;
}
“`

The program first defines a structure `Node` to represent a node in the linked list. It also provides functions for printing the linked list, inserting a new node after a given node, and sorting the linked list using the bubble sort algorithm.

In the main function, the program creates a linked list by reading integer values from the `input` array. It then prints the original list, sorts it, and prints the sorted list. Finally, it inserts a new element ’93’ after ’99’ and prints the updated list.

This program uses the bubble sort algorithm to sort the linked list in ascending order. It starts with the head of the linked list and compares adjacent elements, swapping them if necessary. The sorting process continues until no more swaps are needed.

In addition, the program inserts a new node with data ’93’ after the node with data ’99’. It iterates through the linked list, finds the node with data ’99’, and inserts the new node after it.

Please compile and run the program to see the desired output.

Do you need us to help you on this or any other assignment?


Make an Order Now