数据结构双向循环链表

主程序

#include "fun.h"

int main(int argc, const char *argv[])
{
    double_p H=create_head();
    insert_head(H,10);
    insert_head(H,20);
    insert_head(H,30);
    insert_head(H,40);
    insert_tail(H,50);
    show_link(H);
    del_tail(H);
    show_link(H);
    free_link(H);
    H=NULL;
    show_link(H);
    
    
    return 0;
}

源程序

#include "fun.h"

//创建头节点
double_p create_head()
{
    double_p H=(double_p)malloc(sizeof(node));
    if(H==NULL)
    {
        printf("节点申请失败\n");
        return NULL;
    }
    H->prior=H;
    H->next=H;
    H->len=0;
    return H;
}

//创建新节点
double_p create_new(typdata data)
{
    double_p new=(double_p)malloc(sizeof(node));
    if(new==NULL)
    {
        printf("节点申请失败\n");
        return NULL;
    }
    new->prior=NULL;
    new->next=NULL;
    new->data=data;
    return new;
}
//判空
int empty_link(double_p H)
{
    if(H==NULL)
    {
        printf("入参为空,请检查\n");
        return -1;
    }
    return H->len==0;
}
//头插
void insert_head(double_p H,typdata data)
{
    if(H==NULL)
    {
        printf("入参为空,请检查\n");
        return;
    }

    
    double_p new=create_new(data);
    new->next=H->next;
    H->next=new;
    new->next->prior=new;
    new->prior=H;

    H->len++;
}
//遍历
void show_link(double_p H)
{
    if(H==NULL)
    {
        printf("入参为空,请检查\n");
        return;
    }
    if(empty_link(H))
    {
        printf("链表已空,无需遍历\n");
        return;
    }
    double_p p=H;
    while(p->next!=H)
    {
        p=p->next;
        printf("<-%d->",p->data);
    }
    putchar(10);
}
//尾插
void insert_tail(double_p H,typdata data)
{
    if(H==NULL)
    {
        printf("入参为空,请检查\n");
        return;
    }
    double_p p=H;
    while(p->next!=H)
    {
        p=p->next;
    }
    double_p new=create_new(data);
    new->next=p->next;
    p->next=new;
    new->prior=p;
    H->prior=new;


    H->len++;
}

//尾删
void del_tail(double_p H)
{
    if(H==NULL)
    {
        printf("入参为空,请检查\n");
        return;
    }
    if(empty_link(H))
    {
        printf("链表已空,无需删除\n");
        return;
    }
    
    double_p p=H;
    while(p->next->next!=H)
    {
        p=p->next;
    }
    free(p->next);
    p->next=NULL;
    p->next=H;
    H->prior=p;

    H->len--;
}
//释放链表
void free_link(double_p H)
{
    if(H==NULL)
    {
        printf("入参为空,请检查\n");
    } 
        while(H->next != H)
        {
           del_tail(H);
        }
    
        free(H);
        H=NULL;
        
    
       
    
}

                    头文件

#ifndef __FUN_H_
#define __FUN_H_

#include <myhead.h>

typedef int typdata;
typedef struct node
{
    union
    {
        typdata data;
        int len;
    };
    struct node * prior;
    struct node *next;
}node,*double_p;

double_p create_head();//创建头节点
double_p create_new(typdata data);//创建新节点
int empty_link(double_p H);//判空
void insert_head(double_p H,typdata data);//头插
void show_link(double_p);//遍历
void insert_tail(double_p H,typdata data);//尾插
void del_tail(double_p H);//尾删
void free_link(double_p H);//释放链表
#endif

相关推荐

  1. 数据结构_带头双向循环

    2024-07-10 07:10:04       40 阅读
  2. 数据结构篇 单 循环 双向

    2024-07-10 07:10:04       68 阅读
  3. 数据结构基础(带头节点的双向循环

    2024-07-10 07:10:04       63 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-10 07:10:04       99 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-10 07:10:04       107 阅读
  3. 在Django里面运行非项目文件

    2024-07-10 07:10:04       90 阅读
  4. Python语言-面向对象

    2024-07-10 07:10:04       98 阅读

热门阅读

  1. [linux] 如何优雅的用vim阅读jsonl文件

    2024-07-10 07:10:04       31 阅读
  2. AMBA总线协议与PCIe总线协议的区别

    2024-07-10 07:10:04       32 阅读
  3. YModem在Android上的实现

    2024-07-10 07:10:04       29 阅读
  4. Selenium 切换窗口

    2024-07-10 07:10:04       27 阅读
  5. PCA和PCoA分析的python代码

    2024-07-10 07:10:04       35 阅读
  6. 24/07/09数据结构(3.1206)数组OJ单链表实现

    2024-07-10 07:10:04       24 阅读
  7. [Flutter] Android Studio pub get 不起作用

    2024-07-10 07:10:04       31 阅读
  8. PHP数据结构之队列

    2024-07-10 07:10:04       25 阅读