Question

In: Computer Science

What program would you write to solve the following problems and why does it work? Please...

What program would you write to solve the following problems and why does it work? Please also comment on other students’ code at least three times. 1) Write code to partition a linked list around a value x, such that all nodes less than x come before all nodes greater than or equal to x. If x is contained within the list, the values of x only need to be after the elements less than x. The partition element x can appear anywhere in the “right partition”; it does not need to appear between the left and right partitions. Input: 3à5à8à5à10à2à1 (partition=5) Output: 3à1à2à10à5à5à8. 2) Write a method that finds the maximum of two numbers. You should not use if-else or any other comparison operator. 3) Write methods to implement the multiply, subtract and divide operations for integers. The results of all of these are integers. Use only the add operator.

Solutions

Expert Solution

// C++ program to partition a linked list around a
// given value.
#include<bits/stdc++.h>
using namespace std;

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

// A utility function to create a new node
Node *newNode(int data)
{
   struct Node* new_node = new Node;
   new_node->data = data;
   new_node->next = NULL;
   return new_node;
}

// Function to make two separate lists and return
// head after concatinating
struct Node *partition(struct Node *head, int x)
{
   /* Let us initialize first and last nodes of
   three linked lists
       1) Linked list of values smaller than x.
       2) Linked list of values equal to x.
       3) Linked list of values greater than x.*/
   struct Node *smallerHead = NULL, *smallerLast = NULL;
   struct Node *greaterLast = NULL, *greaterHead = NULL;
   struct Node *equalHead = NULL, *equalLast = NULL;

   // Now iterate original list and connect nodes
   // of appropriate linked lists.
   while (head != NULL)
   {
       // If current node is equal to x, append it
       // to the list of x values
       if (head->data == x)
       {
           if (equalHead == NULL)
               equalHead = equalLast = head;
           else
           {
               equalLast->next = head;
               equalLast = equalLast->next;
           }
       }

       // If current node is less than X, append
       // it to the list of smaller values
       else if (head->data < x)
       {
           if (smallerHead == NULL)
               smallerLast = smallerHead = head;
           else
           {
               smallerLast->next = head;
               smallerLast = head;
           }
       }
       else // Append to the list of greater values
       {
           if (greaterHead == NULL)
               greaterLast = greaterHead = head;
           else
           {
               greaterLast->next = head;
               greaterLast = head;
           }
       }

       head = head->next;
   }

   // Fix end of greater linked list to NULL if this
   // list has some nodes
   if (greaterLast != NULL)
       greaterLast->next = NULL;

   // Connect three lists

   // If smaller list is empty
   if (smallerHead == NULL)
   {
       if (equalHead == NULL)
           return greaterHead;
       equalLast->next = greaterHead;
       return equalHead;
   }

   // If smaller list is not empty
   // and equal list is empty
   if (equalHead == NULL)
   {
       smallerLast->next = greaterHead;
       return smallerHead;
   }

   // If both smaller and equal list
   // are non-empty
   smallerLast->next = equalHead;
   equalLast->next = greaterHead;
   return smallerHead;
}

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

// Driver program to run the case
int main()
{
   /* Start with the empty list */
   struct Node* head = newNode(10);
   head->next = newNode(4);
   head->next->next = newNode(5);
   head->next->next->next = newNode(30);
   head->next->next->next->next = newNode(2);
   head->next->next->next->next->next = newNode(50);

   int x = 3;
   head = partition(head, x);
   printList(head);
   return 0;
}


Related Solutions

What java program would you write to solve the following problems and why does it work?...
What java program would you write to solve the following problems and why does it work? Please also comment on other students’ code at least three times. 1) Implement MyArrayStack (constructor, push, pop, peek and isEmpty), and MyLinkedQueue with both next and previous pointers (constructor, enqueuer/offer, dequeuer/poll, peek and isEmpty), and write the following two program to test them. You must use MyArrayList or MyLinkedList for the implementation. 2) For stack testing, write a program to check if a string...
In java What program would you write to solve the following problems and why does it...
In java What program would you write to solve the following problems and why does it work? Please also comment on other students’ code at least three times. 1) Implement MyArrayStack (constructor, push, pop, peek and isEmpty), and MyLinkedQueue with both next and previous pointers (constructor, enqueuer/offer, dequeuer/poll, peek and isEmpty), and write the following two program to test them. You must use MyArrayList or MyLinkedList for the implementation. 2) For stack testing, write a program to check if a...
Please provide solutions to the following problems. Please use Excel to solve the problems and submit...
Please provide solutions to the following problems. Please use Excel to solve the problems and submit the Excel spreadsheet. A fair coin is tossed 15 times, calculate the probability of getting 0 heads or 15 heads A biased coin with probability of head being .6 is tossed 12 times. What is the probability that number of head would more than 4 but less than or equal to 10. You have a biased dice (with six faces numbered 1,2,3,4,5 and 6)...
Please provide solutions to the following problems. Please use Excel to solve the problems and submit...
Please provide solutions to the following problems. Please use Excel to solve the problems and submit the Excel spreadsheet. You started a new restaurant. Based on invoices for the first 30 days, you estimated your average grocery bill to be $20,000 with a standard deviation of $2000. You want to start another restaurant in a similar neighborhood and you are planning to prepare a brochure for investors and to work out a deal with a whole sale food distributor. Prepare...
Please complete the following problems. Show as much work as you can, and complete the problems...
Please complete the following problems. Show as much work as you can, and complete the problems as neatly as possible. The x in [x] after each problem denotes the point value. For each problem, perform the following steps. Assume that all variables are normally or approximately normally distributed. State the hypothesis and identify the claim. Find the critical value(s). Compute the test value. Make the decision. Summarize the results. The heights (in feet) for a random sample of world famous...
Please solve the following problems in their entirety. That means you will calculate a) all appropriate...
Please solve the following problems in their entirety. That means you will calculate a) all appropriate sums of squares, b) all appropriate mean squares, c) F statistics, d) critical value, and e) η2. Additionally, you are going to f) explain what happens to the null(s), g) relate the conclusion back to the problem, and h) perform any required post hoc tests with explanations 1. There is a study where the dependent variable is movie ratings. One independent variable is sex...
why do more companies not use analytics to solve such problems? how would you argue to...
why do more companies not use analytics to solve such problems? how would you argue to make the case for analytics in an old-line HR department
Why was the gold standard developed? What problems did it appear to solve and what problems...
Why was the gold standard developed? What problems did it appear to solve and what problems did it create? What is the difference between the gold standard and the gold exchange standard?
Please answer them correctly. Here are short 3 problems. Please solve all 3 problems. I would...
Please answer them correctly. Here are short 3 problems. Please solve all 3 problems. I would really appreciate your effort. Thanks. 1. Linda Williams is looking to invest in a three-year bond that makes semi-annual coupon payments at a rate of 5.475 percent. If these bonds have a market price of $982.63, what yield to maturity can she expect to earn? (Round intermediate calculations to 5 decimal places, e.g. 1.25145 and final answer to 2 decimal places, e.g. 15.25%.) Excel...
Please answer them correctly. Here are short 3 problems. Please solve all 3 problems. I would...
Please answer them correctly. Here are short 3 problems. Please solve all 3 problems. I would really appreciate your effort. Thanks. 1. You invest $260 in a mutual fund today that pays 6.30 percent interest annually. How long will it take to double your money? (If you solve this problem with algebra round intermediate calculations to 6 decimal places, in all cases round your final answer to 0 decimal place, e.g. 545) Number of Years ______? 2. You decide to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT