数据结构(双向链表的操作)

接下来进行操作……

1,链表的定义……

对于双向链表的操作,就需要两个指针来进行操作,一个称为前驱指针,一个是后继指针来进行操作的

typedef int LTDateType;

typedef struct ListNode{

struct ListNode* prev;

struct ListNode* next;

LTDateType data;

}ListNode;

2,双向链表的接口定义

接下来的操作进行的比较多……

//1,链表的初始化

ListNode* ListCreat();

//2,链表的打印

void ListPrint(ListNode* phead);

//3,链表的尾插

void ListPushBack(ListNode* phead, LTDateType x);

//4,链表的头插

void ListPushFront(ListNode* phead, LTDateType x);

//5,链表是否为空

bool ListEmpty(ListNode* phead);

//6,链表的尾删

void ListPopBack(ListNode* phead);

//7,链表的头删

void ListPopFront(ListNode* phead);

//8,链表的查找

ListNode* ListFind(ListNode* phead, LTDateType x);

//9,链表在pos前面的插入

void ListInsert(ListNode* pos, LTDateType x);

//10,链表在pos位置处的删除

void ListErase(ListNode* pos);

//11,链表的销毁

void ListDestroy(ListNode* phead);

3,具体的操作实现

操作太多就不一一实现了……

1,进行链表的初始化操作

ListNode* BuyListNode(LTDateType x)
{
    ListNode* newnode = (ListNode*)malloc(sizeof(ListNode));
    if (newnode == NULL)
    {
        perror("malloc");
        return NULL;
    }
    newnode->data = x;
    newnode->next = NULL;
    newnode->prev = NULL;
    return newnode;
}
//链表的初始化
ListNode* ListCreat()
{
    ListNode* phead = BuyListNode(-1);
 
    phead->next = phead;
    phead->prev = phead;
    return phead;
}

2,进行链表的头插法操作
void ListPushFront(ListNode* phead, LTDateType x)
{
    assert(phead);
    ListNode* newnode = BuyListNode(x);
    ListNode* first = phead->next;
 
    phead->next = newnode;
    newnode->prev = phead;
    newnode->next = first;
    first->prev = newnode;
}

3,链表的销毁操作
void ListDestroy(ListNode* phead)
{
    assert(phead);
    ListNode* cur = phead->next;
    while (cur != phead)
    {
        ListNode* next = cur->next;
        free(cur);
        cur = next;
    }
    free(phead);
    phead = NULL;
}

4,链表的查找操作

//链表的查找
ListNode* ListFind(ListNode* phead,LTDateType x)
{
    assert(phead);
    assert(!ListEmpty(phead));
    ListNode* cur = phead->next;
    while (cur != phead)
    {
        if (cur->data == x)
        {
            return cur;
        }
        cur = cur->next;
    }
    return NULL;
}

好了,分享就到这里,如有有错误 ,请及时指出,万分感谢……

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值