C语言实现单链表

c语言实现单链表

list.h
#define _CRT_SECURE_NO_WARNINGS 1
#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);
void PushBack(pList* pplist, DataType d);
void PopBack(pList* pplist);
void PushFront(pList* pplist, DataType d);
void PopFront(pList* pplist);
void PrintList(pList plist);
pNode Find(pList plist, DataType d);
void Remove(pList* pplist, DataType d);
void RemoveAll(pList* pplist, DataType d);
void Insert(pList* pplist,pNode pos, DataType d);
void Erase(pList* pplist, pNode pos);
void ReverseList(pList* pplist);
void BubbleSort(pList* pplist, pList plist);
int get_length(pList* pplist);
void DestroyList(pList* pplist);


#endif


list.c

}

pNode BuyNode(DataType d)
{
    pNode newnode = (pNode)malloc(sizeof(Node));
    newnode->data = d;
    newnode->next = NULL;
    return newnode;
}

void PushBack(pList* pplist, DataType d)
{
    pNode newnode = BuyNode(d);
    pNode cur = *pplist;
    assert(pplist);
    if (*pplist = NULL)
    {
        *pplist = newnode;
        return;
    }
    while (cur->next != NULL)
    {
        cur = cur->next;
    }
    cur->next = newnode;
}
void PopBack(pList* pplist)
{
    pNode cur = *pplist;
    assert(pplist);
    if (*pplist == NULL)
        return;
    if (((*pplist)->next) == NULL)
    {
        free(*pplist);
        *pplist = NULL;
        return;
    }
    while ((cur->next->next) != NULL)
    {
        cur = cur->next;
    }
    free(cur->next);
    cur->next = NULL;
}

void PushFront(pList* pplist, DataType d)
{
    pNode cur = *pplist;
    pNode newnode = BuyNode(d);
    assert(pplist);

    if (cur == NULL)
    {
        *pplist = newnode;
    }
    else
    {
        newnode->next = cur;
        *pplist = newnode;
    }
}
void PopFront(pList* pplist)
{
    pNode cur = *pplist;
    assert(pplist);
    if (*pplist == NULL)
        return;
    if (((*pplist)->next) == NULL)
    {
        free(*pplist);
        *pplist = NULL;
        return;
    }
    else
    {
        *pplist = cur->next;
        free(cur);
    }
}
void DestroyList(pList* pplist)
{
    pNode cur = *pplist;
    while (cur)
    {
        pNode del = cur;
        cur = cur->next;
        free(del);
        del = NULL;
    }
    *pplist = NULL;
}
void PrintList(pList plist)
{
    pNode cur = plist;
    while (cur)
    {
        printf("%d--->", cur->data);
        cur = cur->next;
    }
    printf("NULL\n");
}

pNode Find(pList plist, DataType d)
{
    pNode cur = plist;
    while (cur)
    {
        if (cur->data == d)
            return cur;
        cur = cur->next;

    }
    return NULL;
}
void Remove(pList* pplist, DataType d)
{
    pNode cur = *pplist;
    pNode prev = *pplist;
    assert(pplist);
    if (*pplist == NULL)
        return;
    while (cur)
    {
        if (cur->data == d)
        {
            pNode del = NULL;
            if (cur == *pplist)
            {
                *pplist = cur->next;
            }
            else
            {
                prev->next = cur->next;
            }
            free(del);
            del = NULL;
            return;
        }
        else
        {
            prev = cur;
            cur = cur->next;
        }
    }
}


void RemoveAll(pList* pplist, DataType d)
{
    pNode cur = *pplist;
    pNode prev = *pplist;
    assert(pplist);
    if (*pplist == NULL)
        return;
    while (cur)
    {
        if (cur->data == d)
        {
            pNode del = NULL;
            if (cur == *pplist)
            {
                *pplist = cur->next;
            }
            else
            {
                prev->next = cur->next;
            }
            cur = cur->next;
            free(del);
            del = NULL;
            return;
        }
        else
        {
            prev = cur;
            cur = cur->next;
        }
    }
}
void Insert(pList* pplist, pNode pos, DataType d)
{
    pNode cur = *pplist;
    pNode newnode = BuyNode(d);
    assert(pplist);
    newnode->next = pos->next;
    pos->next = newnode;

}
void Erase(pList* pplist, pNode pos)
{
    assert(pplist);
    pNode cur = *pplist;
    pNode del = NULL;
    pNode prev = NULL;
    if (cur->next == NULL)
    {
        if (pos == cur)
        {                      
            free(cur);
            *pplist= NULL;
        }
        return;
    }
    if (pos == cur)
    {                        
        del = cur;
        cur = cur->next;
        free(del);
        return;
    }
    cur = cur->next;
    while (cur)
    {
        if (cur == pos)
        {
            prev->next = cur->next;
            free(cur);
            return;
        }
        prev = cur;
        cur = cur->next;
    }
}
void ReverseList(pList* pplist)
{
    pNode cur = *pplist;
    pNode p = cur->next;

    if (cur!=NULL)
    {
        pNode q = p->next;
        cur->next = NULL;
        while (cur)
        {
            p->next = cur->next;
            cur->next = p;
            if (q)  
            {
                p = q;
                q = p->next;
            }
            else
                break;
        }
    }
    return cur;
}
int get_length(pList* pplist)
{

    int count = 0;
    pNode cur = *pplist;
    while (cur)
    {
        count++;
        cur = cur->next;
    }
    return count;
}

void BubbleSort(pList* pplist, pList plist)
{
    int i = 0;
    int j = 0;
    pNode tmp = NULL;
    int count = 0;
    count = get_length(&plist);
    for (i = 0; i < count - 1; i++)
    {
        pNode cur = *pplist;
        for (j = 0; j<count - i - 1; j++)
        {
            if (cur->data > cur->next->data)
            {
                tmp = cur->data;
                cur->data = cur->next->data;
                cur->next->data = tmp;
            }
            cur = cur->next;
        }
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值