C语言实现单链表

来源:互联网 发布:泰州网络教育学历报名 编辑:程序博客网 时间:2024/04/29 03:12

头文件

#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