链表的模板

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

//定义节点
typedef struct ListNode
{
    int data; 
    struct ListNode *next;
}ListNode;

//链表
typedef struct LinkedList
{
    ListNode* head;
    size_t size;
}LinkedList;

//链表初始化
void LinkedListCreate(LinkedList* list){
    list->head = NULL;
    list->size = 0;
}

//链表摧毁
void LinkedListDestroy(LinkedList* list){
    while(list->head){
        ListNode* temp = list->head;
        list->head = list->head->next;
        free(temp);
    }
    list->size = 0;
}

//链表插入
void LinkedListInsert(LinkedList* list, int i, int value){
    if(i < 0 || i > list->size){
        printf("Invalid index\n");
        return ;
    }
    ListNode* newnode = (ListNode*)malloc(sizeof(ListNode));
    newnode->data = value;
    if(i == 0){
        newnode->next = list->head;
        list->head = newnode;
    }else{
        ListNode* current = list->head;
        for(int j = 0;j < i - 1;++j){
            current = current->next;
        }
        newnode->next = current->next;
        current->next = newnode;
    }
    list->size++;
}

//链表删除
void LinkedListRemove(LinkedList* list, int i){
    if(i < 0 || i >= list->size){
        printf("Invalid index\n");
        return ;
    }
    if(i == 0){
        ListNode* next = list->head->next;
        free(list->head);
        list->head = next;
    }else{
        ListNode* current = list->head;
        for(int j = 0;j < i - 1;++j){
            current = current->next;
        }
        ListNode* next = current->next->next;
        free(current->next);
        current->next = next;
    }
    --list->size;
}

//链表寻找
ListNode* LinkedListFind(LinkedList* list, int value){
    ListNode* current = list->head;
    while(current){
        if(current->data == value){
            return current;
        }
        current = current->next;
    }
    return NULL;
}

//链表节点索引
ListNode* LinkedListGet(LinkedList* list,int i){
    if(i < 0 || i >= list->size){
        printf("Invalid index\n");
        return NULL;
    }
    ListNode* current = list->head;
    for(int j = 0;j < i;++j){
        current = current->next;
    }
    return current;
}

//链表更新
void LinkedListUpdate(LinkedList* list,int i,int value){
    ListNode* node = LinkedListGet(list, i);
    if(node){
        node->data = value;
    }
}

//打印链表
void LinkedListPrint(LinkedList* list){
    ListNode* current = list->head;
    while(current){
        printf("%d->",current->data);
        current = current->next;
    }
    printf("Null\n");
}

int main(){
    LinkedList* list;
    LinkedListCreate( &list);
    LinkedListInsert( &list, 0, 10);
    LinkedListInsert( &list, 1, 20);
    LinkedListInsert( &list, 2, 40);
    LinkedListInsert (&list, 3, 30);
    printf("Original List:");
    LinkedListPrint( &list);

    LinkedListRemove( &list, 2);
    LinkedListUpdate( &list, 1, 100);
    LinkedListPrint( &list);
    ListNode* found = LinkedListFind( &list, 55);
    if(!found){
        printf("Element not found!");
    }

}

#include <stdio.h>

#include <stdlib.h>

//定义节点

typedef struct ListNode

{

    int data;

    struct ListNode *next;

}ListNode;

//链表

typedef struct LinkedList

{

    ListNode* head;

    size_t size;

}LinkedList;

//链表初始化

void LinkedListCreate(LinkedList* list){

    list->head = NULL;

    list->size = 0;

}

//链表摧毁

void LinkedListDestroy(LinkedList* list){

    while(list->head){

        ListNode* temp = list->head;

        list->head = list->head->next;

        free(temp);

    }

    list->size = 0;

}

//链表插入

void LinkedListInsert(LinkedList* list, int i, int value){

    if(i < 0 || i > list->size){

        printf("Invalid index\n");

        return ;

    }

    ListNode* newnode = (ListNode*)malloc(sizeof(ListNode));

    newnode->data = value;

    if(i == 0){

        newnode->next = list->head;

        list->head = newnode;

    }else{

        ListNode* current = list->head;

        for(int j = 0;j < i - 1;++j){

            current = current->next;

        }

        newnode->next = current->next;

        current->next = newnode;

    }

    list->size++;

}

//链表删除

void LinkedListRemove(LinkedList* list, int i){

    if(i < 0 || i >= list->size){

        printf("Invalid index\n");

        return ;

    }

    if(i == 0){

        ListNode* next = list->head->next;

        free(list->head);

        list->head = next;

    }else{

        ListNode* current = list->head;

        for(int j = 0;j < i - 1;++j){

            current = current->next;

        }

        ListNode* next = current->next->next;

        free(current->next);

        current->next = next;

    }

    --list->size;

}

//链表寻找

ListNode* LinkedListFind(LinkedList* list, int value){

    ListNode* current = list->head;

    while(current){

        if(current->data == value){

            return current;

        }

        current = current->next;

    }

    return NULL;

}

//链表节点索引

ListNode* LinkedListGet(LinkedList* list,int i){

    if(i < 0 || i >= list->size){

        printf("Invalid index\n");

        return NULL;

    }

    ListNode* current = list->head;

    for(int j = 0;j < i;++j){

        current = current->next;

    }

    return current;

}

//链表更新

void LinkedListUpdate(LinkedList* list,int i,int value){

    ListNode* node = LinkedListGet(list, i);

    if(node){

        node->data = value;

    }

}

//打印链表

void LinkedListPrint(LinkedList* list){

    ListNode* current = list->head;

    while(current){

        printf("%d->",current->data);

        current = current->next;

    }

    printf("Null\n");

}

int main(){

    LinkedList* list;

    LinkedListCreate( &list);

    LinkedListInsert( &list, 0, 10);

    LinkedListInsert( &list, 1, 20);

    LinkedListInsert( &list, 2, 40);

    LinkedListInsert (&list, 3, 30);

    printf("Original List:");

    LinkedListPrint( &list);

    LinkedListRemove( &list, 2);

    LinkedListUpdate( &list, 1, 100);

    LinkedListPrint( &list);

    ListNode* found = LinkedListFind( &list, 55);

    if(!found){

        printf("Element not found!");

    }

}

  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值