-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtdd_code.cpp
133 lines (120 loc) · 3.95 KB
/
tdd_code.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
//======== Copyright (c) 2021, FIT VUT Brno, All rights reserved. ============//
//
// Purpose: Test Driven Development - priority queue code
//
// $NoKeywords: $ivs_project_1 $tdd_code.cpp
// $Author: RICHARD KOCIAN <xkocia19@stud.fit.vutbr.cz>
// $Date: $2022-03-10
//============================================================================//
/**
* @file tdd_code.cpp
* @author RICHARD KOCIAN
*
* @brief Implementace metod tridy prioritni fronty.
*/
#include <stdlib.h>
#include <stdio.h>
#include "tdd_code.h"
//============================================================================//
// ** ZDE DOPLNTE IMPLEMENTACI **
//
// Zde doplnte implementaci verejneho rozhrani prioritni fronty (Priority Queue)
// 1. Verejne rozhrani fronty specifikovane v: tdd_code.h (sekce "public:")
// - Konstruktor (PriorityQueue()), Destruktor (~PriorityQueue())
// - Metody Insert/Remove/Find/GetHead ...
// - Pripadne vase metody definovane v tdd_code.h (sekce "protected:")
//
// Cilem je dosahnout plne funkcni implementace prioritni fronty implementovane
// pomoci tzv. "singly linked list", ktera bude splnovat dodane testy
// (tdd_tests.cpp).
//============================================================================//
PriorityQueue::PriorityQueue() {
m_pHead = nullptr;
}
PriorityQueue::~PriorityQueue() {
while (m_pHead != nullptr) {
Remove(m_pHead->value);
}
}
void PriorityQueue::Insert(int value) {
auto newElement = new Element_t;
if (m_pHead != nullptr) {
if (m_pHead->value <= value) {
newElement->pNext = m_pHead;
newElement->value = value;
m_pHead = newElement;
} else {
auto elementPointer = m_pHead->pNext;
auto previousElement = m_pHead;
while (elementPointer != nullptr) {
if (elementPointer->value <= value) {
newElement->pNext = elementPointer;
newElement->value = value;
previousElement->pNext = newElement;
break;
} else {
previousElement = elementPointer;
elementPointer = elementPointer->pNext;
}
}
if (elementPointer == nullptr) {
newElement->pNext = nullptr;
newElement->value = value;
previousElement->pNext = newElement;
}
}
} else {
newElement->pNext = nullptr;
newElement->value = value;
//Element_t newElement{nullptr, value};
m_pHead = newElement;
}
}
bool PriorityQueue::Remove(int value) {
if (m_pHead != nullptr) {
auto pointer = m_pHead;
if (m_pHead->value == value) {
m_pHead = pointer->pNext;
delete pointer;
return true;
} else {
pointer = pointer->pNext;
auto previousElement = m_pHead;
while (pointer!= nullptr) {
if (pointer->value == value) {
previousElement->pNext = pointer->pNext;
delete pointer;
return true;
} else {
previousElement = previousElement->pNext;
pointer = pointer->pNext;
}
}
}
}
return false;
}
PriorityQueue::Element_t *PriorityQueue::Find(int value) {
auto *elementPointer = m_pHead;
while (elementPointer != nullptr) {
if (elementPointer->value == value) {
return elementPointer;
} else {
elementPointer = elementPointer->pNext;
}
}
return NULL;
}
size_t PriorityQueue::Length() {
auto elementPointer = m_pHead;
size_t counter = 0;
while (elementPointer != nullptr) {
counter++;
elementPointer = elementPointer->pNext;
}
return counter;
}
PriorityQueue::Element_t *PriorityQueue::GetHead() {
return m_pHead;
}
/*** Konec souboru tdd_code.cpp ***/