栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

关于双向带头循环链表的记录

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

关于双向带头循环链表的记录

//DList.h  头文件

#include
#include
#include
typedef int TypeData;

typedef struct ListNode
{
    TypeData val;
    struct ListNode* next;
    struct ListNode* prev;
}LTNode;


LTNode* ListInit();
void ListPrint(LTNode* head);


void ListPushBack(LTNode* head, TypeData x);
void ListPopBack(LTNode* head);

void ListPushFront(LTNode* head, TypeData x);
void ListPopFront(LTNode* head);

void ListInsert(LTNode* pos, TypeData x);
void ListErase(LTNode* pos);

void ListDestroy(LTNode* head);

//DList.c 结构函数

#include "DList.h"

LTNode* ListInit()
{
    LTNode* head = (LTNode*)malloc(sizeof(LTNode));
    head->next = head;
    head->prev = head;
    return head;
}
void ListPrint(LTNode* head)
{
    LTNode* tail = head->next;
    assert(head);

    while(tail != head)
    {
        printf("%d ", tail->val);
        tail = tail->next;
    }
    printf("n");
}


void ListPushBack(LTNode* head, TypeData x)
{
    LTNode* tail = head;
    LTNode* cur = head->prev;
    LTNode* newnode = (LTNode*)malloc(sizeof(LTNode));
    assert(head);

    newnode->val = x;
    cur->next = newnode;
    newnode->prev = cur;

    tail->prev = newnode;
    newnode->next = tail;

}


void ListPopBack(LTNode* head)
{
    LTNode* tail = head->prev;
    LTNode* Tailprev = tail->prev;
    assert(head);
    assert(head->next != head);
    free(tail);
    tail = NULL;

    head->prev = Tailprev;
    Tailprev->next = head;
}


void ListPushFront(LTNode* head, TypeData x)
{
    LTNode* newnode= (LTNode*)malloc(sizeof(LTNode));
    LTNode* tail = head->next;

    newnode->val = x;
    newnode->prev = head;
    newnode->next = tail;

    head->next = newnode;
    tail->prev = newnode;
}
void ListPopFront(LTNode* head)
{
    LTNode* tail = head->next;
    LTNode* Tailnext = tail->next;
    assert(head);
    assert(head->next != head);
    free(tail);
    tail = NULL;

    head->next = Tailnext;
    Tailnext->prev = head;
}


void ListInsert(LTNode* pos, TypeData x)
{
    LTNode* newnode = (LTNode*)malloc(sizeof(LTNode));
    LTNode* posPrev = pos->prev;
    assert(pos);
    newnode->val = x;
    newnode->prev = posPrev;
    posPrev->next = newnode;

    newnode->next = pos;
    pos->prev = newnode;
}
void ListErase(LTNode* pos)
{
    LTNode* posPrev = pos->prev;
    LTNode* posNext = pos->next;

    assert(pos);
    free(pos);
    pos = NULL;

    posPrev->next = posNext;
    posNext->prev = posPrev;
}


void ListDestroy(LTNode* head)
{
    LTNode* cur = head->next;
    assert(head);

    while(cur != head)
    {
        LTNode* next = cur->next;
        free(cur);
        cur = next;
    }
    free(head);
}

//Test.c  主函数

#include "DList.h"


//void Test1()
//{
//    LTNode* plist = ListInit(plist);
//    ListPushBack(plist,1);
//    ListPushBack(plist,2);
//    ListPushBack(plist,3);
//    ListPushBack(plist,4);
//
//    ListPrint(plist);
//}

int main()
{
    LTNode* plist = ListInit();
    ListPushBack(plist,1);
    ListPushBack(plist,2);
    ListPushBack(plist,3);
    ListPushBack(plist,4);
    ListPrint(plist);

    ListPopBack(plist);
    ListPopBack(plist);
    ListPrint(plist);

    ListPushFront(plist,1);
    ListPushFront(plist,2);
    ListPushFront(plist,3);
    ListPushFront(plist,4);
    ListPrint(plist);

    ListPopFront(plist);
    ListPrint(plist);

    ListInsert(plist,9);
    ListPrint(plist);
    ListErase(plist->next);
    ListPrint(plist);
    return 0;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/957799.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号