链表中的旋转操作2024年8月28日 | 阅读 21 分钟 对链表可以进行很多操作,例如插入操作,即我们可以将新节点添加到已存在的链表中;删除操作,即我们可以从链表中删除节点;以及显示链表中所有节点中的数据。 在单向链表中,单向链表的节点指向存储下一个节点的内存地址。同样,以这种方式,所有节点都存储其各自下一个节点的地址。除了最后一个节点,最后一个节点的指针中的地址为 NULL,表示它是链表的最后一个节点。 链表还支持一个操作,那就是 **旋转操作**。在此操作中,链表的头节点被移至链表的最后一个元素,而链表的第一个元素将成为链表中存在的最后一个元素。 C 代码现在,让我们看一下用不同编程语言旋转或反转链表的代码。 输出 上面的代码产生以下输出: Please Choose one of the Operations:: 1. To Insert Data in the Linked List. 2. To Rotate the Linked List. 3. To Display Data present in the Linked List. 1 Enter the data that you want to add to the newly added node to the Linked List: 15 Data Added Successfully. Type [N or n] to terminate the program. Type [Y or y] to continue the program. y Please Choose one of the Operations:: 1. To Insert Data in the Linked List. 2. To Rotate the Linked List. 3. To Display Data present in the Linked List. 1 Enter the data that you want to add to the newly added node to the Linked List: 13 Data Added Successfully. Type [N or n] to terminate the program. Type [Y or y] to continue the program. y Please Choose one of the Operations:: 1. To Insert Data in the Linked List. 2. To Rotate the Linked List. 3. To Display Data present in the Linked List. 1 Enter the data that you want to add to the newly added node to the Linked List: 11 Data Added Successfully. Type [N or n] to terminate the program. Type [Y or y] to continue the program. y Please Choose one of the Operations:: 1. To Insert Data in the Linked List. 2. To Rotate the Linked List. 3. To Display Data present in the Linked List. 10 Please enter a valid option from the menu to proceed further. Type [N or n] to terminate the program. Type [Y or y] to continue the program. y Please Choose one of the Operations:: 1. To Insert Data in the Linked List. 2. To Rotate the Linked List. 3. To Display Data present in the Linked List. 1 Enter the data that you want to add to the newly added node to the Linked List: 5 Data Added Successfully. Type [N or n] to terminate the program. Type [Y or y] to continue the program. y Please Choose one of the Operations:: 1. To Insert Data in the Linked List. 2. To Rotate the Linked List. 3. To Display Data present in the Linked List. 1 Enter the data that you want to add to the newly added node to the Linked List: 3 Data Added Successfully. Type [N or n] to terminate the program. Type [Y or y] to continue the program. y Please Choose one of the Operations:: 1. To Insert Data in the Linked List. 2. To Rotate the Linked List. 3. To Display Data present in the Linked List. 1 Enter the data that you want to add to the newly added node to the Linked List: 2 Data Added Successfully. Type [N or n] to terminate the program. Type [Y or y] to continue the program. y Please Choose one of the Operations:: 1. To Insert Data in the Linked List. 2. To Rotate the Linked List. 3. To Display Data present in the Linked List. 1 Enter the data that you want to add to the newly added node to the Linked List: 1 Data Added Successfully. Type [N or n] to terminate the program. Type [Y or y] to continue the program. y Please Choose one of the Operations:: 1. To Insert Data in the Linked List. 2. To Rotate the Linked List. 3. To Display Data present in the Linked List. 3 Data in the Linked List is: 1 2 3 5 11 13 15 Type [N or n] to terminate the program. Type [Y or y] to continue the program. y Please Choose one of the Operations:: 1. To Insert Data in the Linked List. 2. To Rotate the Linked List. 3. To Display Data present in the Linked List. 2 Linked List Rotated Successfully. Type [N or n] to terminate the program. Type [Y or y] to continue the program. y Please Choose one of the Operations:: 1. To Insert Data in the Linked List. 2. To Rotate the Linked List. 3. To Display Data present in the Linked List. 3 Data in the Linked List is: 15 13 11 5 3 2 1 Type [N or n] to terminate the program. Type [Y or y] to continue the program. N C++ 代码现在,让我们看一下用 C++ 旋转或反转链表的代码。 输出 此 C++ 代码产生以下输出: Please Choose one of the Operations:: 1. To Insert Data in the Linked List. 2. To Rotate the Linked List. 3. To Display Data present in the Linked List. 1 Enter the data that you want to add to the newly added node to the Linked List: 190 Data Added Successfully. Type [N or n] to terminate the program. Type [Y or y] to continue the program. y Please Choose one of the Operations:: 1. To Insert Data in the Linked List. 2. To Rotate the Linked List. 3. To Display Data present in the Linked List. 1 Enter the data that you want to add to the newly added node to the Linked List: 70 Data Added Successfully. Type [N or n] to terminate the program. Type [Y or y] to continue the program. y Please Choose one of the Operations:: 1. To Insert Data in the Linked List. 2. To Rotate the Linked List. 3. To Display Data present in the Linked List. 1 Enter the data that you want to add to the newly added node to the Linked List: 55 Data Added Successfully. Type [N or n] to terminate the program. Type [Y or y] to continue the program. y Please Choose one of the Operations:: 1. To Insert Data in the Linked List. 2. To Rotate the Linked List. 3. To Display Data present in the Linked List. 1 Enter the data that you want to add to the newly added node to the Linked List: 40 Data Added Successfully. Type [N or n] to terminate the program. Type [Y or y] to continue the program. y Please Choose one of the Operations:: 1. To Insert Data in the Linked List. 2. To Rotate the Linked List. 3. To Display Data present in the Linked List. 1 Enter the data that you want to add to the newly added node to the Linked List: 30 Data Added Successfully. Type [N or n] to terminate the program. Type [Y or y] to continue the program. y Please Choose one of the Operations:: 1. To Insert Data in the Linked List. 2. To Rotate the Linked List. 3. To Display Data present in the Linked List. 1 Enter the data that you want to add to the newly added node to the Linked List: 20 Data Added Successfully. Type [N or n] to terminate the program. Type [Y or y] to continue the program. y Please Choose one of the Operations:: 1. To Insert Data in the Linked List. 2. To Rotate the Linked List. 3. To Display Data present in the Linked List. 1 Enter the data that you want to add to the newly added node to the Linked List: 10 Data Added Successfully. Type [N or n] to terminate the program. Type [Y or y] to continue the program. y Please Choose one of the Operations:: 1. To Insert Data in the Linked List. 2. To Rotate the Linked List. 3. To Display Data present in the Linked List. 3 Data in the Linked List is: 10 20 30 40 55 70 190 Type [N or n] to terminate the program. Type [Y or y] to continue the program. y Please Choose one of the Operations:: 1. To Insert Data in the Linked List. 2. To Rotate the Linked List. 3. To Display Data present in the Linked List. 2 Linked List Rotated Successfully. Type [N or n] to terminate the program. Type [Y or y] to continue the program. y Please Choose one of the Operations:: 1. To Insert Data in the Linked List. 2. To Rotate the Linked List. 3. To Display Data present in the Linked List. 3 Data in the Linked List is: 190 70 55 40 30 20 10 Type [N or n] to terminate the program. Type [Y or y] to continue the program. N Java 代码现在,让我们看一下用 Java 旋转或反转链表的代码。 输出 上面的 Java 代码产生以下输出: Please Choose one of the Operations:: 1. To Insert Data in the Linked List. 2. To Rotate the Linked List. 3. To Display Data present in the Linked List. 1 Enter the data that you want to add to the newly added node to the Linked List: 1 Data Added Successfully. Type [N or n] to terminate the program. Type [Y or y] to continue the program. y Please Choose one of the Operations:: 1. To Insert Data in the Linked List. 2. To Rotate the Linked List. 3. To Display Data present in the Linked List. 1 Enter the data that you want to add to the newly added node to the Linked List: 2 Data Added Successfully. Type [N or n] to terminate the program. Type [Y or y] to continue the program. y Please Choose one of the Operations:: 1. To Insert Data in the Linked List. 2. To Rotate the Linked List. 3. To Display Data present in the Linked List. 1 Enter the data that you want to add to the newly added node to the Linked List: 3 Data Added Successfully. Type [N or n] to terminate the program. Type [Y or y] to continue the program. y Please Choose one of the Operations:: 1. To Insert Data in the Linked List. 2. To Rotate the Linked List. 3. To Display Data present in the Linked List. 1 Enter the data that you want to add to the newly added node to the Linked List: 4 Data Added Successfully. Type [N or n] to terminate the program. Type [Y or y] to continue the program. y Please Choose one of the Operations:: 1. To Insert Data in the Linked List. 2. To Rotate the Linked List. 3. To Display Data present in the Linked List. 1 Enter the data that you want to add to the newly added node to the Linked List: 5 Data Added Successfully. Type [N or n] to terminate the program. Type [Y or y] to continue the program. y Please Choose one of the Operations:: 1. To Insert Data in the Linked List. 2. To Rotate the Linked List. 3. To Display Data present in the Linked List. 1 Enter the data that you want to add to the newly added node to the Linked List: 6 Data Added Successfully. Type [N or n] to terminate the program. Type [Y or y] to continue the program. y Please Choose one of the Operations:: 1. To Insert Data in the Linked List. 2. To Rotate the Linked List. 3. To Display Data present in the Linked List. 3 Data in the Linked List is: 1 2 3 4 5 6 Type [N or n] to terminate the program. Type [Y or y] to continue the program. y Please Choose one of the Operations:: 1. To Insert Data in the Linked List. 2. To Rotate the Linked List. 3. To Display Data present in the Linked List. 2 Linked List Rotated Successfully. Type [N or n] to terminate the program. Type [Y or y] to continue the program. y Please Choose one of the Operations:: 1. To Insert Data in the Linked List. 2. To Rotate the Linked List. 3. To Display Data present in the Linked List. 3 Data in the Linked List is: 6 5 4 3 2 1 Type [N or n] to terminate the program. Type [Y or y] to continue the program. N 因此,在本文中,我们清楚地了解了链表中的旋转操作。我们还看到了如何在 C、C++ 和 Java 等不同编程语言中编写链表旋转操作的代码。 下一主题给定和的子数组 |
我们请求您订阅我们的新闻通讯以获取最新更新。