双向循环链表的定义以及基本操作 超详细!


#include<stdio.h>//头文件
#include<stdlib.h>//头文件 malloc函数
struct node//结点结构体定义
{
    struct node* prior;//前指针 指向前一个节点
    struct node* next;//后指针 指向后一个节点
    int data;
};
typedef struct node node;//为"struct node"起别名node  指结点
typedef struct node linklist;//为"struct node"起别名linklist  指链表

//初始化双向链表
void initialize_list(linklist* plist)
{
//    plist = (node*)malloc(sizeof(node));
//此句是错误的!!函数输入参数那里已经为plist分配到了地址!
//如果再分配,之前的地址就丢了,就找不到链表了!
    plist->prior = plist;
    plist->next = plist;
    plist->data = -999;
    return;
}

//后插法创建链表
void tail_insert(linklist* plist, int n, int a[])
{
    node* tail;
    tail = plist;

    for(int i=0; i<n; i++)
    {
        node* point = (node*)malloc(sizeof(node));
        point->data = a[i];
        point->next = plist;
        point->prior = tail;

        plist->prior = point;

        tail->next = point;

        tail = point;
    }
    return;
}

//在指定位置插入某个元素
void insert_element(linklist* plist, int x, int e)
{
    node* tail;
    tail = plist;

    int i = 0;
    while(i != x - 1)
    {
        tail = tail->next;
        i++;
    }
    node* point = (node*)malloc(sizeof(node));
    point->data = e;

    tail->next->prior = point;
    point->next = tail->next;

    point->prior = tail;
    tail->next = point;

    tail = point;

    return;
}

//删除制定位置的元素
void delete_element(linklist* plist, int x)
{
    node* tail = plist;
    node* p;
    int i = 0;
    while(i != x - 1)
    {
        tail = tail->next;
        i++;
    }
    p = tail->next;
    tail->next = tail->next->next;
    tail->next->prior = tail;

    free(p);
    return;
}

//后向遍历+计数
int traversal_forth(linklist* plist)
{
    node* p = plist;
    p = p->next;
    int count = 0;
    while(p != plist)
    {
        count++;
        p = p->next;
    }
    return count;
}

//前向遍历+计数
int traversal_back(linklist* plist)
{
    node* p = plist;
    p = p->prior;
    int count = 0;
    while(p != plist)
    {
        count++;
        p = p->prior;
    }
    return count;
}

//打印链表
void print_list(linklist* plist)
{
    node* p = plist;

    while(p->next != plist)
    {
        p = p->next;
        printf("%d  ", p->data);
    }
    printf("\n");
    return;
}

int main()
{
    linklist L;//创建

    initialize_list(&L);//初始化

    printf("how many numbers in this list?\n");
    int n;
    scanf("%d", &n);
    int a[100], b;
    printf("please input those numbers:\n");
    for(int i=0; i<n; i++)
    {
        scanf("%d", &b);
        a[i] = b;
    }
    tail_insert(&L, n, a);//输入元素
    print_list(&L);//打印链表


    insert_element(&L, 4, 999);//在指定位置插入某个元素
    printf("after you insert the element:");
    print_list(&L);

    delete_element(&L, 4);//删除指定位置的元素
    printf("after you delete the element:");
    print_list(&L);

    int count1 = traversal_back(&L);
    int count2 = traversal_forth(&L);
    printf("后序遍历计数结果:%d \n", count1);
    printf("前序遍历计数结果:%d \n", count2);

    return 0;
}

如有错误 欢迎指正!~
  • 7
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值