Question

Write a C++ code to ask the user to enter 4 values (whole numbers). Your code...

Write a C++ code to ask the user to enter 4 values (whole numbers). Your code should print the numbers in descending order. (Don't solve using array)

Homework Answers

Answer #1

In the below code,

1. Take 4 whole numbers as input.

2. Insert these 4 values in the linked list(array cannot be used as directed in the question) as an individual node.

3.Sort the numbers in descending order using insertion sort.

#include<iostream>
using namespace std;

// Link list node
struct Node 
{ 
        int data; 
        struct Node* next; 
}; 

// Function to insert a given node in a reverse sorted linked list 
void descendingSort(struct Node**, struct Node*); 

// function to sort a singly linked list
void insertAtPosition(struct Node **head_ref) 
{ 
        // Initialize sorted linked list 
        struct Node *sorted = NULL; 

        // Traverse the given linked list and insert every node to be sorted 
        struct Node *current = *head_ref; 
        while (current != NULL) 
        { 
                // Store next for next iteration 
                struct Node *next = current->next; 

                // insert current in sorted linked list 
                descendingSort(&sorted, current); 

                // Update current 
                current = next; 
        } 

        // Update head_ref to point to sorted linked list 
        *head_ref = sorted; 
} 

// function to insert a new_node in a list.
void descendingSort(struct Node** head_ref, struct Node* new_node) 
{ 
        struct Node* current; 
        /* Special case for the head end */
        if (*head_ref == NULL || (*head_ref)->data <= new_node->data) 
        { 
                new_node->next = *head_ref; 
                *head_ref = new_node; 
        } 
        else
        { 
                /* Locate the node before the point of insertion */
                current = *head_ref; 
                while (current->next!=NULL && 
                        current->next->data > new_node->data) 
                { 
                        current = current->next; 
                } 
                new_node->next = current->next; 
                current->next = new_node; 
        } 
} 

// Function to print linked list
void printList(struct Node *head) 
{ 
        struct Node *temp = head; 
        while(temp != NULL) 
        { 
                cout<<temp->data<<" "; 
                temp = temp->next; 
        } 
} 

// A function to insert a node at the beginning of linked list
void push(struct Node** head_ref, int new_data) 
{ 
        /* allocate node */
        struct Node* new_node = new Node; 

        /* put in the data */
        new_node->data = new_data; 

        /* link the old list off the new node */
        new_node->next = (*head_ref); 

        /* move the head to point to the new node */
        (*head_ref) = new_node; 
} 

int main() 
{ 
        struct Node *a = NULL;
        int w,x,y,z;
        cin>>w>>x>>y>>z;
        push(&a, w);
        push(&a, x); 
        push(&a, y); 
        push(&a, z); 
        
        cout<<"Linked List before sorting \n"; 
        printList(a); 

        insertAtPosition(&a); 

        cout<<"\nLinked List after sorting \n"; 
        printList(a); 

        return 0; 
} 

OUTPUT :

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
x86 irvine library assembly code Write a complete program that: 1. Prompt the user to enter...
x86 irvine library assembly code Write a complete program that: 1. Prompt the user to enter 10 numbers. 2. save those numbers in a 32-bit integer array. 3. Print the array with the same order it was entered. 3. Calculate the sum of the numbers and display it. 4. Calculate the mean of the array and display it. 5. Rotate the members in the array forward one position for 9 times. so the last rotation will display the array in...
Classwork_1.5: Write a C code that ask the user to enter the name of person, gender...
Classwork_1.5: Write a C code that ask the user to enter the name of person, gender (male or female) and age and display these quantities. You need to create a structure to solve this problem. Output:
Use C++ Write a program that first reads in how many whole numbers the user wants...
Use C++ Write a program that first reads in how many whole numbers the user wants to sum, then reads in that many whole numbers, and finally outputs the sum of all the numbers greater than zero, the sum of all the numbers less than zero (which will be a negative number or zero), and the sum of all the numbers, whether positive, negative, or zero. The user enters the numbers just once each and the user can enter them...
Write Java applet which will ask the user to enter Temperature in Fahrenheit. Your code should...
Write Java applet which will ask the user to enter Temperature in Fahrenheit. Your code should convert it into Celcius and display the value to the user.
Write a C++ program that asks the user to enter in three numbers and displays the...
Write a C++ program that asks the user to enter in three numbers and displays the numbers in ascending order. If the three numbers are all the same the program should tell the user that all the numbers are equal and exits the program. Be sure to think about all the possible cases of three numbers. Be sure to test all possible paths. Sample Runs: NOTE: not all possible runs are shown below. Sample Run 1 Welcome to the order...
Write a complete C++ program asking the user to enter numbers one by one. User enters...
Write a complete C++ program asking the user to enter numbers one by one. User enters only whole numbers 0 and 10. Once the user enters 0, then the program should print sum of only odd numbers entered by user prior to 0. A valid scenario given below: Enter a number: 5 Enter a number: 3 Enter a number: 2 Enter a number: 3 Enter a number: 8 Enter a number: 0 Sum of the odd numbers you entered is...
Write a python code that will ask the user to enter an integer number n. Construct...
Write a python code that will ask the user to enter an integer number n. Construct a recursive function that prints numbers 1 to n in the form “11223344..nn”.
 Write a program to find the prime numbers - Ask user to input the integer...
 Write a program to find the prime numbers - Ask user to input the integer number - test the number whether it is a prime number or not - Then, print “true” or “false” depending on whether the number is prime or isn’t. - Hint: number is prime when is has exactly 2 factors: one and itself. By this definition, number 1 is a special case and is NOT a prime. - Use idea of user input, cumulative sum,...
Using C++ design a program that Prompts the User to enter 4 Numbers, then have the...
Using C++ design a program that Prompts the User to enter 4 Numbers, then have the program output those numbers entered in Ascending order. Use "if" statements to create the code...
Write a program that does the following in order: 1. Ask user to enter a name...
Write a program that does the following in order: 1. Ask user to enter a name 2. Ask the user to enter five numbers “amount1”, “amount2”, “amount3”, “amount4”, “amount5” 3. Calculate the sum of the numbers “amount1”, “amount2”, “amount3”, “amount4”, “amount5” 4. If the sum is greater than 0, print out the sum 5. If the sum is equal to zero, print out “Your account balance is zero” 6. If the sum is less than 0, print out “Your account...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT