c语言实现的双向循环链表

1 c语言实现的双向循环链表

typedef struct DulNode
{
    struct  DulNode * perNode;
    int data;
    struct DulNode * nextNode;

}DulNode,*PSDulNode;

typedef struct dualLink
{
    PSDulNode headNode;
    PSDulNode tailNode;


}DualLick,*PLDualLink;

PLDualLink  InitDulList()
{
    PLDualLink  dl = (PLDualLink)malloc(sizeof(DualLick));
    if(dl->tailNode == NULL)
    {
        printf("init false");
        exit(-1);
    }
    dl->headNode = dl->tailNode = NULL;
    return dl;
}
int emptyDulList(PLDualLink dl)
{
    if(dl->headNode != NULL&&dl->tailNode != NULL)
    {
        return false;
    }
    else
    {
        return true;
    }
}
int lengthDulList(PLDualLink dl)
{
    if(emptyDulList(dl))
    {
        return 0;
    }
    int i = 1;
    PSDulNode headNode = dl->headNode;

    while(headNode->nextNode != dl->headNode)
    {
        i++;
        headNode = headNode->nextNode;
    }
    return i;
}
int getElent(PLDualLink dl,PSDulNode node,int position)//position 从0开始
{

    node = dl->headNode;

    for (int i = 0; i<position; i++) {

        node = node->nextNode;
    }
    return 1;
}
PSDulNode getNodeElemnt(PLDualLink dl,int position)
{

    int length = lengthDulList(dl);

    position = position>length?length:position;


    PSDulNode node = dl->headNode;

    for (int i = 0; i<position; i++) {

        node = node->nextNode;
    }


    return node;
}
int  insertElemt(PLDualLink dl,int a,int position)//position 从0开始算起
{
    int length = lengthDulList(dl);
    PSDulNode newNode =  (PSDulNode)malloc(sizeof(DulNode));
    newNode->data = a;
    if(dl->tailNode==NULL||dl->headNode == NULL)//空链表
    {
        dl->tailNode  = dl->headNode = newNode;
        dl->tailNode->nextNode = newNode;
        dl->tailNode->perNode = newNode;
        dl->headNode->nextNode = newNode;
        dl->headNode->perNode = newNode;

    }
    else
    {

        if(position == 0)
        {
            PSDulNode p = dl->headNode;
            newNode->nextNode = dl->headNode;
            newNode->perNode = dl->tailNode;
            p->perNode = newNode;
            dl->tailNode->nextNode = newNode;
            dl->headNode = newNode;

        }
        else if (position >= length)
        {
            PSDulNode p = dl->tailNode;
            newNode->nextNode = dl->headNode;
            newNode->perNode = p;
            p->nextNode = newNode;
            dl->headNode->perNode = newNode;
            dl->tailNode = newNode;

        }
        else
        {

            PSDulNode p = getNodeElemnt(dl,position);
            newNode->nextNode = p;
            newNode->perNode = p->perNode;
            p->perNode->nextNode = newNode;
            p->perNode = newNode;
        }
    }
    return 1;
}
int  appendElemt(PLDualLink dl,int a)//position 从0开始算起、
{
    int length = lengthDulList(dl);

    insertElemt(dl, a, length);

    return 1;
}
int delElemt(PLDualLink dl,int positon)
{
    int length = lengthDulList(dl);
    if(positon<0||positon>length)
    {
        printf("del error");
        return 0;
    }

    if(positon == 0)
    {
        PSDulNode node = dl->headNode;
        node->nextNode->perNode = dl->tailNode;
        dl->tailNode->nextNode = node->nextNode;
        dl->headNode = node->nextNode;
        free(node);

        return 1;
    }
    else if(positon == length-1)
    {
        PSDulNode node = dl->tailNode;
        node->perNode->nextNode = dl->headNode;
        dl->headNode->perNode = node->perNode;
        dl->tailNode = node->perNode;
        free(node);
        return 1;
    }
    else
    {
        PSDulNode node = dl->headNode;

        for (int i = 0; i<positon; i++) {
            node = node->nextNode;
        }

        node->perNode->nextNode = node->nextNode;
        node->nextNode->perNode = node->perNode;

        free(node);

        return 1;

    }

}
int modify(PLDualLink dl,int position,int a)
{
    PSDulNode node =   getNodeElemnt(dl, position);
    node->data = a;
    return 1;
}
void TrasverDulList(PLDualLink dl)
{
    if(emptyDulList(dl))
    {
        exit(-1);
    }

    PSDulNode p = dl->headNode;

    do
    {
        printf("%d\n",p->data);
        p = p->nextNode;
    } while (p != dl->headNode); //不成立停止


}
main
{
     PLDualLink dl = InitDulList();

    NSLog(@"%d",emptyDulList(dl));

    insertElemt(dl, 100, 0);

    TrasverDulList(dl);

    insertElemt(dl, 200, 1);

    TrasverDulList(dl);


    insertElemt(dl, 300, 1);

    TrasverDulList(dl);

    NSLog(@"%d",lengthDulList(dl));

    appendElemt(dl, 400);

    for (int  i =0 ; i<10; i++) {
        appendElemt(dl, i+999);
    }

    delElemt(dl,lengthDulList(dl)-1);

    NSLog(@"%d",getNodeElemnt(dl, 1)->data);

    modify(dl,0,98765);

    TrasverDulList(dl);


}

三四年的老程序员了,写一个简单的链表 写了一天 惭愧,惭愧,有什么不足的地方 请指正

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