下面是 C 语言中循环链表和双链表的链式表示和实现示例:
**循环链接表:**
'''c
#include
#include
struct Node {
int data;
struct Node* next;
};
// Function to create a new node
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
// Function to insert an element at the beginning of the circular linked list
struct Node* insertAtBeginning(struct Node* head, int data) {
struct Node* newNode = createNode(data);
if (head == NULL) {
newNode->next = newNode;
return newNode;
}
newNode->next = head->next;
head->next = newNode;
return head;
}
// Function to insert an element at the end of the circular linked list
struct Node* insertAtEnd(struct Node* head, int data) {
struct Node* newNode = createNode(data);
if (head == NULL) {
newNode->next = newNode;
return newNode;
}
newNode->next = head->next;
head->next = newNode;
return newNode;
}
// Function to display the elements of the circular linked list
void displayList(struct Node* head) {
if (head == NULL) {
return;
}
struct Node* current = head->next;
do {
printf("%d ", current->data);
current = current->next;
} while (current != head->next);
printf("\n");
}
int main() {
struct Node* head = NULL;
head = insertAtBeginning(head, 3);
head = insertAtEnd(head, 5);
head = insertAtEnd(head, 7);
displayList(head);
return 0;
}
'''
在此实现中,通过将最后一个节点的“下一个”指针设置为指向头节点来创建循环链表。“createNode”函数使用给定数据创建一个新节点。“insertAtBeginning”函数通过更新新节点的“next”指针以指向当前头节点,并将头节点的“next”指针更新为指向新节点,在列表的开头插入新节点。“insertAtEnd”函数通过将新节点的“next”指针更新为指向头节点的“next”指针,并将头节点的“next”指针更新为指向新节点,在列表末尾插入新节点。'displayList' 函数遍历循环链表并打印每个节点的数据,直到它再次到达起点。
**双链表:**
'''c
#include
#include
struct Node {
int data;
struct Node* prev;
struct Node* next;
};
// Function to create a new node
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->prev = NULL;
newNode->next = NULL;
return newNode;
}
// Function to insert an element at the beginning of the doubly linked list
struct Node* insertAtBeginning(struct Node* head, int data) {
struct Node* newNode = createNode(data);
if (head == NULL) {
return newNode;
}
newNode->next = head;
head->prev = newNode;
return newNode;
}
// Function to insert an element at the end of the doubly linked list
struct Node* insertAtEnd(struct Node* head, int data) {
struct Node* newNode = createNode(data);
if (head == NULL) {
return newNode;
}
struct Node* current = head;
while (current->next != NULL){
current = current->next;
}
current->next = newNode;
newNode->prev = current;
return head;
}
// Function to display the elements of the doubly linked list in forward direction
void displayForward(struct Node* head) {
struct Node* current = head;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
// Function to display the elements of the doubly linked list in reverse direction
void displayReverse(struct Node* head) {
struct Node* current = head;
while (current->next != NULL) {
current = current->next;
}
while (current != NULL) {
printf("%d ", current->data);
current = current->prev;
}
printf("\n");
}
int main() {
struct Node* head = NULL;
head = insertAt this point, the response was cut off. Apologies for the incomplete answer. Let me continue with the remaining code for the doubly linked list implementation:
```c
head = insertAtBeginning(head, 3);
head = insertAtEnd(head, 5);
head = insertAtEnd(head, 7);
displayForward(head);
displayReverse(head);
return 0;
}
'''
在此实现中,双向链表的每个节点都包含一个“prev”指针和一个“next”指针,它们分别指向上一个和下一个节点。“createNode”函数使用给定数据创建一个新节点,并初始化指向“NULL”的“prev”和“next”指针。“insertAtBeginning”函数通过更新新节点的“next”指针以指向当前头节点,并将头节点的“prev”指针更新为指向新节点,在列表的开头插入新节点。“insertAtEnd”函数通过遍历列表直到到达最后一个节点,然后更新最后一个节点的“next”指针以指向新节点,并将新节点的“prev”指针更新为指向最后一个节点,从而在列表末尾插入一个新节点。'displayForward' 函数向前(从头到尾)遍历双向链表并打印每个节点的数据。'displayReverse' 函数以相反的方向(从尾部到正面)遍历双向链表并打印每个节点的数据。
在“main”函数中,我们创建一个空的双向链表,然后使用“insertAtBeginning”和“insertAtEnd”函数在列表的开头和结尾插入元素。最后,我们使用 'displayForward' 和 'displayReverse' 函数以正向和反向打印列表的元素。可以根据对循环链表和双向链表实现的要求修改和扩展此代码。