【C++】单链表的增删查改实现


//main函数的实现
#define _CRT_SECURE_NO_WARNINGS 1
#include"test.h"

int main()
{
    //TestLinkList1();
    //TestLinkList2();
    //TestLinkList3();
    TestLinkList4();
    system("pause");
    return 0;
}


//test头文件
#include
#include
#include


typedef int DataType;

typedef struct ListNode
{
    DataType data;
    struct ListNode* next;
}ListNode;


//增删查改
void PrintList(ListNode* pList);
void PushBack(ListNode** ppList, DataType x);
void PopBack(ListNode** ppList);

void PushFront(ListNode** ppList, DataType x);
void PopFront(ListNode** ppList);
ListNode* Find(ListNode* pList, DataType x);

void Insert(ListNode** ppList,ListNode* pos, DataType x);
void Delete(ListNode** ppList, ListNode* pos);



//函数实现
#include"list.h"


ListNode* BuyNode(DataType x)
{
    ListNode* pNode=(ListNode*)malloc(sizeof(ListNode));
    if (pNode != NULL)
    {
        pNode->data = x;
        pNode->next = NULL;
    }
    return pNode;
}

void PrintList(ListNode* pList)
{
    assert(pList);
    ListNode* Node = pList;
    while (Node)
    {
        printf("%d ", Node->data);
        Node = Node->next;
    }
    printf("\n");
}


//尾插尾删
void PushBack(ListNode** ppList, DataType x)
{
    if (*ppList == NULL)
    {
        *ppList = BuyNode(x);
    }
    else
    {
        ListNode* list = *ppList;
        while (list->next != NULL)
        {
            list = list->next;
        }
        list->next = BuyNode(x);
    }
}



void PopBack(ListNode** ppList)
{

    ListNode* Node = *ppList;
    if (Node == NULL)//空链表
    {
        return;
    }
    else if (Node->next == NULL)//一个节点
    {
        free(Node);
        Node = NULL;
    }
    else     //一个以上节点
    {
        ListNode* prev = Node;
        while (Node->next != NULL)
        {
            prev = Node;
            Node = Node->next;
        }
        free(Node);
        Node = NULL;
        prev->next = NULL;
    }
}

//头插头删
void PushFront(ListNode** ppList, DataType x)
{

    if (*ppList == NULL)
    {
        *ppList = BuyNode(x);
    }
    else
    {
        ListNode* list = BuyNode(x);

        list->next = *ppList;
        *ppList = list;
    }
}


void PopFront(ListNode** ppList)
{
    assert(ppList);
    ListNode* list = *ppList;
    if (*ppList == NULL)
    {
        return;
    }
    else
    {
        *ppList = list->next;
        free(list);
        list = NULL;
    }
}


ListNode* Find(ListNode* pList, DataType x)
{
    assert(pList);
    while (pList->next != NULL)
    {
        if (pList->data == x)
        {
            return pList;
        }
        pList = pList->next;
    }
    return NULL;
}

//考虑头插尾插中间插
void Insert(ListNode** ppList, ListNode* pos, DataType x)
{
    assert(pos);
    if (pos == *ppList)
    {
        PushFront(ppList, x);
    }
    else
    {
        ListNode* tmp= BuyNode(x);
        ListNode* prev = *ppList;
        while (prev->next != pos)
        {
            prev = prev->next;
        }
        prev->next = tmp;
        tmp->next = pos;
    }
}


void Delete(ListNode** ppList, ListNode* pos)
{
    assert(pos);
    if (pos == *ppList)
    {
        PopBack(ppList);
    }
    else
    {
        ListNode* prev = *ppList;
        ListNode* cur = *ppList;
        while (prev->next != pos)
        {
            prev = prev->next;
        }
        prev->next = pos->next;
        free(pos);
    }
}

void TestLinkList1()
{
    ListNode* Node=NULL;

    PushBack(&Node, 1);
    PushBack(&Node, 2);
    PushBack(&Node, 3);
    PushBack(&Node, 4);
    PushBack(&Node, 5);

    PrintList(Node);

    PopBack(&Node);
    PopBack(&Node);

    PrintList(Node);
}

void TestLinkList2()
{
    ListNode* Node = NULL;

    PushFront(&Node, 1);
    PushFront(&Node, 2);
    PushFront(&Node, 3);
    PushFront(&Node, 4);
    PushFront(&Node, 5);


    PrintList(Node);

    PopFront(&Node);
    PopFront(&Node);
    PopFront(&Node);
    PopFront(&Node);

    PrintList(Node);
}


void TestLinkList3()
{
    ListNode* Node = NULL;
    PushBack(&Node, 2);
    PushBack(&Node, 3);
    PushBack(&Node, 1);
    PushBack(&Node, 4);
    PushBack(&Node, 1);
    PushBack(&Node, 8);
    PrintList(Node);
    printf("%s \n", Find(Node, 4));
}

void TestLinkList4()
{
    ListNode* Node = NULL;
    ListNode* pos ;

    PushBack(&Node, 2);
    PushBack(&Node, 3);
    PushBack(&Node, 1);
    PushBack(&Node, 4);
    PushBack(&Node, 1);
    PushBack(&Node, 8);
    PrintList(Node);

    pos = Node;
    pos = pos->next;

    Insert(&Node, pos, 4);

    PrintList(Node);

    Delete(&Node, pos);

    PrintList(Node);
}

你可能感兴趣的:(面向对象C++)