不带头结点的单链表操作


typedef int DataType;
typedef struct ListNode
{
    DataType data;
    struct ListNode *pNext;
}SL

所有操作:

#define _CRT_SECURE_NO_WARININGS 1

#include <stdio.h>
#include <assert.h>
#include <malloc.h>
#include "SList.h"

PSListNode BuyNode(PSListNode *p)
{
    (*p) = (PSListNode*)malloc(sizeof(SListNode));
    if ((*p) == NULL)
    {
        printf("内存分配失败\n");
        return NULL;
    }
    return *p;
}

// 初始化单链表(对于无头结点单链表,该函数没有意义)
void InitList(PSListNode* pHead)
{
    (*pHead) = NULL;
}

// 销毁单链表
void DestroyList(PSListNode* pHead)
{
    PSListNode pTemp;
    assert(pHead);
    if (NULL == *pHead)
    {
        return;
    }
    while ((*pHead) != NULL)
    {
        pTemp = *pHead;
        *pHead = (*pHead)->pNext;
        free(pTemp);
    }
    (*pHead) = NULL;
}

// 尾插
void PushBack(PSListNode* pHead, DataType data)
{
    PSListNode pTemp,pNew;
    pTemp = *pHead;
    assert(pHead);
    if (NULL == *pHead)
    {
        pTemp = BuyNode(&pTemp);
        if (pTemp == NULL)
        {
            return;
        }
        pTemp->data = data;
        pTemp->pNext = NULL;
        *pHead = pTemp;
    }
    else
    {
        while (pTemp->pNext != NULL)
        {
            pTemp = pTemp->pNext;
        }
        pNew = BuyNode(&pNew);
        if (pNew == NULL)
        {
            return;
        }
        pNew->data = data;
        pNew->pNext = NULL;
        pTemp->pNext = pNew;
    }
}

// 尾出
void PopBack(PSListNode* pHead)
{
    PSListNode pTemp,pPre;
    assert(pHead);
    pTemp = *pHead;
    pPre = *pHead;
    if (NULL == *pHead)
    {
        printf("链表为空,不能尾出\n");
        return;
    }
    else if ((*pHead)->pNext == NULL)
    {
        free(*pHead);
        *pHead = NULL;
    }
    else
    {
        while (pTemp->pNext != NULL)
        {
            pPre = pTemp;
            pTemp = pTemp->pNext;
        }
        pPre->pNext = NULL;
        free(pTemp);
    }
}

// 头插
void PushFront(PSListNode* pHead, DataType data)
{
    PSListNode pTemp,pNew;
    assert(pHead);
    pTemp = *pHead;
    if (NULL == *pHead)
    {
        pTemp = BuyNode(&pTemp);
        if (pTemp == NULL)
        {
            return;
        }
        pTemp->data = data;
        pTemp->pNext = NULL;
        *pHead = pTemp;
    }
    else
    {
        pNew = BuyNode(&pNew);
        if (pNew == NULL)
        {
            return;
        }
        pNew->data = data;
        pNew->pNext = pTemp;
        *pHead = pNew;
    }
}

// 头出
void PopFront(PSListNode* pHead)
{
    PSListNode pTemp;
    assert(pHead);
    pTemp = *pHead;
    if (NULL == *pHead)
    {
        printf("链表为空,不能头出\n");
        return;
    }
    else if ((*pHead)->pNext == NULL)
    {
        free(*pHead);
        (*pHead) = NULL;
    }
    else
    {
        (*pHead) = (*pHead)->pNext;
        free(pTemp);
    }
}

// 在链表中查找元素data
PSListNode Find(PSListNode pHead, DataType data)
{
    PSListNode pTemp;
    pTemp = pHead;
    if (NULL == pHead)
    {
        printf("链表为空\n");
        return NULL;
    }
    else
    {
        while (pTemp != NULL && pTemp->data != data)
        {
            pTemp = pTemp->pNext;
        }
        if (pTemp != NULL)
        return pTemp;
    }
    printf("没有找到\n");
    return NULL;
}

// 删除pos位置的结点(注意不能用那种替换形式)
void  Erase(PSListNode* pHead, PSListNode pos)
{
    PSListNode pTemp, p;
    assert(pHead);
    assert(NULL != pos);
    pTemp = *pHead;
    p = *pHead;
    if (NULL == *pHead)
    {
        printf("链表为空,删除失败\n");
        return;
    }
    else if (pTemp->pNext == NULL)
    {
        free(*pHead);
        (*pHead) = NULL;
    }
    else 
    {
        while ( pTemp->pNext != pos)
        {
            pTemp = pTemp->pNext;
        }
        p = pTemp;
        p->pNext = pos->pNext;
        free(pos);
    }
}

// 在链表的pos位置插入元素data
void  Insert(PSListNode* pHead, PSListNode pos, DataType data)
{
    PSListNode pTemp,q;
    assert(pHead);
    assert(NULL != pos);
    pTemp = *pHead;
    q = BuyNode(&q);
    if (q == NULL)
    {
        return;
    }
    q->data = data;
    q->pNext = pos->pNext;
    pos->pNext = q;
}

void PrintList(PSListNode pHead)
{
    PSListNode pTemp;
    pTemp = pHead;
    if (NULL == pHead)
    {
        printf("链表为空\n");
        return;
    }
    while (pTemp != NULL)
    {
        printf("%d\t", pTemp->data);
        pTemp = pTemp->pNext;
    }
    printf("\n");
}

/////////////////////////////////////////////////

// 从尾至头打印单链表
void PrintListTailToHead(PSListNode pHead)
{
    PSListNode pTemp;
    pTemp = pHead;
    if (NULL == pTemp)
    {
        printf("链表为空\n");
        return;
    }
    if (pTemp != NULL  && pTemp->pNext != NULL)
    {
        PrintListTailToHead(pTemp->pNext);
    }
        printf("%d\t", pTemp->data);
}

// 链表的非头结点前插入元素data
void InsertNotHead(PSListNode pos, DataType data)
{
    PSListNode pTemp;
    pTemp = BuyNode(&pTemp);
    if (pTemp == NULL)
    {
        return;
    }
    pTemp->data = pos->data;
    pTemp->pNext = pos->pNext;
    pos->data = data;
    pos->pNext = pTemp;
}

// 删除链表的非尾结点
void DelNotTailNode(PSListNode pos)
{
    PSListNode p;
    p = pos->pNext;
    pos->data = p->data;
    pos->pNext = p->pNext;
    free(p);
}


// 查找链表的中间结点,要求只遍历一次链表
PSListNode FindMidNode(PSListNode pHead)
{
    PSListNode Fast, Slow;
    Fast = pHead;
    Slow = pHead;
    if (NULL == pHead)
    {
        printf("链表为空\n");
        return NULL;
    }
    while (Fast->pNext != NULL)
    {
        Slow = Slow->pNext;
        Fast = Fast->pNext;
        if (Fast->pNext == NULL)
        {
            return Slow;
        }
        else
        {
            Fast = Fast->pNext;
        }
    }
    return Slow;
}

你可能感兴趣的:(单链表)