add linked_queue,up linked_list
添加了 基于链表实现的队列 更改了 链表的打印函数的终止判断条件. Co-Authored-By: Jdhggg <111557398+Jdhggg@users.noreply.github.com>
This commit is contained in:
parent
cf39e5499f
commit
aa4996a940
5
.gitignore
vendored
5
.gitignore
vendored
@ -57,3 +57,8 @@ dkms.conf
|
|||||||
*.recipe
|
*.recipe
|
||||||
*.tlog
|
*.tlog
|
||||||
*.log
|
*.log
|
||||||
|
*.vcxproj
|
||||||
|
*.filters
|
||||||
|
Project2.vcxproj.filters
|
||||||
|
*.filters
|
||||||
|
*.vcxproj
|
||||||
|
@ -73,11 +73,14 @@ address_node *find_node(node* head, elem_type value)
|
|||||||
void print_node_list(node* head)
|
void print_node_list(node* head)
|
||||||
{
|
{
|
||||||
node* head_1 = head;
|
node* head_1 = head;
|
||||||
printf("[%d ",head_1->value);
|
//printf("[%d ",head_1->value);
|
||||||
while (head_1->next != NULL)
|
printf("[");
|
||||||
|
while (1)
|
||||||
{
|
{
|
||||||
head_1 = head_1->next;
|
|
||||||
printf("%d ", head_1->value);
|
printf("%d ", head_1->value);
|
||||||
|
head_1 = head_1->next;
|
||||||
|
if (head_1 == NULL)
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
printf("]\n");
|
printf("]\n");
|
||||||
}
|
}
|
||||||
|
91
linked_queue.c
Normal file
91
linked_queue.c
Normal file
@ -0,0 +1,91 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include "linked_queue.h"
|
||||||
|
|
||||||
|
link_queue* init_link_queue()
|
||||||
|
{
|
||||||
|
link_queue* q = (link_queue*)malloc(sizeof(link_queue));
|
||||||
|
if (q == NULL)
|
||||||
|
{
|
||||||
|
printf("error: malloc from init_link_queue failed\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
q->front = NULL;
|
||||||
|
q->rear = NULL;
|
||||||
|
q->size = 0;
|
||||||
|
return q;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Èë¶Ó
|
||||||
|
void push_link_queue(link_queue* q, elem_type value)
|
||||||
|
{
|
||||||
|
node* new_node = (node*)malloc(sizeof(node));
|
||||||
|
if (new_node == NULL)
|
||||||
|
{
|
||||||
|
printf("error: malloc from push_link_queue failed\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
new_node->value = value;
|
||||||
|
new_node->next = NULL;
|
||||||
|
if (q->front == NULL)
|
||||||
|
{
|
||||||
|
q->front = new_node;
|
||||||
|
q->rear = new_node;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
q->rear->next = new_node;
|
||||||
|
q->rear = new_node;
|
||||||
|
}
|
||||||
|
q->size++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ³ö¶Ó
|
||||||
|
elem_type pop_link_queue(link_queue* q)
|
||||||
|
{
|
||||||
|
if (empty_link_queue(q))
|
||||||
|
{
|
||||||
|
printf("error: pop from empty link_queue\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
link_queue* tmp = q->front;
|
||||||
|
elem_type a = q->front->value;
|
||||||
|
q->front = q->front->next;
|
||||||
|
q->size--;
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// ÅпÕ
|
||||||
|
int empty_link_queue(link_queue* q)
|
||||||
|
{
|
||||||
|
if (q->size == 0)
|
||||||
|
{
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// ´òÓ¡
|
||||||
|
int print_link_queue(link_queue* q)
|
||||||
|
{
|
||||||
|
if (empty_link_queue(q))
|
||||||
|
{
|
||||||
|
printf("error: queue empty form: print_link_queue\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
node* head = q->front;
|
||||||
|
printf("[");
|
||||||
|
while (1)
|
||||||
|
{
|
||||||
|
printf(" %d ", head->value);
|
||||||
|
head = head->next;
|
||||||
|
if (head == NULL)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
printf("]\n");
|
||||||
|
}
|
34
linked_queue.h
Normal file
34
linked_queue.h
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
#ifndef LINKED_QUEUE_H
|
||||||
|
#define LINKED_QUEUE_H
|
||||||
|
#define elem_type int
|
||||||
|
|
||||||
|
typedef struct node
|
||||||
|
{
|
||||||
|
elem_type value;
|
||||||
|
struct node* next;
|
||||||
|
} node;
|
||||||
|
|
||||||
|
typedef struct linked_queue
|
||||||
|
{
|
||||||
|
struct node* front;
|
||||||
|
struct node* rear;
|
||||||
|
int size;
|
||||||
|
}link_queue;
|
||||||
|
|
||||||
|
// 初始化
|
||||||
|
link_queue* init_link_queue();
|
||||||
|
|
||||||
|
// 入队
|
||||||
|
void push_link_queue(link_queue* q, elem_type value);
|
||||||
|
|
||||||
|
// 出队
|
||||||
|
elem_type pop_link_queue(link_queue* q);
|
||||||
|
|
||||||
|
// 判空
|
||||||
|
int empty_link_queue(link_queue* q);
|
||||||
|
|
||||||
|
// 打印
|
||||||
|
int print_link_queue(link_queue* q);
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
53
main.c
53
main.c
@ -60,34 +60,34 @@ int main(void)
|
|||||||
push_array_queue(q, 666);
|
push_array_queue(q, 666);
|
||||||
print_array_queue(q);*/
|
print_array_queue(q);*/
|
||||||
|
|
||||||
// node* q1 = init_node(1);
|
//node* q1 = init_node(1);
|
||||||
// node* q2 = init_node(2);
|
//node* q2 = init_node(2);
|
||||||
// node* q3 = init_node(3);
|
//node* q3 = init_node(3);
|
||||||
// node* q4 = init_node(4);
|
//node* q4 = init_node(4);
|
||||||
// q1->next = q2;
|
//q1->next = q2;
|
||||||
// q2->next = q3;
|
//q2->next = q3;
|
||||||
// q3->next = q4;
|
//q3->next = q4;
|
||||||
// //address_node *n = find_node(q1, 3);
|
////address_node *n = find_node(q1, 3);
|
||||||
// // printf("%d\n %p\n", n->n,n->p
|
//// printf("%d\n %p\n", n->n,n->p
|
||||||
// //int e = get_node_list(q1);
|
////int e = get_node_list(q1);
|
||||||
// node* q2_3 = init_node(0);
|
//node* q2_3 = init_node(0);
|
||||||
// insert_node(q2, q2_3);
|
//insert_node(q2, q2_3);
|
||||||
// print_node_list(q1);
|
//print_node_list(q1);
|
||||||
// delete_node(q2);
|
//delete_node(q2);
|
||||||
// print_node_list(q1);
|
//print_node_list(q1);
|
||||||
|
|
||||||
/*
|
|
||||||
stack_linked* head = init_stack_linked();
|
/* stack_linked* head = init_stack_linked();
|
||||||
push_stack_linked(head, 1);
|
push_stack_linked(head, 1);
|
||||||
push_stack_linked(head, 2);
|
push_stack_linked(head, 2);
|
||||||
push_stack_linked(head, 3);
|
push_stack_linked(head, 3);
|
||||||
print_linked(head);
|
print_linked(head);
|
||||||
pop_stack_linked(head);
|
pop_stack_linked(head);
|
||||||
print_linked(head);
|
print_linked(head);*/
|
||||||
*/
|
|
||||||
|
|
||||||
// queue_test();
|
// queue_test();
|
||||||
array_queue *q = init_array_queue();
|
/*array_queue *q = init_array_queue();
|
||||||
push_array_queue(q, 1);
|
push_array_queue(q, 1);
|
||||||
push_array_queue(q, 2);
|
push_array_queue(q, 2);
|
||||||
push_array_queue(q, 3);
|
push_array_queue(q, 3);
|
||||||
@ -97,7 +97,18 @@ stack_linked* head = init_stack_linked();
|
|||||||
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);*/
|
||||||
|
/* link_queue* q = init_link_queue();
|
||||||
|
push_link_queue(q, 1);
|
||||||
|
push_link_queue(q, 2);
|
||||||
|
push_link_queue(q, 3);
|
||||||
|
print_link_queue(q);
|
||||||
|
printf("____________________\n");
|
||||||
|
pop_link_queue(q);
|
||||||
|
print_link_queue(q);*/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
printf("Hello World!\n");
|
printf("Hello World!\n");
|
||||||
system("pause");
|
system("pause");
|
||||||
return 0;
|
return 0;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user