单双链表练习题

本文是关于链表的一些操作(包括单链表和双向循环链表)
1、单链表,双链表的创建。
2、单链表和双链表的打印。
3、单链表的插入,删除。
4、双链表的插入和删除。
5、单链表的逆置。
6、单链表节点的个数。
7、单链表,双链表的查找。
函数代码:

//链表相关问题

typedef int DataType;
typedef struct LinkNode  //单链表结构
{
    struct LinkNode* next;
    DataType data;
}LinkNode;

LinkNode *CreateNode(DataType x) //创建单链表节点
{
    LinkNode *tmp = (LinkNode *)malloc(sizeof(LinkNode));
    if (NULL == tmp)
    {
        printf("分配内存失败!\n");
        return NULL;
    }
    tmp->next=NULL;
    tmp->data=x;
    return tmp;
}

void PrintLinkList(LinkNode *phead)  //单链表打印
{
    while (phead)
    {
        printf("%d ",phead->data);
        phead = phead->next;
    }
    printf("\n");
}

void InsertLinkList(LinkNode **phead,LinkNode *pos,DataType x)  //单链表插入
{
    LinkNode *newNode,*tmp = *phead;
    assert(phead);
    if (NULL==*phead)
    {
        *phead = CreateNode(x);
        return;
    }
    while(*phead != pos)
    {
        tmp = *phead;
        *phead = (*phead)->next;
    }
    newNode = CreateNode(x);
    newNode->next = tmp->next;
    tmp->next = newNode;
}

size_t ListNodeCount(LinkNode* phead) //计算单链表的节点数
{
    size_t count = 0;
    while (phead)
    {
        count++;
        phead = phead->next;
    }
    return count;
}

LinkNode *LinkListSearch(LinkNode *phead,DataType x)  //在单链表中查找一个数
{
    while(phead)
    {
        if (phead->data == x)
            return phead;
        phead = phead->next;
    }
    return NULL;
}

LinkNode *ReverseLinkList(LinkNode *phead)  //单链表的逆置
{
    LinkNode *first = phead;
    LinkNode *cur = first->next;
    first->next=NULL;

    while (cur)
    {
        LinkNode *tmp = cur->next;
        cur->next = first;
        first = cur;
        cur = tmp;
    }
    return first;
}

size_t RemoveLinkList(LinkNode **phead,LinkNode *pos)  //单链表任意节点删除
{
    LinkNode *first = *phead;
    while (first)
    {
        if (*phead == pos) //删头节点
        {
            *phead = first->next;
            free(pos);
            pos = NULL;
            return 1;
        }
        else if (first->next == pos) //非头节点情况
        {
            first->next = pos->next;
            free(pos);
            pos = NULL;
            return 1;
        }
        first = first->next;
    }
    return 0;
}

typedef struct DoubleLinkList  //双链表结构
{
    DataType data;
    struct DoubleLinkList *prev;
    struct DoubleLinkList *next;
}DoubleList;

DoubleList *CreateDoubleList(DataType x) //创建双链表节点
{
    DoubleList *newNode = (DoubleList *)malloc(sizeof(DoubleList));
    assert(newNode);
    newNode->next = NULL;
    newNode->prev = NULL;
    newNode->data = x;
    return newNode;
}

void PrintDoubleList(DoubleList *phead)  //打印双链表
{
    DoubleList *tmp = phead;
    while (tmp)
    {
        printf("%d ",tmp->data);
        tmp = tmp->next;
        if (tmp == phead)
            break;
    }
    printf("\n");
}

DoubleList *DoubleListSearch(DoubleList *phead,DataType x)  //双链表查找
{
    DoubleList *tmp = phead;
    while (phead)
    {
        if (phead->data == x)
            return phead;
        if (tmp == phead->next)
            break;
        phead = phead->next;
    }
    return NULL;
}

void DoubleListInsert(DoubleList **phead, DataType x) //双链表的头插
{
    DoubleList *tmp = (*phead);
    DoubleList *newNode = CreateDoubleList(x);

    if (NULL == *phead)
    {
        *phead = newNode;
        (*phead)->next = *phead;
        (*phead)->prev = *phead;
    }
    else 
    {
        newNode->next = (*phead)->next;
        (*phead)->next = newNode;
        newNode->prev = *phead;
        newNode->next->prev = newNode;
    }
}


size_t RemoveDoubleListNode(DoubleList **phead,DataType x)  //删除双链表节点
{
    DoubleList *tmp = *phead;
    while (*phead)
    {
        if (tmp->data == x)
        {
            tmp->prev->next = tmp->next;
            tmp->next->prev = tmp->prev;
            if (tmp->data == (*phead)->data)
               *phead = tmp->next;
            if ((*phead)->next == *phead)
            {
                free(*phead);
                *phead = NULL;
            }
            free(tmp);
            tmp = NULL;
            return 1;
        }
        if (*phead == tmp->next)
            break;
        tmp = tmp->next;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

龙跃十二

写的不错,给点鼓励

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值