#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node *next;
} Node;
// 创建循环链表
Node* createCircularList(int data) {
Node *head = (Node*)malloc(sizeof(Node));
head->data = data;
head->next = head; // 创建循环,指向自身
return head;
}
// 在循环链表中插入节点
Node* insertNode(Node *head, int data) {
Node *newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->next = head->next; // 新节点指向原来的第二个节点
head->next = newNode; // 头节点指向新节点,完成插入
return head;
}
// 遍历循环链表
void traverseList(Node *head) {
Node *temp = head;
do {
printf("%d ", temp->data);
temp = temp->next;
} while (temp != head); // 当再次回到头节点时结束
printf("\n");
}
int main() {
Node *head = createCircularList(10);
head = insertNode(head, 20);
head = insertNode(head, 30);
traverseList(head);
return 0;
}
08-01
3867
08-07
1592
09-03
624
03-21
3809
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交