单链表的小应用

本文介绍了一个简单的单链表数据结构实现,包括初始化、插入、删除、更新等基本操作,并展示了如何通过这些操作来管理链表中的元素。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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


#define T 1               //对T, F进行宏定义
#define F -1


typedef int Type;                    //自定义数据类型Type


struct Node                         //定义结点
{
    struct Node* next;
    int value;  
};


int init(struct Node** head);                 //初始化数据链表,要初始化链表所以用二维指针
int insert_tail(struct Node* head, Type value);     //尾插函数
int insert_head(struct Node* head, Type value);   //前插函数
int insert_index(struct Node* head, Type value, int index);    //中间插入函数
int length(struct Node* head);         //测定链表长度
int delete_index(struct Node* head, int index);          //按位删除
int delete_value(struct Node* head, Type value);       //按值删除
int update_index(struct Node* head, int index, Type value);    //按位替换
void update_value(struct Node* head, Type value0, Type value1);    //按值替换
int query_value(struct Node* head, Type value);    //按值寻址
int query_index(struct Node* head, int index);           //按位取值
void print(struct Node* head);    //输出函数

int main()
{
    struct Node* head;
    int ret;           //返回值用ret表示
    int i;


    ret = init(&head);      //对head初始化
    if (F == ret)          //判断是否初始化成功
    {
        return 1;

    }


    for (i = 0; i < 10; i++)              //用前插赋值
    {
        insert_head(head, i);
    }


    print(head);


    for (i = 0; i < 10; i++)            //用尾插赋值
    {
        insert_tail(head, i);
    }


    print(head);


    printf("length = %d\n", length(head));         //计算链表长度


    delete_index(head, 0);                        //按位删除
    delete_index(head, length(head) - 1);
    delete_index(head, 5);
    print(head);
    printf("length = %d\n", length(head));


    insert_index(head, 99, 0);            //中间插入
    insert_index(head, 99, length(head));
    insert_index(head, 99, 5);
    print(head);
    printf("length = %d\n", length(head));


    delete_value(head, 99);          //按值删除
    print(head);
    delete_value(head, 0);
    print(head);


    update_index(head, 2, -1);      //按位替换
    print(head);


    update_value(head, 8, 99);        //按值替换
    print(head);
    
    query_value(head, 99);          //按值寻址
    query_value(head, 8);


    query_index(head, 5);        //按位取值


    return 0;

int init(struct Node** head)
{
    struct Node* newnode = (struct Node*)malloc(sizeof(struct Node));
    if (NULL == newnode)
    {
        return F;
    }
    newnode->value = 0;
    newnode->next = NULL;
    (*head) = newnode;


    return T;
}
int insert_tail(struct Node* head, Type value)
{
    struct Node* newnode = (struct Node*)malloc(sizeof(struct Node));
    if (NULL == newnode)
    {
        return F;
    }
    newnode->value = value;
    newnode->next = NULL;
    while (head->next != NULL)
    {
        head = head->next;
    }
    head->next = newnode;
    return T;
}


int insert_head(struct Node* head, Type value)
{
    struct Node* newnode = (struct Node*)malloc(sizeof(struct Node));
    if (NULL == newnode)
    {
        return F;
    }
    newnode->value = value;
    newnode->next = head->next; 
    head->next = newnode;
    return T;
}


int length(struct Node* head)
{
    int cont = 0;
    while (head->next != NULL)
    {
        head = head->next;
        cont++;
    }
    return cont;
}


int delete_index(struct Node* head, int index)
{
    if (index < 0 || index >= length(head))
    {
        printf("out of range\n");
        return F;
    }
    int i;
    struct Node* temp;
    for (i = 0; i < index; i++)
    {
        head = head->next;
    }
    temp = head->next->next;
    free(head->next);
    head->next = temp;
    return T;
}


void print(struct Node* head)
{
    while (head->next != NULL)
    {
        printf("%d ", head->next->value);
        head = head->next;
    }
    printf("\n");    
}


int insert_index(struct Node* head, Type value, int index)
{
    if (index < 0 || index > length(head))
    {
        printf("out of range\n");
        return F;
    }
    struct Node* newnode = (struct Node*)malloc(sizeof(struct Node));
    if (NULL == newnode)
    {
        return F;
    }
    int i;
    for (i = 0; i < index; i++)
    {
        head = head->next;
    }
    newnode->value = value;
    newnode->next = head->next;
    head->next = newnode;
    return T;
}


int delete_value(struct Node* head, Type value)
{
    int i;
    int len = length(head);
    for (i = 0; i < len; i++)
    {
        if (value != head->next->value)
        {
            head = head->next;
            continue;
        }
        else
        {
            struct Node* temp = head->next->next;
            free(head->next);
            head->next = temp;
        }
    }


    return T;
}


int update_index(struct Node* head, int index, Type value)
{
    if (index < 0 || index >= length(head))
    {
        printf("out of range\n");
        return F;
    }
    int i;
    for (i = 0; i <= index; i++)
    {
        head = head->next;
    }
    head->next->value = value;
    return T;
}


void update_value(struct Node* head, Type value0, Type value1)
{
    int i;
    int len = length(head);
    for (i = 0; i < len; i++)
    {
        if (value0 == head->next->value)
        {
            head->next->value = value1;
        }
        head = head->next;
    }
}


int query_value(struct Node* head, Type value)
{
    int cont = 0;
    int i;
    int index = 0;
    int len = length(head);
    for (i = 0; i < len; i++)
    {
        if (value == head->next->value)
        {
            cont++;
            printf("found value %d = index %d\n", value, index);
        }
        index++;
        head = head->next;
    }
    if (!cont)
    {
        printf("not found value %d\n", value);
        return F;
    }
    return T;
}


int query_index(struct Node* head, int index)
{
    if (index < 0 || index >= length(head))
    {
        printf("out of range\n");
        return F;
    }
    int i;
    for (i = 0; i < index; i++)
    {
        head = head->next;
    }
    printf("index %d : value %d\n", index, head->next->value);
    return T;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值