2025-03-17 16:27:15 +08:00
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include "linked_list.h"
|
|
|
|
|
|
|
|
|
|
// <20><>ʼ<EFBFBD><CABC>ֵ
|
2025-03-21 13:07:47 +08:00
|
|
|
|
node* init_node(elem_type value)
|
2025-03-17 16:27:15 +08:00
|
|
|
|
{
|
|
|
|
|
node* new_node = (node*)malloc(sizeof(node));
|
2025-03-24 21:08:31 +08:00
|
|
|
|
if (new_node == NULL)
|
|
|
|
|
{
|
|
|
|
|
printf("error: malloc failed\n");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2025-03-21 13:07:47 +08:00
|
|
|
|
new_node->value = value;
|
2025-03-17 16:27:15 +08:00
|
|
|
|
new_node->next = NULL;
|
|
|
|
|
return new_node;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ɾ<><C9BE><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ڵ<EFBFBD>
|
|
|
|
|
void delete_node(node* n)
|
|
|
|
|
{
|
2025-03-21 13:07:47 +08:00
|
|
|
|
if (n->next != NULL) {
|
|
|
|
|
node* temp = n->next;
|
|
|
|
|
n->next = n->next->next;
|
|
|
|
|
free(temp);
|
|
|
|
|
}
|
2025-03-17 16:27:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <20>滻<EFBFBD>ڵ<EFBFBD>ֵ
|
2025-03-21 13:07:47 +08:00
|
|
|
|
void replace_node(node* n, elem_type value)
|
2025-03-17 16:27:15 +08:00
|
|
|
|
{
|
2025-03-21 13:07:47 +08:00
|
|
|
|
n->value = value;
|
2025-03-17 16:27:15 +08:00
|
|
|
|
}
|
2025-03-17 17:08:26 +08:00
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD>
|
2025-03-21 13:07:47 +08:00
|
|
|
|
void insert_node(node* head, elem_type value)
|
2025-03-17 17:08:26 +08:00
|
|
|
|
{
|
2025-03-21 13:07:47 +08:00
|
|
|
|
node* new_node = init_node(value);
|
2025-03-17 17:08:26 +08:00
|
|
|
|
new_node->next = head->next->next;
|
|
|
|
|
head->next = new_node;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD>ʽڵ<CABD>
|
|
|
|
|
int get_node(node* n)
|
|
|
|
|
{
|
|
|
|
|
return n->value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD>
|
2025-03-21 13:07:47 +08:00
|
|
|
|
address_node *find_node(node* head, elem_type value)
|
2025-03-17 17:08:26 +08:00
|
|
|
|
{
|
|
|
|
|
address_node* n = (address_node*)malloc(sizeof(address_node));
|
2025-03-21 13:07:47 +08:00
|
|
|
|
if (n == NULL) {
|
|
|
|
|
printf("<EFBFBD>ڴ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD><EFBFBD>\n");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2025-03-17 17:08:26 +08:00
|
|
|
|
n->n = 0;
|
|
|
|
|
n->p = head;
|
2025-03-21 13:07:47 +08:00
|
|
|
|
while (head != NULL)
|
2025-03-17 17:08:26 +08:00
|
|
|
|
{
|
2025-03-21 13:07:47 +08:00
|
|
|
|
if (head->value == value)
|
2025-03-17 17:08:26 +08:00
|
|
|
|
return n;
|
|
|
|
|
n->n += 1;
|
|
|
|
|
head = head->next;
|
|
|
|
|
}
|
|
|
|
|
printf("<EFBFBD><EFBFBD><EFBFBD>ҵ<EFBFBD><EFBFBD>ڵ<EFBFBD>\n");
|
2025-03-21 13:07:47 +08:00
|
|
|
|
free(n);
|
|
|
|
|
return NULL;
|
2025-03-17 17:08:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <20><>ӡ<EFBFBD><D3A1><EFBFBD><EFBFBD>
|
|
|
|
|
void print_node_list(node* head)
|
|
|
|
|
{
|
|
|
|
|
while (head->next != NULL)
|
2025-03-21 13:07:47 +08:00
|
|
|
|
{
|
|
|
|
|
head = head->next;
|
|
|
|
|
printf("%d ", head->value);
|
|
|
|
|
}
|
2025-03-17 17:08:26 +08:00
|
|
|
|
}
|