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-27 11:38:12 +08:00
|
|
|
|
void insert_node(node* head,node *new_node)
|
2025-03-17 17:08:26 +08:00
|
|
|
|
{
|
2025-03-27 11:38:12 +08:00
|
|
|
|
new_node->next = head->next;
|
2025-03-17 17:08:26 +08:00
|
|
|
|
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-27 11:38:12 +08:00
|
|
|
|
n->n = 1;
|
|
|
|
|
n->p = head->next;
|
|
|
|
|
node *temp = head;
|
|
|
|
|
while (temp->next != NULL)
|
2025-03-17 17:08:26 +08:00
|
|
|
|
{
|
2025-03-27 11:38:12 +08:00
|
|
|
|
if (temp->value == value)
|
2025-03-17 17:08:26 +08:00
|
|
|
|
return n;
|
|
|
|
|
n->n += 1;
|
2025-03-27 11:38:12 +08:00
|
|
|
|
temp = temp->next;
|
|
|
|
|
n->p = temp;
|
2025-03-17 17:08:26 +08:00
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
|
{
|
2025-03-27 11:38:12 +08:00
|
|
|
|
node* head_1 = head;
|
2025-04-19 00:13:46 +08:00
|
|
|
|
//printf("[%d ",head_1->value);
|
|
|
|
|
printf("[");
|
|
|
|
|
while (1)
|
2025-03-21 13:07:47 +08:00
|
|
|
|
{
|
2025-03-27 11:38:12 +08:00
|
|
|
|
printf("%d ", head_1->value);
|
2025-04-19 00:13:46 +08:00
|
|
|
|
head_1 = head_1->next;
|
|
|
|
|
if (head_1 == NULL)
|
|
|
|
|
break;
|
2025-03-21 13:07:47 +08:00
|
|
|
|
}
|
2025-03-27 11:38:12 +08:00
|
|
|
|
printf("]\n");
|
2025-03-27 09:08:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
int get_node_list(node* head)
|
|
|
|
|
{
|
2025-03-27 11:38:12 +08:00
|
|
|
|
node *head_1 = head;
|
|
|
|
|
int i = 1;
|
|
|
|
|
while (head_1->next != NULL)
|
2025-03-27 09:08:11 +08:00
|
|
|
|
{
|
2025-03-27 11:38:12 +08:00
|
|
|
|
head_1 = head_1->next;
|
2025-03-27 09:08:11 +08:00
|
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ%d\n", i);
|
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA>
|
2025-03-27 11:38:12 +08:00
|
|
|
|
elem_type get_node_value(node* head,int pos)
|
2025-03-27 09:08:11 +08:00
|
|
|
|
{
|
2025-03-27 11:38:12 +08:00
|
|
|
|
node* head_1 = head;
|
2025-03-27 09:08:11 +08:00
|
|
|
|
int i = 1;
|
2025-03-27 11:38:12 +08:00
|
|
|
|
while (i < pos && head_1)
|
2025-03-27 09:08:11 +08:00
|
|
|
|
{
|
2025-03-27 11:38:12 +08:00
|
|
|
|
head_1 = head_1->next;
|
2025-03-27 09:08:11 +08:00
|
|
|
|
i++;
|
|
|
|
|
}
|
2025-03-27 11:38:12 +08:00
|
|
|
|
if (head_1 == NULL || i != pos)
|
|
|
|
|
return;
|
|
|
|
|
return head_1->value;
|
2025-03-17 17:08:26 +08:00
|
|
|
|
}
|