summaryrefslogtreecommitdiff
path: root/c/dataStructure/栈和队列/队列/循环队列
diff options
context:
space:
mode:
Diffstat (limited to 'c/dataStructure/栈和队列/队列/循环队列')
-rwxr-xr-xc/dataStructure/栈和队列/队列/循环队列/main.c0
-rwxr-xr-xc/dataStructure/栈和队列/队列/循环队列/queue.c53
-rwxr-xr-xc/dataStructure/栈和队列/队列/循环队列/queue.h17
3 files changed, 70 insertions, 0 deletions
diff --git a/c/dataStructure/栈和队列/队列/循环队列/main.c b/c/dataStructure/栈和队列/队列/循环队列/main.c
new file mode 100755
index 0000000..e69de29
--- /dev/null
+++ b/c/dataStructure/栈和队列/队列/循环队列/main.c
diff --git a/c/dataStructure/栈和队列/队列/循环队列/queue.c b/c/dataStructure/栈和队列/队列/循环队列/queue.c
new file mode 100755
index 0000000..2c7f781
--- /dev/null
+++ b/c/dataStructure/栈和队列/队列/循环队列/queue.c
@@ -0,0 +1,53 @@
+#include "queue.h"
+
+bool InitQueue(Queue * q)
+{
+ q->base = (int *) malloc (sizeof(int) * MAXSIZE);
+ if (!q->base)
+ {
+ fprintf(stderr,"Failed to allocate memory!\n");
+ return false;
+ }
+
+ q->front = q->rear = 0;
+}
+
+bool EnQueue(Queue * q, int e)
+{
+ if ((q->rear + 1)%MAXSIZE == q->front)
+ {
+ fprintf(stderr,"Queue is full!\n");
+ return false;
+ }
+
+ q->base[q->rear] = e;
+ q->rear = (q->rear + 1) % MAXSIZE;
+
+ return true;
+}
+
+bool DeQueue(Queue * q, int * e)
+{
+ if (q->front == q->rear)
+ {
+ fprintf(stderr,"Queue is empty");
+ return false;
+ }
+
+ *e = q->base[q->front];
+ q->front = (q->front + 1) % MAXSIZE;
+
+ return true;
+}
+
+int LengthQueue(Queue * q)
+{
+ return (q->rear - q->front + MAXSIZE) % MAXSIZE;
+}
+
+int GetHead(Queue * q)
+{
+ if (q->front != q->rear)
+ return q->base[q->front];
+ return -1;
+} \ No newline at end of file
diff --git a/c/dataStructure/栈和队列/队列/循环队列/queue.h b/c/dataStructure/栈和队列/队列/循环队列/queue.h
new file mode 100755
index 0000000..11806b5
--- /dev/null
+++ b/c/dataStructure/栈和队列/队列/循环队列/queue.h
@@ -0,0 +1,17 @@
+#include<stdio.h>
+#include<stdlib.h>
+#include<stdbool.h>
+
+#define MAXSIZE 5
+
+typedef struct queue {
+ int * base;
+ int front;
+ int rear;
+} Queue;
+
+bool InitQueue (Queue * q);
+bool EnQueue (Queue * q, int e);
+bool DeQueue (Queue * q, int * e);
+int LengthQueue (Queue * q);
+int GetHead (Queue * q); \ No newline at end of file