This commit is contained in:
Jdhggg 2025-03-28 10:42:19 +08:00
parent e6d7ab8884
commit 2f1c01e25a
5 changed files with 79 additions and 19 deletions

View File

@ -88,7 +88,8 @@ int main() {
#### 方式1直接克隆
```bash
http://1.95.167.80:3000/learn_code/learn_code.git
git clone https://github.com/jdhnsu/C_DS_Algo.git
cd C_DS_Algo
```
#### 方式2推荐

View File

@ -33,10 +33,9 @@ void replace_node(node* n, elem_type value)
}
// 插入节点
void insert_node(node* head, elem_type value)
void insert_node(node* head,node *new_node)
{
node* new_node = init_node(value);
new_node->next = head->next->next;
new_node->next = head->next;
head->next = new_node;
}
@ -54,14 +53,16 @@ address_node *find_node(node* head, elem_type value)
printf("内存分配失败\n");
return NULL;
}
n->n = 0;
n->p = head;
while (head != NULL)
n->n = 1;
n->p = head->next;
node *temp = head;
while (temp->next != NULL)
{
if (head->value == value)
if (temp->value == value)
return n;
n->n += 1;
head = head->next;
temp = temp->next;
n->p = temp;
}
printf("无法找到节点\n");
free(n);
@ -71,9 +72,42 @@ address_node *find_node(node* head, elem_type value)
// 打印链表
void print_node_list(node* head)
{
while (head->next != NULL)
node* head_1 = head;
printf("[%d ",head_1->value);
while (head_1->next != NULL)
{
head = head->next;
printf("%d ", head->value);
head_1 = head_1->next;
printf("%d ", head_1->value);
}
printf("]\n");
}
// 获得链表长度
int get_node_list(node* head)
{
node *head_1 = head;
int i = 1;
while (head_1->next != NULL)
{
head_1 = head_1->next;
i++;
}
printf("链表长度为%d\n", i);
return i;
}
// 获得元素
elem_type get_node_value(node* head,int pos)
{
node* head_1 = head;
int i = 1;
while (i < pos && head_1)
{
head_1 = head_1->next;
i++;
}
if (head_1 == NULL || i != pos)
return;
return head_1->value;
}

View File

@ -11,7 +11,7 @@ typedef struct node
typedef struct address_node
{
int n;
node* p;
elem_type * p;
}address_node;
@ -25,7 +25,7 @@ void delete_node(node* n);
void replace_node (node* n, elem_type value);
// 插入节点
void insert_node(node* head, elem_type value);
void insert_node(node* head,node *new_node);
//访问节点
int get_node(node* n);
@ -36,5 +36,11 @@ address_node *find_node(node* head, elem_type value);
// 打印链表
void print_node_list(node* head);
// get length of linked list
int get_node_list(node* head);
// »ñµÄ
elem_type get_node_value(node* head,int pos);
#endif

24
main.c
View File

@ -1,9 +1,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "array_stack.h"
#include "sq_list.h"
#include "array_queue.h"
#include "linked_list.h"
int main(void)
{
@ -36,7 +34,7 @@ int main(void)
array_queue *q = init_array_queue();
/* array_queue *q = init_array_queue();
push_array_queue(q,0);
push_array_queue(q,1);
push_array_queue(q,2);
@ -46,7 +44,23 @@ int main(void)
print_array_queue(q);
printf("----------\n");
push_array_queue(q, 666);
print_array_queue(q);
print_array_queue(q);*/
node* q1 = init_node(1);
node* q2 = init_node(2);
node* q3 = init_node(3);
node* q4 = init_node(4);
q1->next = q2;
q2->next = q3;
q3->next = q4;
//address_node *n = find_node(q1, 3);
// printf("%d\n %p\n", n->n,n->p
//int e = get_node_list(q1);
node* q2_3 = init_node(0);
insert_node(q2, q2_3);
print_node_list(q1);
delete_node(q2);
print_node_list(q1);
printf("Hello World!\n");
system("pause");
return 0;

View File

@ -6,6 +6,11 @@
sq_list* init_sq_list(void)
{
sq_list* name = (sq_list*)malloc(sizeof(sq_list));
if (name == NULL)
{
printf("error: malloc failed![From init_sq_list]\n");
return NULL;
}
memset(name, 0, sizeof(sq_list));
return name;
}