温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C语言数据结构 双向链表以及基本功能实现

发布时间:2020-05-30 11:48:41 来源:网络 阅读:712 作者:sonissa 栏目:编程语言

项目头文件:

#ifndef _LIST_H_
#define _LIST_H_
#include <stdio.h>
#include<stdlib.h>

typedef int LTDataType;
typedef struct ListNode {
    LTDataType _data;
    struct ListNode* _next;
    struct ListNode* _prev;
}ListNode;

typedef struct List {
    ListNode* _head;
}List;

void ListInit(List* plist);//初始化
void ListDestory(List* plist);//清空链表

void ListPushBack(List* plist, LTDataType x);//后插
void ListPopBack(List* plist);//后删
void ListPushFront(List* plist, LTDataType x);
void ListPopFront(List* plist);

ListNode* ListFind(List* plist, LTDataType x);//查找

void ListMerge(List* pList1, List* pList2);//将有序链表有序合并

void ListInsertFront(ListNode* pos, LTDataType x);
void ListInsertAfter(ListNode* pos, LTDataType x);
// ????pos??????????
void ListErase(ListNode* pos);//删除
void ListRemove(List* plist, LTDataType x);//移除指定
void ListRemoveAll(List* plist, LTDataType x);//移除指定的全部
void ListPrint(List* plist);//打印

#endif /*_LIST_H_*/

具体功能实现:

void ListInit(List* plist)
{
    plist->_head = (ListNode*)malloc(sizeof(ListNode));
    plist->_head->_next = plist->_head;
    plist->_head->_prev = plist->_head;
}
void ListDestory(List* plist)
{
    while (plist->_head->_next != plist->_head)
    {
        ListPopFront(plist);
    }
    free(plist->_head);
    plist->_head = NULL;
}

void ListPushBack(List* plist, LTDataType x)
{
    ListInsertFront(plist->_head, x);
}
void ListPopBack(List* plist)
{
    ListErase(plist->_head->_prev);
}
void ListPushFront(List* plist, LTDataType x)
{
    ListInsertAfter(plist->_head, x);
}
void ListPopFront(List* plist)
{
    ListErase(plist->_head->_next);
}

ListNode* ListFind(List* plist, LTDataType x)
{
    ListNode* cur;
    for (cur = plist->_head->_next; cur != plist->_head; cur = cur->_next)
    {
        if (cur->_data == x)
        {
            return cur;
        }
    }
    return NULL;
}

void ListMerge(List* pList1, List* pList2)//将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的
{
    ListNode* cur1=pList1->_head->_next, *cur2=pList2->_head->_next;
    ListNode* tmp1, *tmp2;
    while (cur1 != pList1->_head&&cur2 != pList2->_head)
    {
        if (cur1->_data > cur2->_data)
        {
            tmp1 = cur1->_prev;
            tmp2 = cur2->_next;

            tmp1->_next = cur2;
            cur2->_next = cur1;
            cur1->_prev = cur2;
            cur2->_prev = tmp1;
            cur2 = tmp2;
        }
        else
        {
            cur1 = cur1->_next;
        }
    }

    if (cur1 == pList1->_head)
    {
        tmp2 = pList2->_head->_prev;
        cur2->_prev = cur1->_prev;
        cur1->_prev->_next = cur2;
        tmp2->_next = cur1;
        cur1->_prev = tmp2;
    }
    free(pList2->_head);
    pList2->_head = NULL;
}

void ListInsertFront(ListNode* pos, LTDataType x)
{
    ListNode* cur = (ListNode*)malloc(sizeof(ListNode));
    cur->_data = x;
    cur->_next = pos;
    cur->_prev = pos->_prev;
    pos->_prev->_next = cur;
    pos->_prev = cur;
}
void ListInsertAfter(ListNode* pos, LTDataType x)
{
    ListNode* cur = (ListNode*)malloc(sizeof(ListNode));
    cur->_data = x;
    cur->_prev = pos;
    cur->_next = pos->_next;
    pos->_next->_prev = cur;
    pos->_next = cur;
}
// ????pos??????????
void ListErase(ListNode* pos)
{
    pos->_prev->_next = pos->_next;
    pos->_next->_prev = pos->_prev;
    free(pos);
}
void ListRemove(List* plist, LTDataType x)
{
    ListNode * cur = ListFind(plist, x);

    if (cur)
    {
        ListErase(cur);
    }
}
void ListRemoveAll(List* plist, LTDataType x)
{
    ListNode* cur;
    for (cur = plist->_head->_next; cur != plist->_head; cur = cur->_next)
    {
        if (cur->_data == x)
        {
            ListErase(cur);
        }
    }
}

void ListPrint(List* plist)
{
    ListNode* cur;
    if (plist->_head == NULL)
    {
        printf("NULL\n");
        return;
    }
    printf("head->");
    for (cur = plist->_head->_next; cur != plist->_head; cur = cur->_next)
    {
        printf("%d-> ",cur->_data);
    }
    printf("head\n");
}
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI