Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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).

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions