数据结构DAY 5【双向链表】

双向链表:

DS4DList2head.h:

#ifndef _DLINKLIST_
#define _DLINKLIST_ 1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef int ElemType;
typedef struct DLNode
{
    union
    {
        int length;
        ElemType data;
    };
    struct DLNode *prior;
    struct DLNode *next;
}*DLinkList, DLNode;
//=========================================================================
// 双向链表头结点创建
DLinkList DListCreatHead(void);
// 双向链表其它结点创建
DLNode *DListCreatNode(void);
// 双向链表初始化
DLinkList DListInit(int mode);
// 双向链表的遍历
int DListOutPut(DLinkList pHead);
// 双向链表的释放
DLNode *DListFree(DLinkList pHead);
//=========================================================================
// 双向链表头插
int DListInsertHead(DLinkList pHead, DLNode *pNode);
// 双向链表尾插
int DListInsertTail(DLinkList pHead, DLNode *pNode);
//=========================================================================
// 双向链表头删
int DListDleteHead(DLinkList pHead);
// 双向链表尾删
int DListDleteTail(DLinkList pHead);
//=========================================================================
// 位置输入函数[复用]
int GetIndex(void);
// 元素输入函数[复用]
ElemType GetElem(void);

// 双向链表按位置插入
int DListInsertIndex(DLinkList pHead, int index, ElemType elem);
// 双向链表按位置删除
int DListDeleteIndex(DLinkList pHead, int index);
// 双向链表按位置修改
int DListReviseIndex(DLinkList pHead, int index, ElemType elem);
// 双向链表按位置查找
DLNode *DListSearchIndex(DLinkList pHead, int index);
#endif

DS4DList2.c:

#include "DS4DList2head.h"
int main(int argc, char const *argv[])
{
    #if 1
    {
        DLinkList pHead = DListInit(1);
        #if 0
        {
            // 双向链表头删
            DListDleteHead(pHead);
        }
        #endif
        #if 0
        {
            // 双向链表尾删
            DListDleteTail(pHead);
        }
        #endif
        #if 0
        {
            // input 
            int index = GetIndex();
            ElemType elem = GetElem();

            // 双向链表按位置插入
            DListInsertIndex(pHead, index, elem);
        }
        #endif
        #if 0
        {
            // input 
            int index = GetIndex();

            // 双向链表按位置删除
            DListDeleteIndex(pHead, index);
        }
        #endif
        #if 0
        {
            // input 
            int index = GetIndex();
            ElemType elem = GetElem();

            // 双向链表按位置修改
            DListReviseIndex(pHead, index, elem);
        }
        #endif
        #if 0
        {
            // input 
            int index = GetIndex();

            // 双向链表按位置查找
            DLNode *ret = DListSearchIndex(pHead, index);
            printf("\nFind index %d's value : %d\n", index, ret->data);
        }
        #endif
        #if 1
        {
            
        }
        #endif
        DListOutPut(pHead);
        pHead = DListFree(pHead);
    }
    #endif
    return 0;
}

DS4DList2func.c:

