单链表的基本操作

包括链表的初始化,销毁链表,头插,头删,尾插,尾删,指定位置插入,指定位置删除,链表长度

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

#define DateType int

typedef struct Node
{
    DateType data;
    struct Node* next;
}Node,*pNode;

void InitList(pNode *head)//初始化
{
    assert(head);
    (*head) = NULL;
}

pNode BuyNode(DateType e)//申请新节点
{
    pNode lk = (pNode)malloc(sizeof(Node));
    if(!lk)
        return NULL;

    lk->data = e;
    return lk;
}
void PushBack(pNode *head,DateType e)//尾插
{
    pNode nd,tmp;
    assert(head);
    nd = BuyNode(e);
    if(!nd)
        return;
    else if(*head == NULL)
    {
        *head = nd;
        nd->next = NULL;
    }
    else
    {
        tmp = *head;
        while(tmp->next)
        {
            tmp = tmp->next;
        }
        tmp->next = nd;
        nd->next = NULL;
    }

}

void PopBack(pNode *head)//尾删
{
    pNode tmp;
    pNode q = NULL;
    assert(head);

    if(*head == NULL)
        return;

    tmp = *head;
    while(tmp->next)
    {
        q = tmp;
        tmp = tmp->next;
    }
    if(q != NULL)//多个节点
    {
        q->next = NULL;
        free(tmp);
    }
    else//一个节点
    {
        free(tmp);      
        *head = NULL;
    }
}

void PrintList(pNode head)//打印链表
{
    pNode tmp = head;
    while(tmp)
    {
        printf("%d-->",tmp->data);
        tmp = tmp->next;
    }
    printf("NULL\n");
}

void PushFront(pNode *head,DateType e)//头插
{
    pNode nd = BuyNode(e);
    assert(head);
    if(nd)
    {
        nd->next = *head;
        *head = nd;
    }
}

void PopFront(pNode *head)//头删
{
    pNode tmp;
    assert(head);
    if(!*head)
        return;
    tmp = *head;
    *head = (*head)->next;
    free(tmp);
}

pNode Find(pNode head,DateType e)//找到指定元素的位置并返回,没找到返回NULL
{
    pNode tmp;
    assert(head);

    tmp = head;
    while(tmp->data != e)
    {
        tmp = tmp->next;
        if(tmp == NULL)
            return NULL;
    }
    return tmp;
}
void Inster(pNode *head,pNode pos,DateType e)//指定位置插入
{
    pNode nd;
    assert(head);
    if(pos && *head)
    {
        nd = BuyNode(e);
        nd->next = pos->next;
        pos->next = nd;
    }
}

void Erase(pNode *head,pNode pos)//指定位置删除
{
    pNode tmp;
    pNode q = NULL;//q是记录tmp的前一个位置
    assert(head);
    if(*head && pos)
    {
        tmp = *head;
        while(tmp != pos)
        {
            q = tmp;
            tmp = tmp->next;
        }
        if(q != NULL)
        {
            q->next = tmp->next;
            free(tmp);
        }
        else
        {
            free(tmp);      
            *head = NULL;
        }
    }
}

int LenList(pNode head)//链表长度
{
    int count = 0;
    pNode tmp = head;
    while(tmp)
    {
        tmp = tmp->next;
        ++count;
    }
    return count;
}

void Destroy(pNode *head)//销毁链表
{
    pNode tmp = *head;
    while(tmp)
    {
        pNode del = tmp;
        tmp = tmp->next;
        free(del);
    }
    *head = NULL;
}

/***********测试代码*****************/

void test1()
{
    pNode list;
    pNode pos;
    InitList(&list);

    PushBack(&list,1);//1->NULL
    PushBack(&list,2);//1->2->NULL
    PushBack(&list,3);//1->2->3->NULL
    PushFront(&list,0);//0->1->2->3->NULL
    PopFront(&list);//1->2->3->NULL
    PrintList(list);
    //PopBack(&list);
    //PopBack(&list);//1->2->NULL
    //PopBack(&list);//1->NULL
    //PopBack(&list);//NULL
    //PushFront(&list,0);//0->NULL
    //PopFront(&list);//NULL
    //PrintList(list);

    pos = Find(list,2);
    Inster(&list,pos,233);//1->2->233->3->NULL
    PrintList(list);

    pos = Find(list,233);
    Erase(&list,pos);//1->2->3->NULL
    PrintList(list);

    PopBack(&list);
    PopBack(&list);//1->NULL

    pos = Find(list,1);
    Erase(&list,pos);//NULL
    PrintList(list);
}
int main()
{
    test1();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值