Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added linked list implementation of queue.
- Loading branch information
Showing
7 changed files
with
179 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
cmake_minimum_required(VERSION 3.5) | ||
project(queue_linked_list_proj) | ||
|
||
set(SOURCE_FILES | ||
main.c | ||
) | ||
add_executable(queue_linked_list ${SOURCE_FILES}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#include <stdlib.h> | ||
#include "queue.h" | ||
#include "queue.c" | ||
#include "tests_queue_linked_list.h" | ||
#include "tests_queue_linked_list.c" | ||
|
||
int main(int argc, char *argv[]) { | ||
|
||
run_all_tests(); | ||
|
||
return EXIT_SUCCESS; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
queue * create_queue() { | ||
queue *q = malloc(sizeof(node)); | ||
check_address(q); | ||
|
||
q->head = 0; | ||
q->tail = 0; | ||
|
||
return q; | ||
} | ||
|
||
void enqueue(queue *q, int value) { | ||
node *new_node = malloc(sizeof(node)); | ||
check_address(new_node); | ||
|
||
new_node->value = value; | ||
new_node->next = 0; | ||
|
||
if (q->tail) { | ||
q->tail->next = new_node; | ||
} | ||
q->tail = new_node; | ||
|
||
if (q->head == 0) { | ||
q->head = new_node; | ||
} | ||
} | ||
|
||
const int dequeue(queue *q) { | ||
|
||
if (empty(q)) { | ||
printf("Unable to dequeue. Queue is empty.\n"); | ||
exit(EXIT_FAILURE); | ||
} | ||
|
||
int value = q->head->value; | ||
|
||
node *temp = q->head; | ||
|
||
if (q->tail == q->head) { | ||
q->tail = 0; | ||
} | ||
|
||
q->head = q->head->next; | ||
free(temp); | ||
|
||
return value; | ||
} | ||
|
||
bool empty(queue *q) { | ||
return q->head == 0; | ||
} | ||
|
||
void print_debug(queue *q) { | ||
printf("Queue contents: "); | ||
|
||
node *current = q->head; | ||
|
||
while (current) { | ||
printf("%d < ", current->value); | ||
current = current->next; | ||
} | ||
|
||
printf("\n"); | ||
} | ||
|
||
void check_address(void *addr) { | ||
if (addr == 0) { | ||
printf("Unable to allocate more memory.\n"); | ||
exit(EXIT_FAILURE); | ||
} | ||
} | ||
|
||
void destroy_queue(queue *q) { | ||
|
||
node *current = q->head; | ||
node *temp = q->head; | ||
|
||
while (current) { | ||
temp = current; | ||
current = current->next; | ||
free(temp); | ||
} | ||
|
||
free(q); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#include <stdbool.h> | ||
#include <stdio.h> | ||
|
||
#ifndef PROJECT_QUEUE_LINKED_LIST_H | ||
#define PROJECT_QUEUE_LINKED_LIST_H | ||
|
||
typedef struct Node { | ||
int value; | ||
struct Node *next; | ||
} node; | ||
|
||
typedef struct Queue { | ||
node *head; | ||
node *tail; | ||
} queue; | ||
|
||
// Creates new queue | ||
queue * create_queue(); | ||
// Adds value at end of available storage | ||
void enqueue(queue *q, int value); | ||
// Returns value of least recently added element and removes it | ||
const int dequeue(queue *q); | ||
// Returns true if queue is empty | ||
bool empty(queue *q); | ||
// Prints contents of queue, starting with least recently added item first | ||
void print_debug(queue *q); | ||
// Checks address of allocated memory and exits if allocation failed | ||
void check_address(void *addr); | ||
// Free up memory used by queue | ||
void destroy_queue(queue *q); | ||
|
||
#endif //PROJECT_QUEUE_LINKED_LIST_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
void run_all_tests() { | ||
test_empty(); | ||
test_all(); | ||
} | ||
|
||
void test_empty() { | ||
queue *q = create_queue(); | ||
|
||
assert(empty(q)); | ||
|
||
destroy_queue(q); | ||
} | ||
|
||
void test_all() { | ||
queue *q = create_queue(); | ||
|
||
enqueue(q, 100); | ||
assert(empty(q) == false); | ||
assert(dequeue(q) == 100); | ||
enqueue(q, 200); | ||
enqueue(q, 300); | ||
enqueue(q, 400); | ||
assert(dequeue(q) == 200); | ||
assert(dequeue(q) == 300); | ||
assert(dequeue(q) == 400); | ||
|
||
assert(empty(q)); | ||
|
||
destroy_queue(q); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#include <stdio.h> | ||
#include <assert.h> | ||
|
||
#ifndef PROJECT_TESTS_QUEUE_LINKED_LIST_H | ||
#define PROJECT_TESTS_QUEUE_LINKED_LIST_H | ||
|
||
void run_all_tests(); | ||
|
||
void test_empty(); | ||
void test_all(); | ||
|
||
#endif //PROJECT_TESTS_QUEUE_LINKED_LIST_H |