双向链表&循环链表

思维导图:双向链表

循环链表思维导图

双向循环链表的 创建、判空、尾插、遍历、尾删、销毁的简单实现

分文件编译

创建

birCir_link.h

#ifndef __BIRCIR_LINK_H__
#define __BIRCIR_LINK_H__

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

typedef int DataType;

typedef struct node{

    union{
    int len;//头节点的数据域
    DataType data;//普通节点的数据域
    };
    struct node* next;//指针域,指向下一个节点
    struct node* prior;//指针域,指向上一个节点
}birLink,*birLinkPtr;

birLinkPtr create_link();
#endif
 

 birCir_link.c

#include"birCir_link.h"

birLinkPtr create_link(){

    birLinkPtr p =(birLinkPtr)malloc(sizeof(birLink));

    if(p == NULL){
        printf("failed to allocate memory");
        return NULL;
    }
    p->len = 0;
    p->next = p;
    p->prior = p;
    printf("create p success\n");
    return p;
}
 

main.c

#include"birCir_link.h"

int main(){

    birLinkPtr p =create_link();
}
 

 效果

判空

birCir_link.h

#ifndef __BIRCIR_LINK_H__
#define __BIRCIR_LINK_H__

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

typedef int DataType;

typedef struct node{

    union{
    int len;//头节点的数据域
    DataType data;//普通节点的数据域
    };
    struct node* next;//指针域,指向下一个节点
    struct node* prior;//指针域,指向上一个节点
}birLink,*birLinkPtr;

birLinkPtr create_link();

int null(birLinkPtr p);
#endif

 birCir_link.c

int null(birLinkPtr p){
    if(p == NULL){
        printf("error,p is NULL!");
        return -1;
    }
    return p->next == p;
}

尾插/遍历

birCir_link.h

#ifndef __BIRCIR_LINK_H__
#define __BIRCIR_LINK_H__

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

typedef int DataType;

typedef struct node{

    union{
    int len;//头节点的数据域
    DataType data;//普通节点的数据域
    };
    struct node* next;//指针域,指向下一个节点
    struct node* prior;//指针域,指向上一个节点
}birLink,*birLinkPtr;

birLinkPtr create_link();

int null(birLinkPtr p);

int end_insert(birLinkPtr p,DataType t);

void select_link(birLinkPtr p);
#endif

  birCir_link.c

int end_insert(birLinkPtr p,DataType t){
    if(p == NULL){
        printf("error, p is null\n");
        return 0;
    }
    birLinkPtr h =(birLinkPtr)malloc(sizeof(birLink));
    if(h == NULL){
        printf("failed to allocate memory\n");
        return 0;
    }
    h->data = t;
    h->next = NULL;
    h->prior = NULL;
    birLinkPtr z = p;
    while(z->next != p){
        z = z->next;
    }
    h->next = p;
    h->prior = z;
    z->next = h;
    p->len++;
    return 1;
}

void select_link(birLinkPtr p){
    if(p == NULL || null(p)){
        printf("error,p is null\n");
        return;
    }
    birLinkPtr q = p;
    for(int i=0;i<p->len;i++){
        q = q->next;
        printf("%d ",q->data);
    }
    putchar(10);
}
 

main.c

#include"birCir_link.h"

int main(){

    birLinkPtr p =create_link();

    end_insert(p,10);
    end_insert(p,20);
    end_insert(p,30);
    end_insert(p,40);
    end_insert(p,50);

    select_link(p);
    return 0;

效果: 

尾删

birCir_link.h

#ifndef __BIRCIR_LINK_H__
#define __BIRCIR_LINK_H__

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

typedef int DataType;

typedef struct node{

    union{
    int len;//头节点的数据域
    DataType data;//普通节点的数据域
    };
    struct node* next;//指针域,指向下一个节点
    struct node* prior;//指针域,指向上一个节点
}birLink,*birLinkPtr;

birLinkPtr create_link();

int null(birLinkPtr p);

int end_insert(birLinkPtr p,DataType t);

void select_link(birLinkPtr p);

int del_end(birLinkPtr p);
#endif

 birCir_link.c

int del_end(birLinkPtr p){
    if(p == NULL || null(p)){
        printf("delete fail,p is null");
        return 0;
    }
    birLinkPtr q = p;
    for(int i=0;i<p->len;i++){
        q = q->next;
    }
    q->prior->next = p;
    free(q);
    p->len--;
    return 1;

}

main.c

#include"birCir_link.h"

int main(){

    birLinkPtr p =create_link();

    end_insert(p,10);
    end_insert(p,20);
    end_insert(p,30);
    end_insert(p,40);
    end_insert(p,50);

    select_link(p);

    del_end(p);
    select_link(p);
    return 0;

效果: 

销毁

birCir_link.h

#ifndef __BIRCIR_LINK_H__
#define __BIRCIR_LINK_H__

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

typedef int DataType;

typedef struct node{

    union{
    int len;//头节点的数据域
    DataType data;//普通节点的数据域
    };
    struct node* next;//指针域,指向下一个节点
    struct node* prior;//指针域,指向上一个节点
}birLink,*birLinkPtr;

birLinkPtr create_link();

int null(birLinkPtr p);

int end_insert(birLinkPtr p,DataType t);

void select_link(birLinkPtr p);

int del_end(birLinkPtr p);

void del_free(birLinkPtr p);
#endif

birCir_link.c

void del_free(birLinkPtr p){
    if(p == NULL || null(p)){
        printf("free fail ,p is null");
        return;
    }
    while(p->next != p){
        del_end(p);
    }
    free(p);
    p=NULL;
    printf("free p success\n");
}
 

main.c

#include"birCir_link.h"

int main(){

    birLinkPtr p =create_link();

    end_insert(p,10);
    end_insert(p,20);
    end_insert(p,30);
    end_insert(p,40);
    end_insert(p,50);

    select_link(p);

    del_end(p);
    select_link(p);

    del_free(p);//调用销毁函数
    p=NULL;//防止申请的p变成野指针
    return 0;
}
 

效果: 

  • 29
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值