旋转链表【C语言】

#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
	int data;
	struct Node* next;
} Node, *LinkedList;

// 创建一个新节点
Node* createNode(int data) {
	Node* newNode = (Node*)malloc(sizeof(Node));
	if (newNode == NULL) {
		printf("Error! Unable to create a new node.\n");
		exit(0);
	}
	newNode->data = data;
	newNode->next = NULL;
	return newNode;
}

// 在链表末尾添加新节点
void append(LinkedList* head, int data) {
	if (*head == NULL) {
		*head = createNode(data);
	}
	else {
		Node* lastNode = *head;
		while (lastNode->next != NULL) {
			lastNode = lastNode->next;
		}
		lastNode->next = createNode(data);
	}
}

// 打印链表
void printList(LinkedList head) {
	while (head != NULL) {
		printf("%d ", head->data);
		head = head->next;
	}
	printf("\n");
}

LinkedList rotate(Node* head, int k)
{
	if (head == NULL || head->next == NULL || k == 0)
	{
		return head;
	}
	int listLength = 1;
	Node* tail = head;
	while (tail->next != NULL)
	{
		listLength = listLength + 1;   //获取链表的总长度
		tail = tail->next;
	}
	if (listLength <= k)
	{
		return head;
	}

	int i = listLength - k - 1;
	Node* newTail = head;
	Node* newHead = NULL;
	while(i != 0)
	{
	    //这个是获取新的尾部的节点,1,2,3,4,5,6,如果传进来k是2,则newTial指向4所在的节点
		newTail = newTail->next;
		i--;
	}
	newHead = newTail->next;   // 5(newHead ),6

	//进行旋转
	newTail->next = NULL;  // 将4所在的节点的next = NULL;使其成为新的尾部
	tail->next = head;     //  5(newHead ),6 -> 1(head),2,3,4 = NULL   

	return newHead;
}

int main() {
	LinkedList head1 = NULL;
	append(&head1, 1);
	append(&head1, 2);
	append(&head1, 3);
	append(&head1, 4);
	append(&head1, 5);
	append(&head1, 6);

	printf("Original List1: ");
	printList(head1);

	LinkedList head = rotate(head1, 2);

	printf("deleteDuplicate List: ");
	printList(head);
	system("pause");
	return 0;
}
LinkedList rotate(Node* head, int k)
{
	if (head == NULL || head->next == NULL || k == 0) {
		return head;
	}

	int length = 1;
	Node* tail = head;

	// 计算链表长度并找到尾节点
	while (tail->next) {
		tail = tail->next;
		length++;
	}

	k = k % length;
	if (k == 0) {
		return head;
	}

	// 找到新的尾节点和新的头节点
	Node* new_tail = head;
	for (int i = 0; i < length - k - 1; i++) {
		new_tail = new_tail->next;
	}
	Node* new_head = new_tail->next;

	// 进行旋转
	new_tail->next = NULL;
	tail->next = head;

	return new_head;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
链表可以用来存储七巧板游戏的状态。七巧板游戏是一个拼图游戏,需要将七个不同形状的木块拼成一个正方形。每个木块可以旋转和翻转,但不能重叠或悬空。 下面是一个简单的链表实现,每个节点表示一个木块的状态。每个节点包含一个指向下一个节点的指针和一个二维数组,表示该节点表示的木块的状态。 ``` #include <stdio.h> #include <stdlib.h> #define SIZE 7 #define ROWS 3 #define COLS 3 typedef struct node { int data[ROWS][COLS]; struct node* next; } Node; Node* create_node(int data[ROWS][COLS]) { Node* node = (Node*)malloc(sizeof(Node)); for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { node->data[i][j] = data[i][j]; } } node->next = NULL; return node; } void add_node(Node** head, Node* node) { if (*head == NULL) { *head = node; } else { Node* current = *head; while (current->next != NULL) { current = current->next; } current->next = node; } } void print_node(Node* node) { for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { printf("%d ", node->data[i][j]); } printf("\n"); } } void print_list(Node* head) { Node* current = head; while (current != NULL) { print_node(current); current = current->next; } } int main() { // 创建链表 Node* head = NULL; // 添加节点 int data1[ROWS][COLS] = {{1, 1, 1}, {1, 0, 0}, {1, 0, 0}}; int data2[ROWS][COLS] = {{0, 1, 0}, {0, 1, 0}, {1, 1, 1}}; int data3[ROWS][COLS] = {{1, 0, 0}, {1, 0, 0}, {1, 1, 1}}; int data4[ROWS][COLS] = {{1, 0, 0}, {1, 1, 1}, {1, 0, 0}}; int data5[ROWS][COLS] = {{1, 1, 0}, {0, 1, 0}, {0, 1, 1}}; int data6[ROWS][COLS] = {{0, 0, 1}, {1, 1, 1}, {1, 0, 0}}; int data7[ROWS][COLS] = {{0, 1, 1}, {1, 1, 0}, {0, 1, 0}}; add_node(&head, create_node(data1)); add_node(&head, create_node(data2)); add_node(&head, create_node(data3)); add_node(&head, create_node(data4)); add_node(&head, create_node(data5)); add_node(&head, create_node(data6)); add_node(&head, create_node(data7)); // 打印链表 print_list(head); return 0; } ``` 这个程序创建了一个包含 7 个节点的链表,每个节点表示一个木块的状态。可以通过添加节点和打印链表的函数来实现游戏的操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值