up
This commit is contained in:
parent
e6d7ab8884
commit
2f1c01e25a
@ -88,7 +88,8 @@ int main() {
|
|||||||
#### 方式1:直接克隆
|
#### 方式1:直接克隆
|
||||||
|
|
||||||
```bash
|
```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(推荐):
|
#### 方式2(推荐):
|
||||||
|
@ -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;
|
||||||
new_node->next = head->next->next;
|
|
||||||
head->next = new_node;
|
head->next = new_node;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -54,14 +53,16 @@ address_node *find_node(node* head, elem_type value)
|
|||||||
printf("内存分配失败\n");
|
printf("内存分配失败\n");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
n->n = 0;
|
n->n = 1;
|
||||||
n->p = head;
|
n->p = head->next;
|
||||||
while (head != NULL)
|
node *temp = head;
|
||||||
|
while (temp->next != NULL)
|
||||||
{
|
{
|
||||||
if (head->value == value)
|
if (temp->value == value)
|
||||||
return n;
|
return n;
|
||||||
n->n += 1;
|
n->n += 1;
|
||||||
head = head->next;
|
temp = temp->next;
|
||||||
|
n->p = temp;
|
||||||
}
|
}
|
||||||
printf("无法找到节点\n");
|
printf("无法找到节点\n");
|
||||||
free(n);
|
free(n);
|
||||||
@ -71,9 +72,42 @@ address_node *find_node(node* head, elem_type value)
|
|||||||
// 打印链表
|
// 打印链表
|
||||||
void print_node_list(node* head)
|
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;
|
head_1 = head_1->next;
|
||||||
printf("%d ", head->value);
|
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;
|
||||||
}
|
}
|
@ -11,7 +11,7 @@ typedef struct node
|
|||||||
typedef struct address_node
|
typedef struct address_node
|
||||||
{
|
{
|
||||||
int n;
|
int n;
|
||||||
node* p;
|
elem_type * p;
|
||||||
}address_node;
|
}address_node;
|
||||||
|
|
||||||
|
|
||||||
@ -25,7 +25,7 @@ void delete_node(node* n);
|
|||||||
void replace_node (node* n, elem_type value);
|
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);
|
int get_node(node* n);
|
||||||
@ -36,5 +36,11 @@ address_node *find_node(node* head, elem_type value);
|
|||||||
// 打印链表
|
// 打印链表
|
||||||
void print_node_list(node* head);
|
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
|
#endif
|
||||||
|
|
||||||
|
24
main.c
24
main.c
@ -1,9 +1,7 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "array_stack.h"
|
#include "linked_list.h"
|
||||||
#include "sq_list.h"
|
|
||||||
#include "array_queue.h"
|
|
||||||
|
|
||||||
int main(void)
|
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,0);
|
||||||
push_array_queue(q,1);
|
push_array_queue(q,1);
|
||||||
push_array_queue(q,2);
|
push_array_queue(q,2);
|
||||||
@ -46,7 +44,23 @@ int main(void)
|
|||||||
print_array_queue(q);
|
print_array_queue(q);
|
||||||
printf("----------\n");
|
printf("----------\n");
|
||||||
push_array_queue(q, 666);
|
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");
|
printf("Hello World!\n");
|
||||||
system("pause");
|
system("pause");
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -6,6 +6,11 @@
|
|||||||
sq_list* init_sq_list(void)
|
sq_list* init_sq_list(void)
|
||||||
{
|
{
|
||||||
sq_list* name = (sq_list*)malloc(sizeof(sq_list));
|
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));
|
memset(name, 0, sizeof(sq_list));
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user