<2>设计完整程序以判断一个带头结点的单循环链表是否满足这样的条件: 其中每个结点的元素值与其序号的差的绝对值不大于3。 若成立, 返回TRUE, 否则返回FALSE。 实验测试数据基本要求: 第一组数据:链表元素为 (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 13, 15, 16, 18) 第二组数据:链表元素为 (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15, 20, 18)
时间: 2023-09-04 10:10:06 浏览: 93
好的,我可以为您设计一个程序来判断一个带头结点的单循环链表是否满足每个结点的元素值与其序号的差的绝对值不大于3。以下是程序的示例代码:
```C++
#include <iostream>
using namespace std;
struct Node {
int data;
Node *next;
};
bool checkCircularLinkedList(Node *head) {
if (head == NULL || head->next == NULL)
return false;
Node *current = head->next;
int index = 1;
do {
if (abs(current->data - index) > 3)
return false;
current = current->next;
index++;
} while (current != head->next);
return true;
}
int main() {
// 创建第一组数据:(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 13, 15, 16, 18)
Node *head1 = new Node();
Node *node1 = new Node();
Node *node2 = new Node();
Node *node3 = new Node();
Node *node4 = new Node();
Node *node5 = new Node();
Node *node6 = new Node();
Node *node7 = new Node();
Node *node8 = new Node();
Node *node9 = new Node();
Node *node10 = new Node();
Node *node11 = new Node();
Node *node12 = new Node();
Node *node13 = new Node();
Node *node14 = new Node();
head1->next = node1;
node1->data = 1;
node1->next = node2;
node2->data = 2;
node2->next = node3;
node3->data = 3;
node3->next = node4;
node4->data = 4;
node4->next = node5;
node5->data = 5;
node5->next = node6;
node6->data = 6;
node6->next = node7;
node7->data = 7;
node7->next = node8;
node8->data = 8;
node8->next = node9;
node9->data = 9;
node9->next = node10;
node10->data = 10;
node10->next = node11;
node11->data = 12;
node11->next = node12;
node12->data = 13;
node12->next = node13;
node13->data = 15;
node13->next = node14;
node14->data = 16;
node14->next = head1;
// 检查链表是否满足条件
cout << "检查第一组数据:" << checkCircularLinkedList(head1) << endl;
// 释放内存
delete head1;
delete node1;
delete node2;
delete node3;
delete node4;
delete node5;
delete node6;
delete node7;
delete node8;
delete node9;
delete node10;
delete node11;
delete node12;
delete node13;
delete node14;
// 创建第二组数据:(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15, 20, 18)
Node *head2 = new Node();
Node *n1 = new Node();
Node *n2 = new Node();
Node *n3 = new Node();
Node *n4 = new Node();
Node *n5 = new Node();
Node *n6 = new Node();
Node *n7 = new Node();
Node *n8 = new Node();
Node *n9 = new Node();
Node *n10 = new Node();
Node *n11 = new Node();
Node *n12 = new Node();
Node *n13 = new Node();
Node *n14 = new Node();
Node *n15 = new Node();
Node *n16 = new Node();
head2->next = n1;
n1->data = 1;
n1->next = n2;
n2->data = 2;
n2->next = n3;
n3->data = 3;
n3->next = n4;
n4->data = 4;
n4->next = n5;
n5->data = 5;
n5->next = n6;
n6->data = 6;
n6->next = n7;
n7->data = 7;
n7->next = n8;
n8->data = 8;
n8->next = n9;
n9->data = 9;
n9->next = n10;
n10->data = 10;
n10->next = n11;
n11->data = 11;
n11->next = n12;
n12->data = 12;
n12->next = n13;
n13->data = 13;
n13->next = n14;
n14->data = 15;
n14->next = n15;
n15->data = 20;
n15->next = n16;
n16->data = 18;
n16->next = head2;
// 检查链表是否满足条件
cout << "检查第二组数据:" << checkCircularLinkedList(head2) << endl;
// 释放内存
delete head2;
delete n1;
delete n2;
delete n3;
delete n4;
delete n5;
delete n6;
delete n7;
delete n8;
delete n9;
delete n10;
delete n11;
delete n12;
delete n13;
delete n14;
delete n15;
delete n16;
return 0;
}
```
在这个程序中,我们首先定义了一个名为Node的结构体,它包含一个整数数据和一个指向下一个结点的指针。然后我们定义了一个名为checkCircularLinkedList的函数,它的参数是指向链表头结点的指针。函数中我们定义了一个指向当前结点的指针current和一个表示结点序号的变量index,将它们分别初始化为头结点的下一个结点和1。然后我们通过do-while循环依次访问链表的每个结点,直到current指向头结点为止。在循环中,我们判断当前结点的数据和其序号的差的绝对值是否大于3,如果是则返回false。否则,我们将current指向下一个结点,index加1。最后,如果循环结束后都没有返回false,则说明链表满足条件,返回true。在main函数中,我们创建了两个含有不同元素的带头结点的单循环链表,并调用checkCircularLinkedList函数检查它们是否满足条件。最后我们释放了动态分配的内存。
阅读全文