数据结构--------------双向链表

1 概念与结构:

数据结构--------------双向链表_第1张图片

注意:这⾥的“带头”跟前⾯我们说的“头结点”是两个概念,实际前⾯的在单链表阶段称呼不严
谨。 带头链表⾥的头结点,实际为“哨兵位”,哨兵位结点不存储任何有效元素,只是站在这⾥“放哨
的”。
接下来就让我们来实现双向链表。
也是分为三个部分代码如下
List.h

#pragma once
#include
#include
#include
#include

typedef int SLDatetype;

typedef struct List {
    SLDatetype data;
     struct List* next;
    struct List* pre;    
}list;
 
list *chushihua();
//尾插
void Insertback(list *phead,SLDatetype x);
//头插
void Insertfront(list* phead, SLDatetype x);
//尾删
void ltpopback(list* phead);
//
void ltpopfront(list* phead);
//判断链表是否为空
bool emptylist(list* phead);
//查找
list* findlist(list* phead,SLDatetype x);
//在指定位置后插入数据
void listinserback(list* pos, SLDatetype x);
//在指定位置前插入数据
void listinserfront(list* pos, SLDatetype x);
//删除指定位置
void dellist(list* pos);
//销毁链表
void listdestory(list* phead);

List.c

#include"List.h"
void printlist(list *phead)
{
    list *peur = phead->next;
    while (peur != phead)
    {
        printf("%d ->", peur->data);
        peur = peur->next;
    }
    printf("\n");
}
list* buynode(SLDatetype x)
{
    list* phead = (list*)malloc(sizeof(list));
    phead->data = x;
    phead->next = phead->pre = phead;
    return phead;
}
list* chushihua()
{
    list* newnode = buynode(-1);
    return newnode;
}
void Insertback(list* phead, SLDatetype x)
{
    assert(phead);
    list *newcode = buynode(x);
    list* peur=phead;
    newcode->pre = peur->pre;
    newcode->next = peur;
    peur->pre->next = newcode;
    peur->pre = newcode;
}
    void Insertfront(list* phead, SLDatetype x)
    {
        assert(phead);
        list* newcode = buynode(x);
        list* peur = phead;
        newcode->next = peur->next;
        newcode->pre = peur;
        peur->next->pre = newcode;
        peur->next = newcode;
    }
//查早找
list* findlist(list* phead,SLDatetype x)
{
    assert(phead);
    list* node = phead->next;
    while (node!=phead)
    {
        if (node->data == x)
        {
            return node;
        }
        node = node->next;
    }
    return NULL;
}
//判断是否为空
bool emptylist(list* phead)
{
    assert(phead);
    return phead->next == phead;
}
//尾删
void ltpopback(list* phead)
{
    assert(!emptylist(phead));
    list* del = phead->pre;
    del->pre->next = phead;
    phead->pre = del->pre;
    free(del);
    del = NULL;
}
//头删
void ltpopfront(list* phead)
{
    assert(!emptylist(phead));
    list* del = phead->next;
    del->next->pre = phead;
    phead->next = del->next;
    free(del);
    del = NULL;
}
//在指定结点之后插入
void listinserback(list* pos, SLDatetype x)
{
    assert(pos);
    list* newcode =buynode(x);
    newcode->next = pos->next;
    newcode->pre = pos;
    pos->next->pre = newcode;
    pos->next = newcode;
}
//在指定结点前插入
void listinserfront(list* pos, SLDatetype x)
{
    assert(pos);
    list* newcode = buynode(x);
    newcode->next = pos;
    newcode->pre = pos->pre;
    pos->pre->next = newcode;
    pos->pre = newcode;
}
//删除指定数据
void dellist(list* pos)
{
    assert(pos);
    list* poss = pos;
    poss->next->pre = poss->pre;
    poss->pre->next = poss->next;
    free(poss);
    poss = NULL;
}
//链表销毁
void listdestory(list* phead)
{
    list* peur = phead->next;
    while (peur!=phead)
    {
        list* next = peur->next;
        free(peur);
        peur =next;
    }
    free(phead);
    phead = NULL;
}

test.c

#include"List.h"
void test()
{
    list* plist = chushihua();
    Insertback(plist, 1);
    Insertback(plist, 2);
    Insertback(plist, 3);
    Insertback(plist, 4);
    printlist(plist);
    Insertfront(plist, 1);
    Insertfront(plist, 2);
    Insertfront(plist, 3);
    Insertfront(plist, 4);
    printlist(plist);
    ltpopback(plist);
    ltpopfront(plist);
    ltpopfront(plist);
    ltpopfront(plist);
    ltpopfront(plist);
    printlist(plist); 
    list* newcode = findlist(plist, 1);
    if (newcode == NULL)
    {
        printf("没找到");
    }
    else
    {
        printf("找到了");
    }
    listinserback(newcode, 99);
    istinserfront(newcode, 99);
    dellist(newcode);
    printlist(plist);
    listdestory(plist);
    plist = NULL;
}

int main()
{
    test();
}

你可能感兴趣的:(数据结构,链表)