Question in C++ Write a complete C++ program to demonstrate “rotating” of a sing
ID: 3588440 • Letter: Q
Question
Question in C++
Write a complete C++ program to demonstrate “rotating” of a singly linked lists of integers – all elements of the linked list get shifted to the left by one position and the first node becomes the last. The main component of your program must be a function called rotateLeft that takes a pointer (head of the linked list) as input parameter. This function should modify the linked list and return a pointer to the modified linked. For example, headRotated = rotateLeft (head)Input: Output (return value): You should then write a function called print that takes a pointer to a linked list as a parameter and prints out the integers in the linked with a space between the integers. Call the print function in your main program to show the contents of the merged list. Your main program will have the following structure:
// implementation of linked list
// implementation of rotate function
// implementation of print function
int main()
{ // create linked list by reading (say, 6) integers from the user
// declare headRotated
headRotated = rotateLeft (head);
print (headRotated); }
The output of your program for the example input shown above should be the following:
15 30 25 20 35 10
Implementation of linked list: Keep it simple! Only implement what you need to solve this problem (i.e., just define a node and a function to add to a linked list). You do not need to use classes. You may define a Node as a struct as below.
struct Node {
int data;
Node * next;
};
If you want to use a Node class, keep it simple with the data members public and just the constructor as a member function. Do not make print into a member function of the Node class. Also, do not bother making a List class. Do NOT use any STL classes (such as std::list, std::forward_list).
Explanation / Answer
int [] unOrderedArr = {1,2,3,4,5,6,7,8}; int orderToRotate =2; for(int i = 0; i0; j--){ int temp = unOrderedArr[j]; unOrderedArr[j] = unOrderedArr[j-1]; unOrderedArr[j-1] = temp; } } for(int j = 0; jRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.