summaryrefslogtreecommitdiff
path: root/c/dataStructure/栈和队列/队列/链队列
diff options
context:
space:
mode:
Diffstat (limited to 'c/dataStructure/栈和队列/队列/链队列')
-rwxr-xr-xc/dataStructure/栈和队列/队列/链队列/main.c28
-rwxr-xr-xc/dataStructure/栈和队列/队列/链队列/queue.c75
-rwxr-xr-xc/dataStructure/栈和队列/队列/链队列/queue.h23
3 files changed, 126 insertions, 0 deletions
diff --git a/c/dataStructure/栈和队列/队列/链队列/main.c b/c/dataStructure/栈和队列/队列/链队列/main.c
new file mode 100755
index 0000000..db5ae9a
--- /dev/null
+++ b/c/dataStructure/栈和队列/队列/链队列/main.c
@@ -0,0 +1,28 @@
+#include "queue.h"
+
+int main(void)
+{
+ ElemType e;
+ pqueue head, tail;
+ InitQueue(&head);
+ InitQueue(&tail);
+
+ while ((e = getchar()) != '\n')
+ {
+ EnQueue(&tail, &e);
+ if (EmptyQueue(&head))
+ head = tail;
+ }
+
+ PrintQueue(&head);
+ ElemType retval;
+
+ DeQueue(&head, &retval);
+ printf("'%c' quit queue\n",retval);
+
+ PrintQueue(&head);
+
+ RsQueue(&head);
+
+ return 0;
+}
diff --git a/c/dataStructure/栈和队列/队列/链队列/queue.c b/c/dataStructure/栈和队列/队列/链队列/queue.c
new file mode 100755
index 0000000..94361d1
--- /dev/null
+++ b/c/dataStructure/栈和队列/队列/链队列/queue.c
@@ -0,0 +1,75 @@
+#include "queue.h"
+
+void InitQueue(pqueue * q)
+{
+ *q = NULL;
+}
+
+bool EmptyQueue(pqueue * qhead)
+{
+ if (!*qhead)
+ return true;
+ return false;
+}
+
+bool EnQueue(pqueue * qtail, ElemType * e)
+{
+ pqueue new = (pqueue) malloc (sizeof(Queue));
+ if (!new)
+ return false;
+ new->data = *e;
+ new->next = NULL;
+
+ if (!*qtail)
+ *qtail = new;
+ else
+ {
+ (*qtail)->next = new;
+ *qtail = new;
+ }
+
+ return true;
+}
+
+bool DeQueue(pqueue * qhead, ElemType * e)
+{
+ pqueue tmp = *qhead;
+ if (!*qhead)
+ return false;
+ *e = tmp->data;
+ *qhead = (*qhead)->next;
+ free(tmp);
+
+ return true;
+}
+
+bool RsQueue (pqueue * qhead)
+{
+ if (EmptyQueue(qhead))
+ return false;
+ pqueue tmp = *qhead;
+
+ while (!*qhead)
+ {
+ tmp = (*qhead)->next;
+ free(*qhead);
+ *qhead = tmp;
+ }
+
+ return true;
+}
+
+bool PrintQueue (pqueue * qhead)
+{
+ if (EmptyQueue(qhead))
+ return false;
+ pqueue tmp = *qhead;
+
+ while (tmp)
+ {
+ printf("%c", tmp->data);
+ tmp = tmp->next;
+ }
+ putchar('\n');
+ return true;
+}
diff --git a/c/dataStructure/栈和队列/队列/链队列/queue.h b/c/dataStructure/栈和队列/队列/链队列/queue.h
new file mode 100755
index 0000000..e444632
--- /dev/null
+++ b/c/dataStructure/栈和队列/队列/链队列/queue.h
@@ -0,0 +1,23 @@
+#ifndef _QUEUE_H
+#define _QUEUE_H
+
+#include<stdio.h>
+#include<stdlib.h>
+#include<stdbool.h>
+
+typedef char ElemType;
+
+typedef struct queue {
+ ElemType data;
+ struct queue * next;
+} Queue;
+typedef Queue * pqueue;
+
+void InitQueue (pqueue * q);
+bool EnQueue (pqueue * qtail, ElemType * e);
+bool DeQueue (pqueue * qhead, ElemType * e);
+bool RsQueue (pqueue * qhead);
+bool EmptyQueue (pqueue * qhead);
+bool PrintQueue (pqueue * qhead);
+
+#endif