循环链表和双向链表

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

 

  • 16
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

BlurryFace36549

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值