【数据结构】双向链表(6)

双向链表

学习了前面的之后感觉不是很难了

typedef int ElemType;
struct Node
{
    ElemType data;
    struct Node * pPrior;
    struct Node * pNext;
};

//带头结点的双向循环链表基本操作(14个)
//1产生一个空的双向循环链表
struct Node * init_list()
{
    struct Node * pHead;
    pHead = (struct Node *)malloc(sizeof(struct Node));
    if (pHead)
        pHead->pNext = pHead->pPrior = pHead;
    else
        exit(-1);
    return pHead;
}

//2将链表置为空
void clear_list(struct Node * pHead)
{
    struct Node * p = pHead->pNext;
    while (p != pHead)
    {
        p = p->pNext;
        free(p->pPrior);    //实际上是释放的传入的p
    }
    pHead->pNext = pHead->pPrior = pHead;
}

//3销毁双向循环链表
void destroy_list(struct Node * pHead)
{
    clear_list(pHead);
    free(pHead);
    pHead = NULL;
}

//4判断是否为空
bool is_empty(struct Node * pHead)
{
    if (pHead->pNext == pHead && pHead->pPrior == pHead)
        return true;
    else
        return false;
}

//5返回链表中元素个数
int length_list(struct Node * pHead)
{

    int cnt = 0;
    struct Node * p;
    p = pHead->pNext;
    while (pHead != p)
    {
        cnt++;
        p = p->pNext;
    }
    return cnt;
}

//6取得第pos个位置上元素的值
bool get_list(struct Node * pHead, int pos, ElemType * value)
{
    int i = 1;
    struct Node * p = pHead->pNext;
    while (p != pHead && i < pos)
    {
        i++;
        p = p->pNext;
    }
    if (p == pHead || i > pos)
        return false;

    *value = p->data;

    return true;
}

//7给定一个数,得到线性表中与此数相等的最小的位序
bool compare(ElemType e1, ElemType e2)
{
    if (e1 == e2)
        return true;
    else
        return false;
}
int locate_list(struct Node * pHead, ElemType val)
{
    int i = 0;
    struct Node * p = pHead->pNext;
    while (p != pHead)
    {
        i++;
        if(compare(p->data, val))
            return i;

        p = p->pNext;
    }
    return 0;
}

//8给定一个数,判断是否在链表里,如果在,从第二个元素开始得到此数的前一个数
bool prior_list(struct Node * pHead, ElemType cur_e, ElemType * pre_e)
{
    struct Node * p = pHead->pNext->pNext;  //p指向第2个元素

    while (p != pHead)
    {
        if (p->data == cur_e)
        {
            *pre_e = p->pPrior->data;
            return true;
        }
        p = p->pNext;
    }
    return false;
}

//9返回cur的后一个
bool next_list(struct Node * pHead, ElemType cur_e, ElemType * next_e)
{
    struct Node * p = pHead->pNext->pNext;
    while (p != pHead)
    {
        if (p->pPrior->data == cur_e)
        {
            *next_e = p->data;
            return true;
        }
        p = p->pNext;
    }
    return false;
}

//10插入数据
//10-1首先得到指向pos位置数据的结点
struct Node * get_listP(struct Node * pHead, int pos)
{
    int i;
    struct Node * p = pHead;
    if (pos < 0 || pos > length_list(pHead))
        return NULL;
    for (i = 1; i <= pos; ++i)
    {
        p = p->pNext;
    }
    return p;
}
//10-2插入数据
bool insert_list(struct Node * pHead, int pos, int value)
{

    struct Node * p;
    if (pos < 1 || pos > length_list(pHead) + 10)
        return false;
    p = get_listP(pHead, pos - 1);
    if (p == NULL)
        return false;
    struct Node * pNew = (struct Node *) malloc(sizeof (struct Node));
    if (pNew == NULL)
        return false;

    pNew->data = value;
    pNew->pPrior = p;
    pNew->pNext = p->pNext;
    p->pNext->pPrior = pNew;
    p->pNext = pNew;

    return true;
}

//11删除数据
bool delete_list(struct Node * pHead, int pos, int * val)
{
    struct Node * p;
    p = get_listP(pHead, pos);
    if (pos <= 0 && pos > length_list(pHead) + 1)
        return false;

    *val = p->data;
    p->pPrior->pNext = p->pNext;
    p->pNext->pPrior = p->pPrior;
    free(p);

    return true;
}

//12正序遍历
void traverse_list(struct Node * pHead)
{
    struct Node * p = pHead->pNext;
    while (p != pHead)
    {
        printf("%d ", p->data);
        p = p->pNext;
    }
    printf("\n");
}

//13正序遍历
void traverse_list_back(struct Node * pHead)
{
    struct Node * p = pHead->pPrior;
    while (p != pHead)
    {
        printf("%d ", p->data);
        p = p->pPrior;
    }
    printf("\n");
}

测试

#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <stdbool.h>
int main() {
    struct Node * pHead;
    pHead = init_list();

    for (int i = 0; i < 6; i++)
        insert_list(pHead, i + 1, 2 * i);

    insert_list(pHead, 7, 15);
    insert_list(pHead, 8, 17);
    traverse_list(pHead);
    traverse_list_back(pHead);

    int len = length_list(pHead);
    printf("len = %d\n", len);

    ElemType val;
    get_list(pHead, 4, &val);
    printf("val = %d\n", val);

    ElemType delete_val;
    delete_list(pHead, 5, &delete_val);
    printf("delete_val = %d\n", delete_val);
    traverse_list(pHead);

    int cnt = locate_list(pHead, 17);
    printf("cnt = %d\n", cnt);

    ElemType pre_e;
    ElemType next_e;
    prior_list(pHead, 10, &pre_e);
    next_list(pHead, 10, &next_e);
    printf("pre_e = %d\n", pre_e);
    printf("next_e = %d\n", next_e);
    traverse_list(pHead);
    traverse_list_back(pHead);

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值