循环链表和双向链表

下面是 C 语言中循环链表和双链表的链式表示和实现示例:

 

**循环链接表:**

 

'''c

 

#include <stdio.h>

#include <stdlib.h>

 

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 <stdio.h>

#include <stdlib.h>

 

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' 函数以正向和反向打印列表的元素。可以根据对循环链表和双向链表实现的要求修改和扩展此代码。

 

相关推荐

  1. 循环双向

    2024-01-08 23:46:01       28 阅读
  2. 数据结构之篇 单 循环 双向

    2024-01-08 23:46:01       39 阅读
  3. C语言实现双向循环

    2024-01-08 23:46:01       38 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-08 23:46:01       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-08 23:46:01       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-08 23:46:01       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-08 23:46:01       18 阅读

热门阅读

  1. vue3.0与vue2.0-prop

    2024-01-08 23:46:01       36 阅读
  2. 2023秋电子科大信软 程算I 机考真题

    2024-01-08 23:46:01       27 阅读
  3. 子类重写父类的方法

    2024-01-08 23:46:01       35 阅读
  4. 用Python实现组件化大屏

    2024-01-08 23:46:01       41 阅读
  5. C语言中的关键字与标识符详解

    2024-01-08 23:46:01       32 阅读
  6. 列表如何查找索引与字符串

    2024-01-08 23:46:01       30 阅读