summaryrefslogtreecommitdiff
path: root/src/01/01b/stack.c
diff options
context:
space:
mode:
authormo khan <mo.khan@gmail.com>2020-07-04 13:10:46 -0600
committermo khan <mo.khan@gmail.com>2020-07-04 13:10:46 -0600
commit05f5d5496957939bee3cb360bea5b96da98b4a00 (patch)
treea4d5bbb2cabfae7544ae9960650508a428fad93b /src/01/01b/stack.c
parent24700ade3ae9ff643b0c092ff617fb065d9f19f1 (diff)
Split stack_test.c
Diffstat (limited to 'src/01/01b/stack.c')
-rw-r--r--src/01/01b/stack.c99
1 files changed, 99 insertions, 0 deletions
diff --git a/src/01/01b/stack.c b/src/01/01b/stack.c
new file mode 100644
index 0000000..beffd64
--- /dev/null
+++ b/src/01/01b/stack.c
@@ -0,0 +1,99 @@
+#include "stack.h"
+#include <stdio.h>
+#include <stdlib.h>
+
+static Queue *newQueue(){
+ Queue *queue = malloc(sizeof(Queue));
+ queue->size = 0;
+ queue->head = NULL;
+ return queue;
+}
+
+Stack *initialize() {
+ Stack *stack = malloc(sizeof(Stack));
+ stack->q1 = newQueue();
+ return stack;
+}
+
+Node *newNode(int data) {
+ Node *node = malloc(sizeof(Node));
+ node->data = data;
+ node->next = NULL;
+ return node;
+}
+
+void enqueue(Queue *q, int data) {
+ Node *node = newNode(data);
+ q->size++;
+
+ if (q->head == NULL) {
+ q->head = node;
+ } else {
+ Node *tail = q->head;
+
+ while (tail->next != NULL)
+ tail = tail->next;
+ tail->next = node;
+ }
+}
+
+int dequeue(Queue *q) {
+ if (q->head == NULL) return -1;
+
+ Node *node = q->head;
+ int data = node->data;
+
+ q->head = node->next;
+ free(node);
+ return data;
+}
+
+// The running time is linear time.
+void push(Stack *stack, int data) {
+ enqueue(stack->q1, data);
+}
+
+// The running time is linear time.
+int pop(Stack *stack) {
+ int count = stack->q1->size - 1;
+ Queue *tmp = newQueue();
+
+ for (int i = 0; i < count; i++)
+ enqueue(tmp, dequeue(stack->q1));
+
+ int data = dequeue(stack->q1);
+ free(stack->q1);
+ stack->q1 = tmp;
+ return data;
+}
+
+int size(Stack *stack) {
+ return stack->q1->size;
+}
+
+void destroy(Stack *stack) {
+ int count = size(stack);
+
+ for (int i = 0; i < count; i++)
+ pop(stack);
+
+ free(stack->q1);
+ free(stack);
+}
+
+static void inspect(Queue *q) {
+ Node *tmp = q->head;
+
+ if (q->size == 0) {
+ printf("[]\n");
+ return;
+ }
+
+ printf("[");
+ for (int i = 0; i < q->size; i++) {
+ printf("%d,", tmp->data);
+ tmp = tmp->next;
+ }
+ printf("\b]\n");
+}
+