C++ 程序在排序和旋转的链表中计算旋转
给定一个由 n 个节点组成的链表,该链表首先排序,然后旋转 k 个元素。求 k 的值。
这个想法是遍历单链表来检查当前节点值是否大于下一个节点的值。如果给定条件为真,则中断循环。否则增加计数器变量并通过node->next增加节点。下面是这种方法的实现。
C++
// Program for count number of rotations in
// sorted linked list.
#include
using namespace std;
/* Linked list node */
struct Node {
int data;
struct Node* next;
};
// Function that count number of
// rotation in singly linked list.
int countRotation(struct Node* head)
{
// declare count variable and assign it 1.
int count = 0;
// declare a min variable and assign to
// data of head node.
int min = head->data;
// check that while head not equal to NULL.
while (head != NULL) {
// if min value is greater then head->data
// then it breaks the while loop and
// return the value of count.
if (min > head->data)
break;
count++;
// head assign the next value of head.
head = head->next;
}
return count;
}
// Function to push element in linked list.
void push(struct Node** head, int data)
{
// Allocate dynamic memory for newNode.
struct Node* newNode = new Node;
// Assign the data into newNode.
newNode->data = data;
// newNode->next assign the address of
// head node.
newNode->next = (*head);
// newNode become the headNode.
(*head) = newNode;
}
// Display linked list.
void printList(struct Node* node)
{
while (node != NULL) {
printf("%d ", node->data);
node = node->next;
}
}
// Driver functions
int main()
{
// Create a node and initialize with NULL
struct Node* head = NULL;
// push() insert node in linked list.
// 15 -> 18 -> 5 -> 8 -> 11 -> 12
push(&head, 12);
push(&head, 11);
push(&head, 8);
push(&head, 5);
push(&head, 18);
push(&head, 15);
printList(head);
cout << endl;
cout << "Linked list rotated elements: ";
// Function call countRotation()
cout << countRotation(head) << endl;
return 0;
}
输出
15 18 5 8 11 12
Linked list rotated elements: 2
有关详细信息,请参阅有关排序和旋转链表中的计数旋转的完整文章!