C语言实现单链表

头文件

#ifndef __LIST_H__
#define __LIST_H__
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef int DataType;
typedef struct Node//创建链表节点模型
{
    DataType data;
    struct Node *next;
}Node,*pNode,*pList;
void Initlist(pList *pplist);
pNode BuyNode(DataType data);
void PushBack(pList *pplist,DataType data);
void PushFront(pList *pplist, DataType data);
void Print(pList plist);
void Destroylist(pList* pplist);
void PopBack(pList* pplist,DataType data);
void PopFront(pList* pplist,DataType data);
DataTypeFind(pList* pplist, DataType data);
void Remove(pList*pplist, DataType data);
void RemoveAll(pList* pplist, DataType data);
void Insert(pList* pplist,pNode pos,DataType data);
void Erase(pList*pplist,pNode pos, DataType data);
#endif//__LIST_H__

测试文件

#define _CRT_SECURE_NO_WARNINGS 1
#include"list.h"
void test()
{
    pList plist;
    Initlist(&plist);
    PushBack(&plist, 1);
    Print(plist);
    Destroylist(&plist);
    Print(plist);
    PopBack(&plist, 1);
    Print(plist);
    PopFront(&plist, 2);
    Print(plist);
    find(&plist, 1);
    /*Insert(&plist,3)*/
    Remove(&plist, 1);
}
int main()
{
    test();
    system("pause");
    return 0;
}

函数实现

#define _CRT_SECURE_NO_WARNINGS 1
#include "list.h"
void Initlist(pList *pplist)
{
    assert(pplist != NULL);
        *pplist = NULL;
}
pNode BuyNode(DataType data)
{
    pNode pnode = NULL;
    pnode = malloc(sizeof(Node));
    if (pnode == NULL)
    {
        perror("malloc");
        return NULL;
    }
    else
    {
        pnode->data = data;
        pnode->next = NULL;
        return pnode;
    }
}
void PushBack(pList *pplist, DataType data)//尾插
{
    assert(pplist != NULL);
    pNode pnewnode = NULL;
    pnewnode = BuyNode(data);
    if (*pplist == NULL)//没有节点的情况
    {
        *pplist = pnewnode;
    }
    else
    {
        pNode cur = NULL;
        cur = *pplist;
        while (cur->next != NULL)
        {
            cur = cur->next;
        }
        cur->next = pnewnode;
    }
}
void Print(pList plist)
{
    if (plist == NULL)
    {
        printf("链表为空\n");
    }
    else
    {
        pList cur = NULL;
        cur = plist;
        printf("list:");
        while (cur != NULL)
        {
            printf("%d->", cur->data);
            cur = cur->next;
        }
        printf("NULL");
    }
}
void PushFront(pList *pplist, DataType data)//头插
{
    pNode pnewnode = NULL;
    assert(pplist!=NULL);
    pnewnode = BuyNode(data);
    if (*pplist == NULL)//没有节点的情况
    {
        *pplist = pnewnode;
        pnewnode->next = NULL;
    }
    else
    {
        pNode cur = *pplist;
        *pplist = pnewnode;
        pnewnode->next =cur;
    }
}
void Destroylist(pList* pplist)//销毁链表
{
    pNode cur = *pplist;
    assert(pplist != NULL);
    while (cur)
    {
        pNode del = cur;
        cur = cur->next;
        free(del);
    }
    *pplist = NULL;
}
void PopBack(pList* pplist, DataType data)//尾删
{

    assert(pplist != NULL);
    pNode cur = *pplist;
    if (cur == NULL)
    {
        printf("链表为空\n");
    }
    else if (cur->next == NULL)
    {
        free(cur);
        *pplist = NULL;
    }
    else
    {
        while (cur->next->next != NULL)
        {
            cur = cur->next;
        }
        free(cur->next->next);
        cur->next = NULL;
    }
}
void PopFront(pList* pplist, DataType data)//头删
{
    assert(pplist != NULL);
    pNode cur = NULL;
    cur = *pplist;
    if (cur == NULL)
    {
        printf("链表为空\n");
    }
    else if (cur->next == NULL)
    {
        free(cur);
        *pplist = NULL;
    }
    else
    {
        pNode cur = NULL;
        cur = *pplist;
        *pplist = cur->next;
        free(cur);
    }
}
DataType Find(pList* pplist, DataType data)//查找指定元素
{
    assert(pplist != NULL);
    pNode cur = *pplist;
    if (cur == NULL)
    {
        printf("链表为空\n");
    }
    else if (cur->next==NULL)
    {
        if (cur->data == data)
        {
            return cur->data;
        }
        else
        {
            return -1;
        }
    }
    else
    {
        while (cur)
        {
            if (cur->data == data)
            {
                return cur->data;
            }
            else
            {
                cur = cur->next;
            }
        }
        printf("查找元素不存在\n");
        return -1;
    }
}
void Insert(pList* pplist, DataType data)
{

}
void Remove(pList*pplist, DataType data)//删除指定元素
{
    assert(pplist != NULL);
    pNode cur = *pplist;
    if (cur == NULL)
    {
        printf("链表为空\n");
    }
    else if (cur->next == NULL)
    {
        if (cur->data == data)
        {
            free(cur);
            *pplist == NULL;
        }
        else
        {
            printf("指定删除的元素不存在\n");
        }
    }
    else                                     //>=2节点
    {
        pNode prev = NULL;
        while (cur)
        {
            if (cur->data == data)
            {
                if (cur==*pplist)   //如果删除的是有多个节点的链表的第一个节点
                {
                    *pplist = cur->next;
                    free(cur);
                }
                else               //删除的不是第一个节点,利用prev始终保存当前cur的前一个节点,在释放当前节点之前让当前节点的前一个节点的指针指向当前节点的后一个节点即cur->next,然后再释放当前节点cur,
                {
                    prev->next = cur->next;
                    free(cur);
                }
            }
            else
            {
                prev = cur;
                cur = cur->next;
            }
        }

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值