#include "DS4DList2head.h"
//=========================================================================
// 双向链表头结点创建
// 双向链表其它结点创建
// 双向链表初始化
// 双向链表的遍历
// 双向链表的释放
DLinkList DListCreatHead(void)
{
    DLinkList pHead = (DLinkList)malloc(sizeof(DLNode));
    if(NULL == pHead)
    {
        printf("DListCreatHead : Malloc failed exit.\n");
        return NULL;
    }
    pHead->length = 0;
    pHead->prior = NULL;
    pHead->next = NULL;
    return pHead;
}
DLNode *DListCreatNode(void)
{
    DLNode *pNode = (DLNode *)malloc(sizeof(DLNode));
    if(NULL == pNode)
    {
        printf("DListCreatNode : Malloc failed exit.\n");
        return NULL;
    }
    ElemType elem = 0;
    printf("Please input elem : ");
    scanf("%d", &elem);
    pNode->data = elem;
    pNode->prior = NULL;
    pNode->next = NULL;
    return pNode;
}
DLinkList DListInit(int mode)
{
    DLinkList pHead = DListCreatHead();
    if(NULL == pHead)
    {
        printf("DListInit : Head illegal exit.\n");
        return NULL;
    }
    int len = 0;
    printf("Please set the len : ");
    Circle : 
    scanf("%d", &len);
    if(len < 0)
    {
        goto Circle;
    }
    for(int i = 0; i < len; i++)
    {
        printf("Init Node[%d] : \n", i+1);
        DLNode *pNode = DListCreatNode();
        switch (mode)
        {
            case 1 :
                DListInsertTail(pHead, pNode);
                break;
            default:
                DListInsertHead(pHead, pNode);
                break;
        }
    }
    return pHead;
}
int DListOutPut(DLinkList pHead)
{
    if(NULL == pHead)
    {
        printf("DListOutPut : Head illegal exit.\n");
        return -1;
    }
    if(0 == pHead->length)
    {
        printf("DListOutPut : Length zero exit.\n");
        return -2;
    }
    printf("\nDListOutPut : \n");
    DLNode *pFlag = pHead->next;
    for(int i = 0; i < pHead->length; i++, pFlag=pFlag->next)
    {
        printf("Node[%d] : %d\n", i+1, pFlag->data);
    }
    return 0;
}
DLNode *DListFree(DLinkList pHead)
{
    if(NULL == pHead)
    {
        printf("DListFree : Head illegal exit.\n");
        return NULL;
    }
    int len = pHead->length;
    for(int i = 0; i < len; i++)
    {
        DListDleteHead(pHead);
    }
    // free
    free(pHead);
    pHead = NULL;
    return pHead;
}
//=========================================================================
// 双向链表头插
// 双向链表尾插
int DListInsertHead(DLinkList pHead, DLNode *pNode)
{                              
    if(NULL == pHead)
    {
        printf("DListInsertHead : Head illegal exit.\n");
        return -1;
    }
    if(NULL == pNode)
    {
        printf("DListInsertHead : Node illegal exit.\n");
        return -2;
    }
    // insert 4+1
    pNode->prior = pHead;
    pNode->next = pHead->next;
    if(NULL != pHead->next)
    {
        pNode->next->prior = pNode;
    }
    pHead->next = pNode;
    pHead->length++;
    printf("test : %d, %d", pHead->length, pHead->next->data);
    return 0;
}
int DListInsertTail(DLinkList pHead, DLNode *pNode)
{
    if(NULL == pHead)
    {
        printf("DListInsertHead : Head illegal exit.\n");
        return -1;
    }
    if(NULL == pNode)
    {
        printf("DListInsertHead : Node illegal exit.\n");
        return -2;
    }
    // find tail
    DLNode *pTail = pHead;
    for(int i = 0; i < pHead->length; i++, pTail=pTail->next);
    // insert 3+1
    pNode->next = NULL;
    pNode->prior = pTail;
    pTail->next = pNode;
    pHead->length++;
    return 0;
}
//=========================================================================
// 双向链表头删
// 双向链表尾删
int DListDleteHead(DLinkList pHead)
{
    if(NULL == pHead)
    {
        printf("DListDleteHead : Head illegal exit.\n");
        return -1;
    }
    if(0 == pHead->length)
    {
        printf("DListDleteHead : Length zero exit.\n");
        return -2;
    }
    DLNode *pDel = pHead->next;
    // delete
    pHead->next = pDel->next;
    if(NULL != pDel->next)
    {
        pDel->next->prior = pHead;
    }
    pHead->length--;
    // free
    free(pDel);
    pDel = NULL;
}
int DListDleteTail(DLinkList pHead)
{
    if(NULL == pHead)
    {
        printf("DListDleteTail : Head illegal exit.\n");
        return -1;
    }
    if(0 == pHead->length)
    {
        printf("DListDleteTail : Length zero exit.\n");
        return -2;
    }
    DLNode *pTailPrior = pHead;
    // find tail
    for(int i = 1; i < pHead->length; i++, pTailPrior=pTailPrior->next);
    // free
    free(pTailPrior->next);
    // delete
    pTailPrior->next = NULL;
    pHead->length--;
}
//=========================================================================
// 双向链表按位置插入
// 双向链表按位置删除
// 双向链表按位置修改
// 双向链表按位置查找

