feat(queue): added queue datastructure with init and destroy function
This commit is contained in:
parent
d8cc4b78a7
commit
3e04e24b1c
4 changed files with 147 additions and 1 deletions
43
Include/Fledasty/Core/Queue.h
Normal file
43
Include/Fledasty/Core/Queue.h
Normal file
|
|
@ -0,0 +1,43 @@
|
||||||
|
/*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*
|
||||||
|
* -----------------------------------------------------------------------------
|
||||||
|
* File: Queue.h
|
||||||
|
* Description:
|
||||||
|
* This file contains the Queue structure and the functions for modifying it.
|
||||||
|
* It includes functions to Push, Pop, Peek and check if empty.
|
||||||
|
*
|
||||||
|
* Author: Mineplay
|
||||||
|
* -----------------------------------------------------------------------------
|
||||||
|
*/
|
||||||
|
#ifndef FLEDASTY_QUEUE
|
||||||
|
#define FLEDASTY_QUEUE
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
#include "../Utils/Error.h"
|
||||||
|
|
||||||
|
typedef struct FledastyQueueNode {
|
||||||
|
void *value;
|
||||||
|
struct FledastyQueueNode *previous, *next;
|
||||||
|
} FledastyQueueNode;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
size_t size;
|
||||||
|
FledastyQueueNode *start, *end;
|
||||||
|
} FledastyQueue;
|
||||||
|
|
||||||
|
FledastyError fledasty_queue_initialize(FledastyQueue *new_queue);
|
||||||
|
FledastyError fledasty_queue_destroy(FledastyQueue *current_queue);
|
||||||
|
|
||||||
|
#endif
|
||||||
32
Include/Fledasty/Utils/Error.h
Normal file
32
Include/Fledasty/Utils/Error.h
Normal file
|
|
@ -0,0 +1,32 @@
|
||||||
|
/*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*
|
||||||
|
* -----------------------------------------------------------------------------
|
||||||
|
* File: Error.h
|
||||||
|
* Description:
|
||||||
|
* This file contains the ErrorCode enum that defines the error codes for the
|
||||||
|
* library.
|
||||||
|
*
|
||||||
|
* Author: Mineplay
|
||||||
|
* -----------------------------------------------------------------------------
|
||||||
|
*/
|
||||||
|
#ifndef FLEDASTY_ERROR
|
||||||
|
#define FLEDASTY_ERROR
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
FLEDASTY_ERROR_NONE = 0,
|
||||||
|
FLEDASTY_ERROR_ALLOCATION = 1,
|
||||||
|
FLEDASTY_ERROR_INVALID_POINTER = 2,
|
||||||
|
} FledastyError;
|
||||||
|
|
||||||
|
#endif
|
||||||
66
Src/Core/Queue.c
Normal file
66
Src/Core/Queue.c
Normal file
|
|
@ -0,0 +1,66 @@
|
||||||
|
/*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*
|
||||||
|
* -----------------------------------------------------------------------------
|
||||||
|
* File: Main.c
|
||||||
|
* Description:
|
||||||
|
* Executes all tests.
|
||||||
|
*
|
||||||
|
* Author: Mineplay
|
||||||
|
* -----------------------------------------------------------------------------
|
||||||
|
*/
|
||||||
|
#include "../../Include/Fledasty/Core/Queue.h"
|
||||||
|
|
||||||
|
#include <Hallocy/Core/Allocator.h>
|
||||||
|
#include <Hallocy/Utils/Error.h>
|
||||||
|
|
||||||
|
FledastyError fledasty_queue_initialize(FledastyQueue *new_queue) {
|
||||||
|
if (new_queue == NULL) {
|
||||||
|
return FLEDASTY_ERROR_INVALID_POINTER;
|
||||||
|
}
|
||||||
|
|
||||||
|
new_queue->size = 0;
|
||||||
|
new_queue->start = NULL;
|
||||||
|
new_queue->end = NULL;
|
||||||
|
|
||||||
|
return FLEDASTY_ERROR_NONE;
|
||||||
|
}
|
||||||
|
|
||||||
|
FledastyError fledasty_queue_destroy(FledastyQueue *current_queue) {
|
||||||
|
if (current_queue == NULL) {
|
||||||
|
return FLEDASTY_ERROR_INVALID_POINTER;
|
||||||
|
}
|
||||||
|
|
||||||
|
FledastyQueueNode *previousNode = NULL;
|
||||||
|
FledastyQueueNode *current_node = current_queue->start;
|
||||||
|
while (current_node != NULL) {
|
||||||
|
previousNode = current_node;
|
||||||
|
current_node = current_node->next;
|
||||||
|
|
||||||
|
HallocyError error = hallocy_free(previousNode->value);
|
||||||
|
if (error != HALLOCY_ERROR_NONE) {
|
||||||
|
return FLEDASTY_ERROR_ALLOCATION;
|
||||||
|
}
|
||||||
|
|
||||||
|
error = hallocy_free(previousNode);
|
||||||
|
if (error != HALLOCY_ERROR_NONE) {
|
||||||
|
return FLEDASTY_ERROR_ALLOCATION;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
current_queue->size = 0;
|
||||||
|
current_queue->start = NULL;
|
||||||
|
current_queue->end = NULL;
|
||||||
|
|
||||||
|
return FLEDASTY_ERROR_NONE;
|
||||||
|
}
|
||||||
|
|
@ -20,8 +20,13 @@
|
||||||
* -----------------------------------------------------------------------------
|
* -----------------------------------------------------------------------------
|
||||||
*/
|
*/
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <Fledasty/Core/Queue.h>
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
printf("Test\n");
|
FledastyQueue test_queue;
|
||||||
|
fledasty_queue_initialize(&test_queue);
|
||||||
|
fledasty_queue_destroy(&test_queue);
|
||||||
|
|
||||||
|
printf("Done\n");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
Loading…
Add table
Reference in a new issue