队列
This commit is contained in:
parent
72d649b09f
commit
1633ab47cb
6
.vs/VSWorkspaceState.json
Normal file
6
.vs/VSWorkspaceState.json
Normal file
@ -0,0 +1,6 @@
|
||||
{
|
||||
"ExpandedNodes": [
|
||||
""
|
||||
],
|
||||
"PreviewInSolutionExplorer": false
|
||||
}
|
@ -19,6 +19,7 @@
|
||||
</ProjectConfiguration>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="array_queue.c" />
|
||||
<ClCompile Include="array_stack.c" />
|
||||
<ClCompile Include="linked_list.c" />
|
||||
<ClCompile Include="linked_list_stack.c" />
|
||||
@ -26,6 +27,7 @@
|
||||
<ClCompile Include="sq_list.c" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="array_queue.h" />
|
||||
<ClInclude Include="array_stack.h" />
|
||||
<ClInclude Include="linked_list.h" />
|
||||
<ClInclude Include="linked_list_stack.h" />
|
||||
|
@ -30,6 +30,9 @@
|
||||
<ClCompile Include="array_stack.c">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="array_queue.c">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="sq_list.h">
|
||||
@ -44,5 +47,8 @@
|
||||
<ClInclude Include="array_stack.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="array_queue.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
@ -1,18 +0,0 @@
|
||||
c:\code\lencode\project2\project2\debug\vc143.pdb
|
||||
c:\code\lencode\project2\project2\debug\vc143.idb
|
||||
c:\code\lencode\project2\project2\debug\sq_list.obj
|
||||
c:\code\lencode\project2\project2\debug\main.obj
|
||||
c:\code\lencode\project2\project2\debug\linked_list_stack.obj
|
||||
c:\code\lencode\project2\project2\debug\linked_list.obj
|
||||
c:\code\lencode\project2\project2\debug\array_stack.obj
|
||||
c:\code\lencode\project2\debug\project2.exe
|
||||
c:\code\lencode\project2\debug\project2.pdb
|
||||
c:\code\lencode\project2\project2\debug\project2.ilk
|
||||
c:\code\lencode\project2\project2\debug\project2.tlog\cl.command.1.tlog
|
||||
c:\code\lencode\project2\project2\debug\project2.tlog\cl.items.tlog
|
||||
c:\code\lencode\project2\project2\debug\project2.tlog\cl.read.1.tlog
|
||||
c:\code\lencode\project2\project2\debug\project2.tlog\cl.write.1.tlog
|
||||
c:\code\lencode\project2\project2\debug\project2.tlog\link.command.1.tlog
|
||||
c:\code\lencode\project2\project2\debug\project2.tlog\link.read.1.tlog
|
||||
c:\code\lencode\project2\project2\debug\project2.tlog\link.secondary.1.tlog
|
||||
c:\code\lencode\project2\project2\debug\project2.tlog\link.write.1.tlog
|
@ -1,3 +1,25 @@
|
||||
array_stack.c
|
||||
C:\code\lencode\Project2\array_stack.c(13,5): warning C4013: “memset”未定义;假设外部返回 int
|
||||
array_queue.c
|
||||
array_stack.c
|
||||
C:\code\lencode\Project2\array_queue.c(1,1): warning C4819: 该文件包含不能在当前代码页(936)中表示的字符。请将该文件保存为 Unicode 格式以防止数据丢失
|
||||
C:\code\lencode\Project2\array_queue.h(1,1): warning C4819: 该文件包含不能在当前代码页(936)中表示的字符。请将该文件保存为 Unicode 格式以防止数据丢失
|
||||
(编译源文件“array_queue.c”)
|
||||
|
||||
C:\code\lencode\Project2\array_queue.c(22,5): warning C4013: “memset”未定义;假设外部返回 int
|
||||
C:\code\lencode\Project2\array_stack.c(12,5): warning C4013: “memset”未定义;假设外部返回 int
|
||||
linked_list.c
|
||||
linked_list_stack.c
|
||||
C:\code\lencode\Project2\linked_list_stack.c(26,3): warning C4098: “push_stack_linked”:“void”函数返回值
|
||||
main.c
|
||||
C:\code\lencode\Project2\array_queue.h(1,1): warning C4819: 该文件包含不能在当前代码页(936)中表示的字符。请将该文件保存为 Unicode 格式以防止数据丢失
|
||||
(编译源文件“main.c”)
|
||||
|
||||
C:\code\lencode\Project2\main.c(37,22): warning C4013: “init_array_queue”未定义;假设外部返回 int
|
||||
C:\code\lencode\Project2\main.c(37,18): warning C4047: “初始化”:“array_queue *”与“int”的间接级别不同
|
||||
sq_list.c
|
||||
C:\code\lencode\Project2\sq_list.c(102,9): warning C4033: “locate_list”必须返回值
|
||||
C:\code\lencode\Project2\sq_list.c(112,9): warning C4033: “locate_list”必须返回值
|
||||
C:\code\lencode\Project2\sq_list.c(166,34): warning C4047: “函数”:“int”与“sq_list *”的间接级别不同
|
||||
C:\code\lencode\Project2\sq_list.c(166,34): warning C4024: “locate_list”: 形参和实参 2 的类型不同
|
||||
正在生成代码...
|
||||
C:\code\lencode\Project2\sq_list.c(116,1): warning C4715: “locate_list”: 不是所有的控件路径都返回值
|
||||
Project2.vcxproj -> C:\code\lencode\Project2\Debug\Project2.exe
|
||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -1,3 +1,4 @@
|
||||
C:\code\lencode\Project2\array_queue.c;C:\code\lencode\Project2\Project2\Debug\array_queue.obj
|
||||
C:\code\lencode\Project2\array_stack.c;C:\code\lencode\Project2\Project2\Debug\array_stack.obj
|
||||
C:\code\lencode\Project2\linked_list.c;C:\code\lencode\Project2\Project2\Debug\linked_list.obj
|
||||
C:\code\lencode\Project2\linked_list_stack.c;C:\code\lencode\Project2\Project2\Debug\linked_list_stack.obj
|
||||
|
Binary file not shown.
Binary file not shown.
@ -1,2 +1,2 @@
|
||||
^C:\CODE\LENCODE\PROJECT2\PROJECT2\DEBUG\ARRAY_STACK.OBJ|C:\CODE\LENCODE\PROJECT2\PROJECT2\DEBUG\LINKED_LIST.OBJ|C:\CODE\LENCODE\PROJECT2\PROJECT2\DEBUG\LINKED_LIST_STACK.OBJ|C:\CODE\LENCODE\PROJECT2\PROJECT2\DEBUG\MAIN.OBJ|C:\CODE\LENCODE\PROJECT2\PROJECT2\DEBUG\SQ_LIST.OBJ
|
||||
^C:\CODE\LENCODE\PROJECT2\PROJECT2\DEBUG\ARRAY_QUEUE.OBJ|C:\CODE\LENCODE\PROJECT2\PROJECT2\DEBUG\ARRAY_STACK.OBJ|C:\CODE\LENCODE\PROJECT2\PROJECT2\DEBUG\LINKED_LIST.OBJ|C:\CODE\LENCODE\PROJECT2\PROJECT2\DEBUG\LINKED_LIST_STACK.OBJ|C:\CODE\LENCODE\PROJECT2\PROJECT2\DEBUG\MAIN.OBJ|C:\CODE\LENCODE\PROJECT2\PROJECT2\DEBUG\SQ_LIST.OBJ
|
||||
C:\code\lencode\Project2\Project2\Debug\Project2.ilk
|
||||
|
Binary file not shown.
86
array_queue.c
Normal file
86
array_queue.c
Normal file
@ -0,0 +1,86 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "array_queue.h"
|
||||
|
||||
/*@-------------
|
||||
# 设计思路更新(原版漏洞太多):
|
||||
设计圆环式队列,当rear == size时,循环到数组的[0]位,当然这样的队列仍然遵循`先进先出原则`
|
||||
## 实现思路:
|
||||
通过取模运算,使得rear和front都在数组的[0,size-1]范围内,这样就能实现循环队列的功能.
|
||||
# 优点解析:
|
||||
在传统的队列中,当队尾到达队列的末尾时,即使队列前面还有空闲空间,也无法继续入队,导致空间浪费。而环形队列通过取模运算,\n
|
||||
使得队尾指针可以在到达队列末尾后,从头开始继续使用队列的空闲空间,从而提高了空间的利用率。
|
||||
@-------------*/
|
||||
|
||||
|
||||
// 初始化队列
|
||||
array_queue *init_array_queue()
|
||||
{
|
||||
array_queue *q = (array_queue *)malloc(sizeof(array_queue));
|
||||
if (q == NULL)
|
||||
{printf("error: malloc failed[From init_queue]"); return NULL;}
|
||||
memset(q,0,sizeof(array_queue));
|
||||
q->front = 0;
|
||||
q->rear = 0;
|
||||
q->size = 0;
|
||||
return q;
|
||||
}
|
||||
|
||||
// 判空
|
||||
int empty_array_queue(array_queue *q)
|
||||
{
|
||||
if (q->size == 0)
|
||||
return 1;
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 判断满
|
||||
int full_array_queue(array_queue *q)
|
||||
{
|
||||
if (q->size == MAX_QUEUE)
|
||||
return 1;
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 入队
|
||||
void push_array_queue(array_queue *q, elem_type value)
|
||||
{
|
||||
if (full_array_queue(q))
|
||||
{printf("error: queue is full[From push_queue]"); return;}
|
||||
q->data[q->rear] = value;
|
||||
q->rear = (q->front + q->size + 1) % MAX_QUEUE;
|
||||
q->size++;
|
||||
}
|
||||
|
||||
// 出队
|
||||
elem_type pop_array_queue(array_queue *q)
|
||||
{
|
||||
if (empty_array_queue(q))
|
||||
{printf("error: queue is empty[From pop_queue]"); return -1;}
|
||||
elem_type value = q->data[q->front];
|
||||
q->front = (q->front + 1) % MAX_QUEUE;
|
||||
q->size--;
|
||||
return value;
|
||||
}
|
||||
|
||||
// 打印队列
|
||||
void print_array_queue(array_queue *q)
|
||||
{
|
||||
if (empty_array_queue(q))
|
||||
{
|
||||
printf("error :\n");
|
||||
return ;
|
||||
}
|
||||
for (int i=q->front; i!=q->rear;i = (i+1)%MAX_QUEUE)
|
||||
{
|
||||
printf("[");
|
||||
printf("%d ",q->data[i]);
|
||||
printf("]\n");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
// if while for void return fuck shit def sleep
|
34
array_queue.h
Normal file
34
array_queue.h
Normal file
@ -0,0 +1,34 @@
|
||||
#ifndef ARRAY_QUEUE_H
|
||||
#define ARRAY_QUEUE_H
|
||||
#define MAX_QUEUE 3
|
||||
#define elem_type int
|
||||
// queue 构建
|
||||
typedef struct array_queue
|
||||
{
|
||||
elem_type data[MAX_QUEUE];
|
||||
int front; // 队列头指针
|
||||
int rear; // 队列尾指针
|
||||
int size;
|
||||
}array_queue;
|
||||
|
||||
// queue 初始化
|
||||
array_queue *inti_array_queue();
|
||||
|
||||
// 判空
|
||||
int empty_array_queue(array_queue *q);
|
||||
|
||||
//判满
|
||||
int full_array_queue(array_queue *q);
|
||||
|
||||
// 入队
|
||||
void push_array_queue(array_queue *q, elem_type value);
|
||||
|
||||
// 出队
|
||||
elem_type pop_array_queue(array_queue *q);
|
||||
|
||||
// 打印队列
|
||||
void print_array_queue(array_queue *q);
|
||||
|
||||
|
||||
|
||||
#endif
|
@ -2,12 +2,11 @@
|
||||
#include <stdlib.h>
|
||||
#include "array_stack.h"
|
||||
|
||||
// 初始化栈
|
||||
// 初始化数组栈
|
||||
array_stack* init_array_stack(void)
|
||||
{
|
||||
array_stack* new_stack = (array_stack*)malloc(sizeof(array_stack));
|
||||
if (new_stack == NULL) {
|
||||
printf("内存分配失败[来自init_array_stack()]\n");
|
||||
if (new_stack == NULL) { printf("error: malloc() failed [From: init_array_stack()]\n");
|
||||
return NULL;
|
||||
}
|
||||
memset(new_stack, 0, sizeof(array_stack));
|
||||
@ -15,24 +14,24 @@ array_stack* init_array_stack(void)
|
||||
return new_stack;
|
||||
}
|
||||
|
||||
// 入栈
|
||||
// 入栈
|
||||
void push_array_stack(array_stack* stack, elem_type value)
|
||||
{
|
||||
if (stack->top + 1 > MAX_S)
|
||||
{
|
||||
printf("栈已满[来自push_array_stack()]\n");
|
||||
printf("Stack overflow [From: push_array_stack()]\n");
|
||||
return;
|
||||
}
|
||||
stack->top++;
|
||||
stack->data[stack->top] = value;
|
||||
}
|
||||
|
||||
// 出栈
|
||||
// 出栈
|
||||
int pop_array_stack(array_stack* stack)
|
||||
{
|
||||
if (stack->top == -1)
|
||||
{
|
||||
printf("栈为空[来自pop_array_stack()]\n");
|
||||
printf("Stack underflow [From: pop_array_stack()]\n");
|
||||
return -1;
|
||||
}
|
||||
int value = stack->data[stack->top];
|
||||
@ -40,15 +39,15 @@ int pop_array_stack(array_stack* stack)
|
||||
return value;
|
||||
}
|
||||
|
||||
// 打印数组栈
|
||||
// 打印
|
||||
void print_array_stack(array_stack* stack)
|
||||
{
|
||||
if (stack->top == -1)
|
||||
{
|
||||
printf("栈为空[来自print_array_stack()]\n");
|
||||
printf("viod stack [From: print_array_stack()]\n");
|
||||
return;
|
||||
}
|
||||
printf("栈顶元素: %d\n", stack->data[stack->top]);
|
||||
printf("top: %d\n", stack->data[stack->top]);
|
||||
printf("-----\n");
|
||||
for (int i = stack->top; i >= 0; i--)
|
||||
{
|
||||
|
19
main.c
19
main.c
@ -3,6 +3,7 @@
|
||||
#include <string.h>
|
||||
#include "array_stack.h"
|
||||
#include "sq_list.h"
|
||||
#include "array_queue.h"
|
||||
|
||||
int main(void)
|
||||
{
|
||||
@ -27,13 +28,23 @@ int main(void)
|
||||
//print_sq_list(L);
|
||||
//printf("L->length:[%d]\n", L->length);
|
||||
|
||||
|
||||
// Őť˛âĘÔ
|
||||
/*
|
||||
// ջ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
array_stack* stack = init_array_stack();
|
||||
push_array_stack(stack, 1);
|
||||
push_array_stack(stack, 2);
|
||||
push_array_stack(stack, 3);
|
||||
print_array_stack(stack);
|
||||
push_array_stack(stack, 3); */
|
||||
array_queue *q = init_array_queue();
|
||||
push_array_queue(q,0);
|
||||
push_array_queue(q,1);
|
||||
push_array_queue(q,2);
|
||||
print_array_queue(q);
|
||||
printf("---------------------\n");
|
||||
pop_array_queue(q);
|
||||
print_array_queue(q);
|
||||
printf("----------\n");
|
||||
push_array_queue(q, 666);
|
||||
print_array_queue(q);
|
||||
printf("Hello World!\n");
|
||||
system("pause");
|
||||
return 0;
|
||||
|
Loading…
x
Reference in New Issue
Block a user