双向循环链表的创建

双向循环链表的创建

1 节点的创建

struct node
{
    dataType data;
    struct node *prev;
    struct node *next;
};

2 管理结构体

struct headNode
{
    struct node *first;//指向链表首节点
    struct node *last;//指向链表尾节点
    dataType nodeNumder;//链表节点数
};

3 主代码(可运行)

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>

typedef int dataType;

//节点
struct node
{
    dataType data;
    struct node *prev;
    struct node *next;
};

//头结点,管理链表
struct headNode
{
    struct node *first;//指向链表首节点
    struct node *last;//指向链表尾节点
    dataType nodeNumder;//链表节点数
};

//头插法
void *addHead(struct headNode *head,struct node *pnew)
{
    pnew->next=head->first;
    pnew->prev=head->first->prev;
    head->first->prev=pnew;
    head->first=pnew;

}

//尾插法
void addTail(struct headNode *head,struct node *pnew)
{
    pnew->prev = head->last;
    head->last->next = pnew;
    head->last = pnew;
}

//初始化头结点
struct headNode *init_headNode()
{
    struct headNode *head=malloc(sizeof(struct headNode));
    if(head == NULL)
    {
        perror("create head failed:");
        return NULL;
    }
    //初始化
    head->first=NULL;
    head->last=NULL;
    head->nodeNumder=0;
    return head;
}

//创建新节点
struct node *create_newNode(dataType data)
{
    struct node *pnew=malloc(sizeof(struct node));
    if(pnew == NULL)
    {
        perror("create new node failed:");
        return NULL;
    }

    pnew->data=data;
    pnew->prev=NULL;
    pnew->next=NULL;
    return pnew;

}

//创建新链表
struct headNode *create_list()
{
    //初始化头节点
    struct headNode *head=init_headNode();
    if(head==NULL)
    {
        printf("over\n");
        // exit(0);
    }

    while (1)
    {
        dataType data;
        scanf("%d",&data);
        if(data==0)
            break;
        
        //创建新节点
        struct node *pnew=create_newNode(data);
        if(head->first==NULL)
        {
            head->first=pnew;
            head->last=pnew;
        }
        else
        {
            // addHead(head,pnew);
            addTail(head,pnew);

        }
        head->nodeNumder++;

    }

    if(head->nodeNumder != 0)
    {
        head->last->next = head->first;
        head->first->prev = head->last;
    }

    return head;

}

//显示链表
void showList(struct headNode* head)
{
    if(head->nodeNumder == 0)
    {
        printf("链表为空\n");
        return;
    }

    // for(struct node* p = head->first;p->next!=head->first;p = p->next)
    // {
    //     printf("%d\t",p->data);
    // }

    struct node *p=head->first;
    int n=head->nodeNumder;
    while (n--)
    {
        printf("%d\t",p->data);
        p=p->next;
    }
    
    printf("\n");
    printf("链表节点数为:%d\n",head->nodeNumder);
}

//删除
struct headNode *del_node(struct headNode *head,dataType data)
{
    // 找节点
    int n=head->nodeNumder;
    struct node *p = head->first;
    // while(p->next!=head->first)
    while(n!=0)
    {
        if(p->data == data)
        {
            break;
        }
            
        else
            p = p->next;
        
        n--;
    }

    if(n==0)
    {
        printf("没有找到\n");
    }

    else if(p->next==head->first)
    {
        p->prev->next = NULL;
        p->prev = NULL;
        free(p);
        head->nodeNumder--;
    }

    // 如果是第一个节点
    else if(head->first->data == data)
    {
        head->first = p->next;
        p->next = NULL;
        p->prev = NULL;
        free(p);
        head->nodeNumder--;
    }

    else
    {
        p->prev->next = p->next;
        p->next->prev = p->prev;
        p->next = NULL;
        p->prev = NULL;
        head->nodeNumder--;

        free(p);
    }
    
    return head;
}

//插入节点
struct headNode *add_node_list(struct headNode *head,dataType newData,dataType data)
{
    struct node *pnew=create_newNode(newData);
    int n=head->nodeNumder;
    struct node *p=head->first;
    // while (p->next!=head->first)
    while(n!=0)
    {
        if(p->data==data)
            break;
        p=p->next;
        n--;
    }


    if(n==0)
    {
        addTail(head,pnew);
    }