// 位置输入函数[复用]
int GetIndex(void)
{
    int index = 1;
    printf("GetIndex : Please input index[>=1] : ");
    Circle : 
    scanf("%d", &index);
    if(index < 1)
    {
        goto Circle;
    }
    return index;
}
// 元素输入函数[复用]
ElemType GetElem(void)
{
    ElemType elem = 0;
    printf("Please input your elem : ");
    scanf("%d", &elem);
    return elem;
}

int DListInsertIndex(DLinkList pHead, int index, ElemType elem)
{
    if(NULL == pHead)
    {
        printf("DListInsertIndex : Head illegal exit.\n");
        return -1;
    }
    if(index < 1 || index > pHead->length+1)// [1, len+1]
    {
        printf("DListInsertIndex : Index illegal exit.\n");
        return -2;
    }
    DLNode *pIndexPrior = pHead;
    // find index->prior
    for(int i = 1; i < index; i++, pIndexPrior=pIndexPrior->next);
    // insert
    DLNode *pInsert = (DLNode *)malloc(sizeof(DLNode));
    pInsert->data = elem;
    pInsert->next = pIndexPrior->next;
    pInsert->prior = pIndexPrior;
    pIndexPrior->next = pInsert;
    if(NULL != pInsert->next)
    {
        pInsert->next->prior = pInsert;
    }
    pHead->length++;
    return 0;
}
int DListDeleteIndex(DLinkList pHead, int index)
{
    if(NULL == pHead)
    {
        printf("DListDeleteIndex : Head illegal exit.\n");
        return -1;
    }
    if(0 == pHead->length)
    {
        printf("DListDeleteIndex : Length zero exit.\n");
        return -2;
    }
    if(index < 1 || index > pHead->length)// [1, len]
    {
        printf("DListDeleteIndex : Index illegal exit.\n");
        return -3;
    }
    DLNode *pDeletePrior = pHead;
    // find delete-prior
    for(int i = 1; i < index; i++, pDeletePrior=pDeletePrior->next);
    // delete
    DLNode *pDelete = pDeletePrior->next;
    pDeletePrior->next = pDelete->next;
    if(NULL != pDelete->next)
    {
        pDelete->next->prior = pDeletePrior;
    }
    pHead->length--;
    // free
    free(pDelete);
    pDelete = NULL;
}
int DListReviseIndex(DLinkList pHead, int index, ElemType elem)
{
    if(NULL == pHead)
    {
        printf("DListReviseIndex : Head illegal exit.\n");
        return -1;
    }
    if(0 == pHead->length)
    {
        printf("DListReviseIndex : Length zero exit.\n");
        return -2;
    }
    if(index < 1 || index > pHead->length)// [1, len]
    {
        printf("DListReviseIndex : Index illegal exit.\n");
        return -3;
    }
    DLNode *pRevise = pHead->next;
    // find revise
    for(int i = 1; i < index; i++, pRevise=pRevise->next);
    // revise
    pRevise->data = elem;
    return 0;
}
DLNode *DListSearchIndex(DLinkList pHead, int index)
{
    if(NULL == pHead)
    {
        printf("DListSearchIndex : Head illegal exit.\n");
        return NULL;
    }
    if(0 == pHead->length)
    {
        printf("DListSearchIndex : Length zero exit.\n");
        return NULL;
    }
    if(index < 1 || index > pHead->length)// [1, len]
    {
        printf("DListSearchIndex : Index illegal exit.\n");
        return NULL;
    }
    DLNode *pIndex = pHead->next;
    // find index
    for(int i = 1; i < index; i++, pIndex=pIndex->next);
    // revise
    return pIndex;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值