Skip to content

Commit 5637588

Browse files
Added queue and stack, some enhancements on array and llist (#7)
1 parent 2ddc5f8 commit 5637588

File tree

20 files changed

+505
-985
lines changed

20 files changed

+505
-985
lines changed

src/chapter/02_datastructures/array/array.c

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ void array_init(Array *a, int capacity)
2525
{
2626
a->capacity = capacity;
2727
a->size = 0;
28-
a->elements = (char*) malloc(capacity);
28+
a->elements = (char *)malloc(capacity);
2929
}
3030

31-
void array_resize(Array* a, int new_capacity)
31+
void array_resize(Array *a, int new_capacity)
3232
{
3333
if (new_capacity != a->capacity)
3434
{
3535
// Allocate new memory
36-
char* new_elements = (char*)malloc(new_capacity);
36+
char *new_elements = (char *)malloc(new_capacity);
3737
// Copy data from old to new memory
3838
memcpy(new_elements, a->elements, a->size);
3939
// // "Manual way":
@@ -68,9 +68,9 @@ void array_insert(Array *a, int idx, char value)
6868
a->size++;
6969
}
7070

71-
char* array_at(const Array *a, int idx)
71+
char *array_at(const Array *a, int idx)
7272
{
73-
if((idx > a->capacity) || (idx > a->size))
73+
if ((idx > a->capacity) || (idx > a->size))
7474
{
7575
printf("Out of bounds.");
7676
return "\0";
@@ -79,17 +79,17 @@ char* array_at(const Array *a, int idx)
7979
return &(a->elements[idx]);
8080
}
8181

82-
void array_push_back(Array* a, char value)
82+
void array_push_back(Array *a, char value)
8383
{
8484
array_insert(a, a->size, value);
8585
}
8686

87-
char array_pop_back(Array* a)
87+
char array_pop_back(Array *a)
8888
{
8989
return array_remove(a, a->size - 1);
9090
}
9191

92-
char array_remove(Array* a, int idx)
92+
char array_remove(Array *a, int idx)
9393
{
9494
// Save element to be removed
9595
char temp = a->elements[idx];
@@ -104,14 +104,14 @@ char array_remove(Array* a, int idx)
104104
// Shrink if needed
105105
if (a->size <= a->capacity / 4)
106106
{
107-
int new_capacity = (a->capacity / 2) + 1 ;
107+
int new_capacity = (a->capacity / 2) + 1;
108108
array_resize(a, new_capacity);
109109
}
110110

111111
return temp;
112112
}
113113

114-
int array_find(Array* a, char c)
114+
int array_find(Array *a, char c)
115115
{
116116
for (int i = 0; i < a->capacity; i++)
117117
{
@@ -129,3 +129,36 @@ void array_destroy(Array *a)
129129
a->size = 0;
130130
free(a->elements);
131131
}
132+
133+
void array_print_graphviz(const Array *a)
134+
{
135+
char separator = '|';
136+
137+
printf("graph array\n");
138+
printf("{\n");
139+
140+
printf(" Array [ shape = record, label = \"{");
141+
for (int i = 0; i < a->capacity; i++)
142+
{
143+
char sep = i == (a->capacity - 1) ? ' ' : separator;
144+
if (i < a->size)
145+
{
146+
printf("%c %c ", a->elements[i], sep);
147+
}
148+
else
149+
{
150+
printf(" %c", sep);
151+
}
152+
}
153+
printf("}\"] ;\n");
154+
155+
printf(" notes [ shape = record, color = white, label = \"{");
156+
for (int i = 0; i < a->capacity; i++)
157+
{
158+
char sep = i == (a->capacity - 1) ? ' ' : separator;
159+
printf("%d %c ", i, sep);
160+
}
161+
printf("}\" ];\n");
162+
163+
printf("}\n\n");
164+
}

src/chapter/02_datastructures/array/array.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,10 @@ int array_find(Array *a, char c);
8989
*/
9090
void array_destroy(Array *a);
9191

92+
/**
93+
* Prints content of an Array to stdout in Graphviz format.
94+
* @param a the array to be printed.
95+
*/
96+
void array_print_graphviz(const Array *a);
97+
9298
#endif // ARRAY_H_

src/chapter/02_datastructures/linkedlist/linkedlist.c

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,4 +244,55 @@ node *linkedlist_find_previous(LinkedList *l, const node *item)
244244
previous = previous->next;
245245
}
246246
return 0;
247-
}
247+
}
248+
249+
void linkedlist_print_graphviz(const LinkedList *l)
250+
{
251+
printf("digraph g {\n");
252+
printf("graph [\n");
253+
printf(" rankdir = \"LR\"\n");
254+
printf("];\n");
255+
256+
printf("\"%p\" [\n", (void *)l->head);
257+
printf(" label = \"<f0> %p | <f1> Head\"\n", (void *)l->head);
258+
printf(" style = \"filled\"\n");
259+
printf(" fillcolor = \"#9dc4c4\"\n");
260+
printf(" shape = \"record\"\n");
261+
printf("];\n");
262+
263+
// Nodes
264+
{
265+
node *curPtr = l->head->next;
266+
while (curPtr != 0 && curPtr != l->tail)
267+
{
268+
printf("\"%p\" [\n", (void *)curPtr);
269+
printf(" label = \"<f0> %p | <f1> %c\"\n", (void *)curPtr, *(curPtr->value));
270+
printf(" shape = \"record\"");
271+
printf("];\n");
272+
273+
curPtr = curPtr->next;
274+
}
275+
}
276+
277+
printf("\"%p\" [\n", (void *)l->tail);
278+
printf(" label = \"<f0> %p | <f1> Tail\"\n", (void *)l->tail);
279+
printf(" style = \"filled\"\n");
280+
printf(" fillcolor = \"#9dc4c4\"\n");
281+
printf(" shape = \"record\"\n");
282+
printf("];\n");
283+
284+
// Links
285+
{
286+
node *curPtr = l->head;
287+
printf("\"%p\" -> \"%p\"\n", (void *)curPtr, (void *)curPtr->next);
288+
curPtr = curPtr->next;
289+
290+
while (curPtr != 0 && curPtr != l->tail)
291+
{
292+
printf("\"%p\" -> \"%p\"\n", (void *)curPtr, (void *)curPtr->next);
293+
curPtr = curPtr->next;
294+
}
295+
}
296+
printf("}\n");
297+
printf("\n");
298+
}

src/chapter/02_datastructures/linkedlist/linkedlist.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,13 @@ size_t linkedlist_size(const LinkedList *l);
6868
*/
6969
void linkedlist_print(const LinkedList *l);
7070

71+
/**
72+
* Prints all LinkedList elements to stdout in a Graphviz format.
73+
*
74+
* @param l LinkedList to be printed.
75+
*/
76+
void linkedlist_print_graphviz(const LinkedList *l);
77+
7178
/**
7279
* Empty given LinkedList.
7380
* @param l LinkedList list to operate on.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
add_library(queue queue.cpp)
2+
target_link_libraries(queue linkedlist)
3+
set_target_properties(queue PROPERTIES PUBLIC_HEADER "queue.h")
4+
install(TARGETS queue PUBLIC_HEADER DESTINATION include/queue)
5+
6+
add_executable(queue_test queue_test.cpp)
7+
target_link_libraries(queue_test queue)
8+
install(TARGETS queue_test DESTINATION bin/c02)
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#include <iostream>
2+
#include <string>
3+
#include <sstream>
4+
5+
#include "queue.h"
6+
extern "C"
7+
{
8+
#include "linkedlist/linkedlist.h"
9+
}
10+
11+
// avoid prefixing std::
12+
using namespace std;
13+
14+
namespace fom
15+
{
16+
namespace AuD
17+
{
18+
Queue::Queue()
19+
{
20+
this->l = new LinkedList;
21+
linkedlist_init(this->l);
22+
}
23+
24+
Queue::~Queue()
25+
{
26+
linkedlist_destroy(this->l);
27+
delete (this->l);
28+
this->l = 0;
29+
}
30+
31+
void Queue::enqueue(const char element)
32+
{
33+
linkedlist_push_back(this->l, element);
34+
}
35+
36+
char Queue::dequeue()
37+
{
38+
// ... pretty much the only difference to a Stack:
39+
// Return the "oldest" element
40+
return linkedlist_pop_front(this->l);
41+
}
42+
43+
const std::string Queue::toString()
44+
{
45+
std::stringstream returnStr;
46+
for (size_t i = 0; i < linkedlist_size(this->l); i++)
47+
{
48+
const char c = *((linkedlist_at(this->l, i))->value);
49+
50+
returnStr << "[" << c << "]"; // << std::endl;
51+
}
52+
53+
return returnStr.str();
54+
}
55+
56+
std::string Queue::toGraphviz() const
57+
{
58+
std::stringstream returnStr;
59+
60+
returnStr << ("digraph g {\n");
61+
returnStr << ("graph [\n");
62+
returnStr << (" rankdir = \"LR\"\n");
63+
returnStr << ("];\n");
64+
65+
for (size_t i = 0; i < linkedlist_size(this->l); i++)
66+
{
67+
node* n = linkedlist_at(this->l, i);
68+
const char c = *(n->value);
69+
70+
returnStr << "\"" << (void *)n << "\" [\n";
71+
returnStr << " label = \"<f0> " << c << "\"\n";
72+
returnStr << " shape = \"record\"\n";
73+
returnStr << ("];\n");
74+
}
75+
76+
for (size_t i = 0; i < linkedlist_size(this->l) -1; i++)
77+
{
78+
node* n = linkedlist_at(this->l, i);
79+
returnStr << "\"" << (void *)n << "\" -> \"" << (void *)n->next << "\"\n";
80+
}
81+
82+
returnStr << ("}\n") << std::endl;
83+
return returnStr.str();
84+
}
85+
}
86+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#ifndef QUEUE_H
2+
#define QUEUE_H
3+
4+
struct LinkedList;
5+
6+
namespace fom
7+
{
8+
namespace AuD
9+
{
10+
/**
11+
* Queue implementation based on LinkedList.
12+
*/
13+
class Queue
14+
{
15+
public:
16+
/** Default constructor. */
17+
Queue();
18+
/** Destructor, free's resources. */
19+
~Queue();
20+
21+
/** Copy constructur. Not implemented here. */
22+
Queue(const Queue&) = delete;
23+
/** Assignment operator. Not implemented here. */
24+
Queue& operator=(const Queue& other) = delete;
25+
26+
/**
27+
* Pushes an element to the Queue.
28+
*
29+
* @param element the new element.
30+
*/
31+
void enqueue(const char element);
32+
33+
/**
34+
* Returns the top element from the Queue
35+
* (and deletes it from the Queue).
36+
*
37+
* @return the top element.
38+
*/
39+
char dequeue();
40+
41+
/**
42+
* Helper to output a Queue.
43+
*
44+
* @return a String representation of the contents.
45+
*/
46+
const std::string toString();
47+
48+
/**
49+
* Convert the Queue to a string in a Graphviz format.
50+
* @return a string in Graphviz format representing the data inside.
51+
*/
52+
std::string toGraphviz() const;
53+
54+
private:
55+
// Clang does not detect we will be utilizing l in implementation.
56+
#ifdef __clang__
57+
#pragma clang diagnostic push
58+
#pragma clang diagnostic ignored "-Wunused-private-field"
59+
#endif
60+
LinkedList *l;
61+
#ifdef __clang__
62+
#pragma clang diagnostic pop
63+
#endif
64+
};
65+
}
66+
}
67+
68+
#endif // QUEUE_H
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <iostream>
2+
#include <string>
3+
4+
#include "queue.h"
5+
6+
int main()
7+
{
8+
fom::AuD::Queue s;
9+
10+
std::cout << "## --- Enqueue some values to the Queue" << std::endl;
11+
int ansi_start = 97; // 65 = 'A' | 97 = 'a | 48 = '0'
12+
for (int i = ansi_start; i < 10 + ansi_start; ++i)
13+
{
14+
s.enqueue((char) i);
15+
}
16+
std::cout << s.toString() << std::endl;
17+
18+
std::cout << "## --- Dequeue & Print" << std::endl;
19+
std::cout << "Dequeue: " << s.dequeue() << std::endl;
20+
std::cout << s.toString() << std::endl;
21+
std::cout << "Dequeue: " << s.dequeue() << std::endl;
22+
std::cout << s.toString() << std::endl;
23+
std::cout << "Dequeue: " << s.dequeue() << std::endl;
24+
std::cout << s.toString() << std::endl;
25+
std::cout << "Dequeue: " << s.dequeue() << std::endl;
26+
std::cout << s.toString() << std::endl;
27+
std::cout << "Dequeue: " << s.dequeue() << std::endl;
28+
std::cout << s.toString() << std::endl;
29+
30+
return 0;
31+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
add_library(stack stack.cpp)
2+
target_link_libraries(stack linkedlist)
3+
set_target_properties(stack PROPERTIES PUBLIC_HEADER "stack.h")
4+
install(TARGETS stack PUBLIC_HEADER DESTINATION include/stack)
5+
6+
add_executable(stack_test stack_test.cpp)
7+
target_link_libraries(stack_test stack)
8+
install(TARGETS stack_test DESTINATION bin/c02)

0 commit comments

Comments
 (0)