Question

Write a C++ code based this question n this assignment you will create three additional functions...

Write a C++ code based this question

n this assignment you will create three additional functions for each one of the data structures created in class. You will be provided with a header file which you will modify and a demo program for each data structure. If your functions are implemented correctly the demo programs should compile and execute correctly.

Part 0 (Comments)

1. Over the course of the semester we have discussed comments for each data structure. Please add any comments you view as necessary to the header file before writing your own functions

Part 1 (Stack)

1. Write a function which sorts the stack in order from the largest value to the smallest value. This should be a modified version of bubble sort.

2. Write a function which prints the values in the stack in reverse order.

3. Write a function which the counts the number of odd numbers and even numbers currently in the stack and prints the results.

Optional

4. Write a function which prints how many numbers in the stack are divisible by 2,3, and 5.

Part 2 (Queue)

1. Write a function which sorts the queue in order from the smallest value to the largest value. This should be a modified version of bubble sort.

2. Write a function which adds a value to the queue in the correct position (numbers are arranged from the smallest value to the largest value).

3. Write a function which prints the median and mean of the queue. Please take some time to consider all the required conditions to calculate the median of a dataset.

Optional

4. Write a function which prints the mode of the queue. (The number which appears the most frequently in the queue)

Part 3 (Link List)

1. Write a function which adds 4 to all the even values in the link list and multiples all the odd values by 5.

2. Write a function which multiplies the values in odd position values by 10. Odd positions in this case refers to the first value in the list, the third value, the fifth value etc.

3. Write a function which determines if the values in the queue is divisible by 5 or 10 if true the value is divided by 5.

Optional

4. Write a function which prints all the prime values in the queue

Part 4

1. Trace each Demo program by hand using the table from your midterm exam for stacks and queues.

Notes- While writing these functions you should consider all the conditions which need to be checked by each function. The demo program will check these cases. When the word print is included in the question no changes are made to the values in the queue, stack, or link list; the required information is just displayed on the screen.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer :

1.  

stack<int> sortStack(stack<int> &input)
{
stack<int> tmpStack;

while (!input.empty())
{
// pop out the first element
int tmp = input.top();
input.pop();

// while temporary stack is not empty and top
// of stack is greater than temp
while (!tmpStack.empty() && tmpStack.top() > tmp)
{
// pop from temporary stack and push
// it to the input stack
input.push(tmpStack.top());
tmpStack.pop();
}

// push temp in tempory of stack
tmpStack.push(tmp);
}

return tmpStack;
}

2.

void printStack(stack<int> s)
{
if(s.empty())
return;
int x = s.top();
  
//Pop elements of the stack;
s.pop();
  
//Recursive call the fucntion printStack
printStack();
  
//Print the stack elements from bottom
cout<< x <<" ";
  
//Push the elements into the stack the preserve the order

s.push(x);
}

3.

void countEvenOdd(stack<int> s)
{
int odd=0,even=0; //To store the count values
while(!s.empty)
{
int x = s.top(); //Store the value of top element of the stack
if(x%2==0)
even++;
else
odd++;
s.pop();
}
cout<<"Number of even elements: "<<even<<endl;
cout<<"Number of odd elements: "<<odd<<endl;
}

Thank you...

