#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!");
}
}