数据结构---单链表

SeqList.h对单链表进行操作的函数:

#include
#include
#include
#include

typedef int DataType;

typedef struct SeqList
{
    DataType _data;
    struct SeqList* _next;
}SeqList;

SeqList* BuyHead(DataType data);        //创建节点
void InitList(SeqList** ppHead);        //初始化链表
void PrintList(SeqList *pHead);         //打印链表
void PushBack(SeqList** ppHead, DataType data);//尾插
void PopBack(SeqList** ppHead);         //尾删
void PushFront(SeqList** ppHead, DataType data);//头插
void PopFront(SeqList** ppHead);        //头删
void FindList(SeqList* pHead,DataType data);//查找单链表
void PopList(SeqList** ppHead, SeqList* pos, DataType data);//任意位置插入
void ListErase(SeqList** ppHead,SeqList* pos);     //指定位置删除

SeqList.c 函数实现代码:

#include"SeqList.h"

//打印链表
void PrintList(SeqList *pHead)
{
    assert(pHead);
    while (pHead)
    {
        printf("%d-> ", pHead->_data);
        pHead = pHead->_next;
    }
    printf("NULL\n");
}

//创建链表
SeqList* BuyHead(DataType data)
{
    SeqList* pNewNode = (SeqList*)malloc(sizeof(SeqList));
    if (NULL == pNewNode)
    {
        assert(0);
        return NULL;
    }
    pNewNode->_data = data;
    pNewNode->_next = NULL;
    return pNewNode;
}

//初始化链表
void InitList(SeqList** ppHead)
{
    *ppHead = NULL;
}

//尾插
void PushBack(SeqList** ppHead, DataType data)
{
    if (NULL == *ppHead)
        *ppHead = BuyHead(data);
    else
    {
        SeqList *pNewHead = *ppHead;
        while (pNewHead->_next)
        {
            pNewHead = pNewHead->_next;
        }
        pNewHead->_next = BuyHead(data);
    }
}

//尾删
void PopBack(SeqList** ppHead)
{
    assert(*ppHead);
    SeqList* cur = *ppHead;
    SeqList* Tail = NULL;
    if (NULL == *ppHead)
        assert(0);
    else
    {

        while (cur->_next)
        {
            Tail = cur;
            cur = cur->_next;
        }
        free(cur);
        Tail->_next = NULL;
    }
}

//头插
void PushFront(SeqList** ppHead, DataType data)
{
    if (NULL == *ppHead)
        *ppHead = BuyHead(data);
    else
    {
        SeqList* cur = BuyHead(data);
        cur->_next = *ppHead;
        *ppHead = cur;
    }
}

//头删
void PopFront(SeqList** ppHead)
{
    assert(*ppHead);
    if (NULL == *ppHead)
        assert(0);
    else
    {
        SeqList* NewNode = *ppHead;
        *ppHead = (*ppHead)->_next;
        free(NewNode);
    }
}

//查找
void FindList(SeqList* pHead, DataType data)
{
    assert(pHead);
    while (pHead)
    {
        if (data == pHead->_data)
        {
            printf("找到了->%d\n", pHead->_data);
            break;
        }
        pHead = pHead->_next;
    }
    if (pHead == NULL)
        printf("NO!\n");
}

//任意位置插入
void PopList(SeqList** ppHead, SeqList* pos, DataType data)
{
    assert(*ppHead);
    SeqList* NewNode = *ppHead;
    SeqList* cur = NULL;
    if (pos==*ppHead)
        PushFront(*ppHead, data);
    else
    {
        while (NewNode->_next!=pos)
        {
            NewNode = NewNode->_next;
        }
        cur = BuyHead(data);
        NewNode->_next = cur;
        cur->_next = pos;
    }
}

//指定位置删除
void ListErase(SeqList** ppHead, SeqList* pos)
{
    assert(*ppHead);
    if (*ppHead == pos)
        PopFront(*ppHead);
    else
    {
        SeqList* cur = *ppHead;
        while (cur->_next != pos)
        {
            cur = cur->_next;
        }
        PopFront(&pos);
        cur->_next = pos;
    }
}

test.c 测试用例:

#include"SeqList.h"
int main()
{
    SeqList* pHead=NULL;
    InitList(&pHead);
//////////////////////////////////
    PushBack(&pHead,1);
    PushBack(&pHead,2);
    PushBack(&pHead,3);
    PushBack(&pHead,4);
    PushBack(&pHead,5);
    printf("尾插->\n");
    PrintList(pHead);
///////////////////////////////////
    printf("尾删->\n");
    PopBack(&pHead);
    PrintList(pHead);
///////////////////////////////////
    printf("头插->\n");
    PushFront(&pHead, 0);
    PrintList(pHead);
//////////////////////////////////
    printf("头删->\n");
    PopFront(&pHead);
    PrintList(pHead);
//////////////////////////////////
    printf("指定位置插入->\n");
    PopList(&pHead,pHead->_next, 6);
    PrintList(pHead);
//////////////////////////////////
    printf("指定位置删除->\n");
    ListErase(&pHead, pHead->_next);
    PrintList(pHead);
///////////////////////////////////
    system("pause");
    return 0;
}

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