您好,欢迎来到华佗健康网。
搜索
您的当前位置:首页C link_list_without_head 无头单链表

C link_list_without_head 无头单链表

来源:华佗健康网
/*
    不带空头结点的单链表
    without dummy head node
*/

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

typedef int ElemType;

struct Node
{
    ElemType data;
    struct Node *next;
};

// 为了隐藏更多细节,同时创建更多的API
void InitNode(struct Node **node)
{
    *node = NULL;
}

struct Node *CreatNode(int data)
{
    struct Node *node;
    node = (struct Node *)malloc(sizeof(struct Node));
    if (node != NULL)
    {
        node->data = data;
        node->next = NULL;
        return node;
    }
    else
        return NULL;
}

void PrintList(struct Node *node)
{
    struct Node *ptr = node;
    while (ptr)
    {
        printf("%d ", ptr->data);
        ptr = ptr->next;
    }
    putchar('\n');
}

int Insertdata(struct Node **node, int pos, int data)
{
    int cnt = 0;
    if (*node == NULL) // 此时还没有元素,不管位置参数,直接作为头
    {
        *node = CreatNode(data);
        return 1;
    }
    else
    {
        struct Node *temp = NULL;
        struct Node *new = NULL;
        if (pos == 0) // 插在第一位
        {
            new = CreatNode(data);
            if (new)
            {
                new->next = *node;
                *node = new;
                return 1;
            }
            else
                return 0;
        }
        else
        {
            temp = *node;
            int cnt = 0;
            new = CreatNode(data);

            for (int i = 0; i < pos; i++)
            {
                if (!temp->next) // 到最后了还没找到
                {
                    if (new)
                    {
                        temp->next = CreatNode(data);
                        return 1;
                    }
                    else
                        return 0;
                }
                else
                {
                    if (i == pos - 1)
                    {
                        new->next = temp->next;
                        temp->next = new;
                        return 1;
                    }
                    temp = temp->next;
                }
            }
        }
    }
}

struct Node *Search_By_Index(struct Node **node, int index)
{
    struct Node *ptr = *node;
    for (int i = index - 1; i > 0; i--)
    {
        if (!ptr)
            return NULL;
        ptr = ptr->next;
    }
    return ptr;
}

int DelItem(struct Node **node, int index)
{
    struct Node *ptr = *node;
    if (Search_By_Index(node, index - 1))
    {
        if (index == 0)
        {
            *node = ptr->next;
            free(ptr);
        }
        else
        {
            for (int i = 0; i < index - 2; i++)
                ptr = ptr->next;
            if (ptr->next->next)
            {
                ptr->next = ptr->next->next;
                // free(ptr->next);
                return 1;
            }
            else
            {
                ptr->next = NULL;
                return 1;
            }
        }
        return 1;
    }
    else
    {
        printf("Delete Error!\n");
        return 0;
    }
}

int UpdateData(struct Node **node, int old_pos, int data)
{
    struct Node *ptr = Search_By_Index(node, old_pos);
    if (!ptr)
        return 0;
    else
    {
        ptr->data = data;
        return 1;
    }
}

int main()
{
    struct Node *phead;
    InitNode(&phead);
    Insertdata(&phead, 0, 2);
    Insertdata(&phead, 0, 1);
    Insertdata(&phead, 2, 3);
    Insertdata(&phead, 4, 4);
    PrintList(phead);
    DelItem(&phead, 2);
    PrintList(phead);
    UpdateData(&phead, 0, 9);
    PrintList(phead);
    return 0;
}

 

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- huatuo0.com 版权所有 湘ICP备2023021991号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务