无头单链表用C语言实现

typedef int DataType;

typedef struct Node
{
    struct Node *Next;
    DataType data;
}Node,*PNode;

这是链表的节点,由结构体组成,
第一个成员是结构体指针,能指向下一个节点,第二个成员存放的是数据
List.h

#include<stdio.h>
#pragma once
#include<assert.h>
#include<stdlib.h>


typedef int DataType;

typedef struct Node
{
    struct Node *Next;
    DataType data;
}Node,*PNode;


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

List.c

#include"List.h"


void SListInit(PNode* pHead)
{
    assert(pHead);
    *pHead = NULL;
}
PNode NewNode(DataType data)
{
    PNode pnewNode = (PNode)malloc(sizeof(Node));
    if (NULL == pnewNode)
        return NULL;
    pnewNode->data = data;
    pnewNode->Next = NULL;
    return pnewNode;
}
void SListPushBack(PNode* pHead, DataType data)
{
    assert(pHead);
    PNode pnewNode = NULL;
    pnewNode = NewNode(data);
    if (pnewNode == NULL)
        return;
    if (*pHead == NULL)
        *pHead = pnewNode;
    else
    {
        PNode pCur = *pHead;
        while (pCur->Next != NULL)
        {
            pCur = pCur->Next;
        }
        pCur->Next = pnewNode;
    }
}
void SListPopBack(PNode* pHead)
{
    assert(pHead);
    if (NULL == *pHead)
        return;
    else if ((*pHead)->Next == NULL)
    {
        free(*pHead);
        *pHead = NULL;
    }
    else
    {
        PNode pCur = *pHead;
        while (pCur->Next != NULL)
        {
            pCur = pCur->Next;
        }
        free(pCur);
        pCur = NULL;
    }
}
void SListPrint(PNode pHead)
{
    PNode pCur = NULL;
    if (pHead == NULL)
        return;
    pCur = pHead;
    while (pCur->Next)
    {
        printf("%d---->", pCur->data);
        pCur=pCur->Next;
    }
    printf("NULL\n");
}
void SListPushFront(PNode* pHead, DataType data)
{
    assert(pHead);
    PNode newNode = NULL;
    newNode = NewNode(data);
    if (newNode == NULL)
        return;
    newNode->Next = *pHead;
    *pHead = newNode;
}
//头删
void SListPopFront(PNode* pHead)
{
    PNode pCur = NULL;
    assert(pHead);
    if (*pHead == NULL)
    {
        return;
    }
    else
    {
        pCur = (*pHead);
        *pHead = (*pHead)->Next;
        free(pCur);
    }
}
//查找
PNode SListFind(PNode pHead, DataType data)
{
    PNode pCur = NULL;
    if (pHead == NULL)
        return NULL;
    pCur = pHead;
    while (pCur)
    {
        if (pCur->data == data)
            return pCur;
        pCur = pCur->Next;
    }
    return NULL;
}
//任意位置插入
void SListInsert(PNode* pHead, PNode pos, DataType data)
{
    PNode pnewnode = NULL;
    assert(pHead);
    if (*pHead == NULL || pos == NULL)
        return;
    pnewnode=NewNode(data);
    if (NULL == pnewnode)
        return;
    pnewnode->Next = pos->Next;
    pos->Next = pnewnode;
}
//任意位置删除
void SListErase(PNode* pHead, PNode pos)
{
    PNode newnode = NULL;
    assert(pHead);
    if (*pHead == NULL || pos == NULL)
        return;
    else if ((*pHead)==pos)
        SListPopFront(pHead);
    else
    {
        newnode = *pHead;
        while (newnode->Next != pos)
        {
            newnode = newnode->Next;
        }
        newnode->Next = pos->Next;
        free(pos);
    }
}
//求链表节点个数
int SListSize(PNode pHead)
{
    int count = 0;
    if (pHead == NULL)
        return 0;
    while (pHead)
    {
        count++;
        pHead = pHead->Next;
    }
    return count;
}
//判空
int SListEmpty(PNode pHead)
{
    return !pHead;
}
//销毁
void SListDestroy(PNode* pHead)
{
    PNode pnew = NULL;
    assert(pHead);
    if (NULL == (*pHead))
        return;
    while (pnew)
    {
        pnew = *pHead;
        *pHead = pnew->Next;
        free(pnew);
    }
}




void Test()
{
    PNode list;
    SListInit(&list);
    SListPushBack(&list, 1);
    SListPushBack(&list, 2);
    SListPushBack(&list, 3);
    SListPushBack(&list, 4);
    SListPushBack(&list, 5);
    SListPushBack(&list, 6);
    SListPopFront(&list);
    SListPushFront(&list, 1);
    SListFind(list, 1);
    //SListErase(&list, SListFind(list, 1));
    SListPrint(list);
    SListDestroy(&list);
}

test.c

#include"List.h"



int main()
{
    Test();
}
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页