    else if(p->data==head->first->data)
    {
        addHead(head,pnew);
    }

    else
    {
        pnew->next=p;
        pnew->prev=p->prev;
        p->prev->next = pnew;
        p->prev = pnew;

    }
    
    head->nodeNumder++;
    return head;
}

//更新链表
struct headNode *updata_node(struct headNode *head,dataType data1,dataType data2)
{
    struct node *p=head->first;
    while (p->next!=head->first)
    {
        if(p->data==data1)
        {
            p->data=data2;
        }

        p=p->next;
    }
}

//销毁
struct headNode *destory(struct headNode *head)
{
    if(head==NULL)
    {
        printf("链表为空\n");
        free(head);
    }

    struct node *temp=NULL;
    struct node *p=head->first;
    int n=head->nodeNumder;
    while (n--)
    {
        temp=p->next;
        p->next=NULL;
        free(p);
        head->nodeNumder--;

    }

    return head;
}

//判断链表是否有环
bool judge_circle(struct headNode *head)
{
    struct node *fast=head->first;

    struct node *slow=head->first;
    do
    {
        fast=fast->next;
        
        if(fast!=NULL)
        {
            fast=fast->next;
        }
        else
        {
            return false;
        }
        slow=slow->next;


    } while (fast!=NULL&&slow!=NULL&&fast!=slow);
    return fast==slow;
    
}

int main(int argc, char const *argv[])
{
    struct headNode *head = create_list();

    if(head==NULL)
    {
        printf("创建链表失败");
        return -1;
    }

    // updata_node(head,1,20);
    del_node(head,2);
    // add_node_list(head,1,2);
    // destory(head);
    // if(judge_circle(head))
    // {
    //     printf("有环\n");
    // }
    // else
    // {
    //     printf("无环\n");
    // }

    showList(head);

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
双向循环链表是一种特殊的链表,它的每个节点都有两个指针,一个指向前一个节点,一个指向后一个节点,而最后一个节点的后继指针指向头结点,头结点的前驱指针指向最后一个节点。下面是一个简单的 C 语言程序示例,用于创建双向循环链表: ```c #include <stdio.h> #include <stdlib.h> struct node { int data; struct node *prev; struct node *next; }; struct node *head = NULL; struct node *tail = NULL; void create_list(int n); void display_list(); int main() { int n; printf("Enter the number of nodes: "); scanf("%d", &n); create_list(n); printf("Data in the list:\n"); display_list(); return 0; } void create_list(int n) { int i, data; struct node *newNode; if (n <= 0) { printf("List size must be greater than zero.\n"); return; } head = (struct node*)malloc(sizeof(struct node)); if (head == NULL) { printf("Unable to allocate memory."); return; } printf("Enter the data of node 1: "); scanf("%d", &data); head->data = data; head->prev = NULL; head->next = NULL; tail = head; for (i = 2; i <= n; i++) { newNode = (struct node*)malloc(sizeof(struct node)); if (newNode == NULL) { printf("Unable to allocate memory."); break; } printf("Enter the data of node %d: ", i); scanf("%d", &data); newNode->data = data; newNode->prev = tail; newNode->next = head; tail->next = newNode; tail = newNode; head->prev = tail; } printf("List created successfully.\n"); } void display_list() { struct node *current; if (head == NULL) { printf("List is empty.\n"); return; } current = head; printf("List is:\n"); do { printf("%d\n", current->data); current = current->next; } while (current != head); } ``` 在这个程序中,我们定义了一个结构体 `node`,它包含三个成员:`data`,`prev` 和 `next`,分别表示节点的数据、前驱指针和后继指针。我们还定义了两个指针 `head` 和 `tail`,分别指向链表的头结点和尾结点。 在 `create_list` 函数中,我们首先创建头结点,并让 `head` 和 `tail` 指向头结点。然后通过 `for` 循环创建其他节点,并将它们插入到链表的尾部。每个节点的前驱指针指向上一个节点,后继指针指向下一个节点,最后一个节点的后继指针指向头结点,头结点的前驱指针指向最后一个节点。 在 `display_list` 函数中,我们首先判断链表是否为空,然后通过 `do-while` 循环遍历链表,输出每个节点的数据。由于这是一个双向循环链表,循环条件为 `current != head`。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值