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
|
||||
*.tlog
|
||||
*.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)
|
||||
{
|
||||
node* head_1 = head;
|
||||
printf("[%d ",head_1->value);
|
||||
while (head_1->next != NULL)
|
||||
//printf("[%d ",head_1->value);
|
||||
printf("[");
|
||||
while (1)
|
||||
{
|
||||
head_1 = head_1->next;
|
||||
printf("%d ", head_1->value);
|
||||
head_1 = head_1->next;
|
||||
if (head_1 == NULL)
|
||||
break;
|
||||
}
|
||||
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
|
23
main.c
23
main.c
@ -76,18 +76,18 @@ int main(void)
|
||||
//delete_node(q2);
|
||||
//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, 2);
|
||||
push_stack_linked(head, 3);
|
||||
print_linked(head);
|
||||
pop_stack_linked(head);
|
||||
print_linked(head);
|
||||
*/
|
||||
print_linked(head);*/
|
||||
|
||||
|
||||
// queue_test();
|
||||
array_queue *q = init_array_queue();
|
||||
/*array_queue *q = init_array_queue();
|
||||
push_array_queue(q, 1);
|
||||
push_array_queue(q, 2);
|
||||
push_array_queue(q, 3);
|
||||
@ -97,7 +97,18 @@ stack_linked* head = init_stack_linked();
|
||||
print_array_queue(q);
|
||||
printf("----------\n");
|
||||
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");
|
||||
system("pause");
|
||||
return 0;
|
||||
|
Loading…
x
Reference in New Issue
Block a user