链表的基本操作

链表的常见面试题
头文件声明
//SList.h

#pragma once

#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <windows.h>

typedef int DataType;
typedef struct Node
{
    struct Node * _pNext;
    DataType _data;
}Node, *PNode;



void SListInit(PNode * pHead);                      //初始化
void SListPushBlack(PNode* pHead, DataType data);       //尾插
void SListPopBlack(PNode *pHead);                       //尾删
void SListPushFront(PNode *pHead, DataType data);       //头插
void SListPopFront(PNode *pHead);                       //头删
void SListInsert(PNode pHead,PNode pos, DataType data); //在链表pos位置后插入值为data的新节点  
void SListErase(PNode* pHead, PNode pos);               //删除链表pos位置上的结点 
PNode SListFind(PNode pHead, DataType data);            // 在链表中查找值为data的元素,找到后返回值为data的结点
int SListSize(PNode pHead);                         // 获取链表中值data的结点的个数
int SListEmpty(PNode pHead,DataType data);                          // 判断链表是否为空
void SListDestroy(PNode* pHead);                        // 销毁聊表

void SListPrint(PNode pHead);                               //打印
PNode BuySListNode(DataType Data);                          //分配空间


void TestPush();
void test();

函数实现
//SList.c

#include "SList.h"

//初始化
void SListInit(PNode *pHead)
{
    assert(pHead);
    *pHead = NULL;
}

//尾插
void SListPushBlack(PNode *pHead, DataType data)
{
    PNode pCur = *pHead;
    assert(pHead);
    if (NULL == *pHead)
    {
        *pHead = BuySListNode(data);  //如果链表为空,则直接插入
    }
    else
    {
        while (pCur->_pNext)        //如果不为空,则找到最后一个节点
        {
            pCur = pCur->_pNext;
        }
        pCur->_pNext = BuySListNode(data);

    }

}


//尾删
void SListPopBlack(PNode *pHead)
{   
    PNode pCur = *pHead;
    PNode pTail = NULL;
    assert(pHead);
    //链表为空
    if (NULL == *pHead)             
    {
        return;
    }
    //只有一个结点
    else if (NULL == (*pHead)->_pNext)  
    {
        free(*pHead);
        *pHead = NULL;
    }
    //多个结点
    else
    {
        while (pCur->_pNext)
        {
            pTail = pCur;
            pCur = pCur->_pNext;
        }
        free(pCur);
        pTail->_pNext = NULL;
    }

}



//头插
void SListPushFront(PNode *pHead, DataType data)
{
    assert(pHead);
    PNode pNewNode = BuySListNode(data);

    pNewNode->_pNext = (*pHead);
    *pHead = pNewNode;
}


//头删
void SListPopFront(PNode *pHead)
{   
    PNode pDelNode = NULL;
    assert(pHead);
    if (NULL == *pHead)
    {
        return;
    }
    else
    {
        pDelNode = *pHead;
        *pHead = (*pHead)->_pNext;
        free(pDelNode);
        pDelNode = NULL;
    }

}


//在链表pos位置后插入值为data的新节点 
void SListInsert(PNode pHead, PNode pos, DataType data)
{
    PNode pNewNode = NULL;
    if (NULL == pHead||pos == NULL)
    {
        printf("链表不存在!\n");
        return;
    }
    else
    {
        pNewNode = BuySListNode(data);
        pNewNode->_pNext = pos->_pNext;
        pos->_pNext = pNewNode;
    }
}

//删除链表pos位置上的结点 
void SListErase(PNode *pHead, PNode pos)
{

    PNode pNext = NULL;
    assert(pHead);
    if (NULL == *pHead||NULL == pos)
    {
        printf("输入有误!\n");
        return;
    }
    else
    {
        pNext = pos->_pNext;
        pos->_pNext = pNext->_pNext;
        pos->_data = pNext->_data;
        free(pNext);
        pNext->_pNext = NULL;
    }

}


// 获取链表中值data的结点的个数
int SListSize(PNode pHead,DataType data)
{
    PNode pCur = pHead;
    int count = 0;

    if (NULL == pHead)
    {
        printf("链表为空!\n");
    }
    while (pCur)
    {
        if (pCur->_data == data)
        {
            count++;
        }
        pCur = pCur->_pNext;
    }
    return count;
}

// 判断链表是否为空
int SListEmpty(PNode pHead)
{
    if (NULL == pHead)
    {
        return 0;
    }
    else
    {
        return 1;
    }
}


// 销毁聊表
void SListDestroy(PNode* pHead)
{
    PNode pCur = pHead;
    PNode pNext = NULL;
    assert(pHead);
    if (NULL == *pHead)
    {
        return;
    }
    else
    {
        pCur = *pHead;
        *pHead = NULL;
        while (pCur)
        {
            pNext = pCur->_pNext;
            free(pCur);
            pCur = pNext;
        }
    }
}

//构建新结点
PNode BuySListNode(DataType data)
{
    PNode pNewNode = (PNode)malloc(sizeof(Node));

    if (NULL == pNewNode)
    {
        perror("pNewNode");
        exit(EXIT_FAILURE);
    }

    pNewNode->_data = data;
    pNewNode->_pNext= NULL;

    return pNewNode;
}

//打印
void SListPrint(PNode pHead)
{
    PNode pCur = pHead;
    if (NULL == pHead)
    {
        printf("链表为空!!\n");
        return;
    }
    while (pCur)
    {
        printf("%d->", pCur->_data);
        pCur = pCur->_pNext;
    }
    printf("NULL\n");
}


//查找pos位置节点
PNode SListFind(PNode pHead, DataType data)
{
    PNode pCur = pHead;
    if (NULL == pHead)
    {
        printf("链表为空!\n");
        return NULL;
    }
    while (pCur)
    {
        pCur = pCur->_pNext;
        if (pCur->_data == data)
        {
            return pCur;
        }
    }

    return NULL;
}


//测试
void test()
{
    PNode pHead,pCur;

    SListInit(&pHead);
    SListPushBlack(&pHead, 1);
    SListPushBlack(&pHead, 3);
    SListPushBlack(&pHead, 6);
    //SListPushBlack(&pHead, 2);
    //SListPushBlack(&pHead, 2);
    //SListPushBlack(&pHead, 2);
    SListPrint(pHead);
    pCur = SListFind(pHead, 3);
    SListInsert(pHead, pCur, 9);
    SListPrint(pHead);
    pCur = SListFind(pHead, 6);
    SListErase(&pHead, pCur);
    SListPrint(pHead);
    int ret = SListSize(pHead,2);
    SListDestroy(&pHead);
    SListEmpty(pHead);
    SListPrint(pHead);

}

//测试头尾插
void TestPush()
{
    PNode pHead;

    SListInit(&pHead);
    SListPushBlack(&pHead, 1);
    SListPushBlack(&pHead, 3);
    SListPushBlack(&pHead, 6);
    SListPushBlack(&pHead, 2);
    SListPrint(pHead);
    //SListPopBlack(&pHead);
    //SListPushFront(&pHead,9);
    //SListPrint(pHead);
    //SListPopFront(&pHead);
    //SListPrint(pHead);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值