data_structures

data structures in C
Log | Files | Refs | Feed

commit bd1ccd3360c4a571ad80942cfdd1a757402a7d41
parent aaa9b305c81c5e52ab7c44e438abf24a41d08f1f
Author: Jenny Doe <tng@soykaf.me>
Date:   Thu, 21 Mar 2019 23:09:55 +0100

added: stack and queue

Diffstat:
Aqueue.h | 15+++++++++++++++
Aqueue_test.c | 48++++++++++++++++++++++++++++++++++++++++++++++++
Astack.h | 15+++++++++++++++
Astack_test.c | 48++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 126 insertions(+), 0 deletions(-)

diff --git a/queue.h b/queue.h @@ -0,0 +1,15 @@ +#pragma once + +#include "list.h" + +typedef List Queue; +typedef ListElmt QueueElmt; + +#define enqueue(list, data) \ + list_ins_next(list, list->tail, data) + +#define dequeue(list, data) \ + list_rem_next(list, NULL, data) + +#define queue_init list_init +#define queue_destroy list_destroy diff --git a/queue_test.c b/queue_test.c @@ -0,0 +1,48 @@ +#include <stdio.h> +#include <stdlib.h> + +#include "list.h" +#include "queue.h" + +int +main(void) +{ + char *t = "abc"; + + Queue *my_queue = malloc(sizeof(Queue)); + if (my_queue == NULL) { + fprintf(stderr, "Failed to allocate memory!\n"); + return (1); + } + + queue_init(my_queue, NULL); + + enqueue(my_queue, &t[0]); /* enqueue 'a' */ + enqueue(my_queue, &t[1]); /* enqueue 'b' */ + enqueue(my_queue, &t[2]); /* enqueue 'c' */ + + /* easier than checking return value */ + if (my_queue->size != 3) { + fprintf(stderr, "Failed to insert some element in my_queue!\n"); + return (1); + } + + void *data; + while (my_queue->size != 0) { + dequeue(my_queue, &data); + + printf("Now processing: '%c'\n", *(char *)data); + } + data = NULL; + + printf("tail=%p data='%c'\n", + my_queue->tail, *(char *)my_queue->tail->data); + + queue_destroy(my_queue); + if (my_queue->size != 0) { + fprintf(stderr, "Failed to destroy my_queue!\n"); + return (1); + } + + return (0); +} diff --git a/stack.h b/stack.h @@ -0,0 +1,15 @@ +#pragma once + +#include "list.h" + +typedef List Stack; +typedef ListElmt StackElmt; + +#define push(list, data) \ + list_ins_next(list, NULL, data) + +#define pop(list, data) \ + list_rem_next(list, NULL, data) + +#define stack_init list_init +#define stack_destroy list_destroy diff --git a/stack_test.c b/stack_test.c @@ -0,0 +1,48 @@ +#include <stdio.h> +#include <stdlib.h> + +#include "list.h" +#include "stack.h" + +int +main(void) +{ + char *t = "abc"; + + Stack *my_stack = malloc(sizeof(Stack)); + if (my_stack == NULL) { + fprintf(stderr, "Failed to allocate memory!\n"); + return (1); + } + + stack_init(my_stack, NULL); + + push(my_stack, &t[0]); /* push 'a' */ + push(my_stack, &t[1]); /* push 'b' */ + push(my_stack, &t[2]); /* push 'c' */ + + /* easier than checking return value */ + if (my_stack->size != 3) { + fprintf(stderr, "Failed to insert some element in my_stack!\n"); + return (1); + } + + void *data; + while (my_stack->size != 0) { + pop(my_stack, &data); + + printf("Now processing: '%c'\n", *(char *)data); + } + data = NULL; + + printf("tail=%p data='%c'\n", + my_stack->tail, *(char *)my_stack->tail->data); + + stack_destroy(my_stack); + if (my_stack->size != 0) { + fprintf(stderr, "Failed to destroy my_stack!\n"); + return (1); + } + + return (0); +}