Add a comment
Know the answer?
Add Answer to:
Write a C++ code based this question n this assignment you will create three additional functions...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • c++ no pointers just follow instructions Write a program (array.cpp) that read positive floating point numbers...

    c++ no pointers just follow instructions Write a program (array.cpp) that read positive floating point numbers into an array of capacity 100. The program will then ask for one of three letters(A, M or S). if the user inputs something other than one of these letters, then the program should ask for that input until an A, M, or S is entered. A do-while loop should be used for this. If the user inputs an A, then the program should...

  • I need a c++ code please. 32. Program. Write a program that creates an integer constant...

    I need a c++ code please. 32. Program. Write a program that creates an integer constant called SIZE and initialize to the size of the array you will be creating. Use this constant throughout your functions you will implement for the next parts and your main program. Also, in your main program create an integer array called numbers and initialize it with the following values (Please see demo program below): 68, 100, 43, 58, 76, 72, 46, 55, 92, 94,...

  • Q.1. Write a C program that determines whether a line entered by a user is a...

    Q.1. Write a C program that determines whether a line entered by a user is a palindrome or not. You must demonstrate it as an application of stack (you may use linked list implementation demonstrated in the class). Hint! Think of the basic property of a stack i.e. LIFO (list-in-first-out). Q.2. Write a charQueue header file containing all the function headers of following functions: 1- initiateQueue—to initialize a queue 2- enqueue—to add a node at the rear end of the...

  • **IN C*** * In this lab, you will write a program with three recursive functions you...

    **IN C*** * In this lab, you will write a program with three recursive functions you will call in your main. For the purposes of this lab, keep all functions in a single source file: main.c Here are the three functions you will write. For each function, the output example is for this array: int array[ ] = { 35, 25, 20, 15, 10 }; • Function 1: This function is named printReverse(). It takes in an array of integers...

  • 1) Translate the following equation into a Python assignment statement 2) Write Python code that prints...

    1) Translate the following equation into a Python assignment statement 2) Write Python code that prints PLUS, MINUS, O ZERO, depending on the value stored in a variable named N. 3) What is printed by: 3 - 1 while 5: while 10 Print ) Page 1 of 9 4) Write a Python while loop that reads in integers until the user enters a negative number, then prints the sum of the numbers. 1-2 of 9 4) Write a Python while...

  • C++

    INFO1112 A11Lab 6March 18, 2021 Note:  It is important that you do this lab exercise properly.  Please read and follow the instructions very carefully.  You need to understand what you are required to do, and complete the program.  It uses a one-dimensional array.  You can refer to the PowerPoint slides, many of the tasks can be found there, but you need to apply them for this program. Note that the array will be filled with the integers when the user enters them. Consider...

  • PROJECT 6    Functions You are to write a PYTHON program which: *Defines the following 4  functions: whoamI()...

    PROJECT 6    Functions You are to write a PYTHON program which: *Defines the following 4  functions: whoamI() This function prints out your name and lists any programming course you have taken prior to this course. isEven(number) This function accepts one parameter, a number, and prints out a message indicating if the number is even or odd. Keep in mind that a number is even if there is no remainder when the number is divided by two. printEven(number) This function accepts one...

  • in c++ language, using quincy compiler Write a program that will read in 12 characters from...

    in c++ language, using quincy compiler Write a program that will read in 12 characters from the keyboard between a – z in lowercase. These letters will be inserted into a queue (imported from the Queue.h header file). When the 12th character has been entered, the queue will serve the characters one at a time to a character variable, which will push the values, one at a time, into a stack (using code imported from the Stack.h header file) and...

  • FUNCTIONS In this assignment, you will revisit reading data from a file, and use that data...

    FUNCTIONS In this assignment, you will revisit reading data from a file, and use that data as arguments (parameters) for a number of functions you will write. You will need to: Write and test a function square_each(nums) Where nums is a (Python) list of numbers. It modifies the list nums by squaring each entry and replacing its original value. You must modify the parameter, a return will not be allowed! Write and test a function sum_list(nums) Where nums is a...

  • MODIFY THIS LINK LIST USING C++: Write a function which determines if the values in the...

    MODIFY THIS LINK LIST USING C++: Write a function which determines if the values in the queue is divisible by 5 or 10 if true the value is divided by 5 void NumberList::listDivisibleBy5or10() ListNode* currentNode = head;   //Pointer for current value in the link list       if(!head) // checks for an empty link list    {        std::cout<<"The list is empty \n"; // tells user that the link list is empty    }    else    {   ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT