File tree 8 files changed +194
-8
lines changed
8 files changed +194
-8
lines changed Original file line number Diff line number Diff line change 4
4
#include " src/queue_array.h"
5
5
#include " src/queue_array.cc"
6
6
7
- // for stdlib experiments:
8
- #include < queue>
9
- #include < stack>
10
-
11
- // void stdlib_forward_list_experiment();
12
- // void stdlib_doubly_linked_list_experiment();
13
-
14
7
int main (int argc, char *argv[]) {
15
8
16
- std::cout << " Here we go ." << std::endl;
9
+ std::cout << " Nothing to see here. Look in tests directory ." << std::endl;
17
10
18
11
return EXIT_SUCCESS;
19
12
}
Original file line number Diff line number Diff line change
1
+ cmake_minimum_required (VERSION 3.5)
2
+ project (queue_llist_proj)
3
+
4
+ add_definitions (-std=c++11)
5
+ add_definitions (-Werror) # stop compile on warning
6
+
7
+ set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D_GLIBCXX_DEBUG" )
8
+
9
+ set (SOURCE_FILES main.cc)
10
+ add_executable (queue_llist_run ${SOURCE_FILES} )
11
+
12
+ include_directories (src)
13
+
14
+ add_subdirectory (src)
15
+ add_subdirectory (tests)
16
+
17
+ target_link_libraries (queue_llist_run queue_llist_lib)
Original file line number Diff line number Diff line change
1
+ #include < cstdlib>
2
+ #include < iostream>
3
+
4
+ #include " src/queue_linked_list.h"
5
+ #include " src/queue_linked_list.cc"
6
+
7
+ // for stdlib experiments:
8
+ #include < queue>
9
+ #include < stack>
10
+
11
+ // void stdlib_forward_list_experiment();
12
+ // void stdlib_doubly_linked_list_experiment();
13
+
14
+ int main (int argc, char *argv[]) {
15
+
16
+ std::cout << " Here we go." << std::endl;
17
+
18
+ return EXIT_SUCCESS;
19
+ }
Original file line number Diff line number Diff line change
1
+ cmake_minimum_required (VERSION 3.5)
2
+ project (queue_llist_lib)
3
+
4
+ add_definitions (-std=c++11)
5
+
6
+ set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -D_GLIBCXX_DEBUG" )
7
+
8
+ set (HEADER_FILES queue_linked_list.h)
9
+
10
+ set (SOURCE_FILES queue_linked_list.cc)
11
+
12
+ add_library (queue_llist_lib STATIC ${SOURCE_FILES} ${HEADER_FILES} )
Original file line number Diff line number Diff line change
1
+ #include " queue_linked_list.h"
2
+
3
+ namespace jw {
4
+
5
+ template <class T >
6
+ void Queue<T>::Enqueue(T value) {
7
+ if (Full ()) {
8
+ std::cerr << " Queue is full. Unable to enqueue." << std::endl;
9
+ exit (EXIT_FAILURE);
10
+ }
11
+
12
+ data_[insert_] = value;
13
+ insert_++;
14
+ }
15
+
16
+ template <class T >
17
+ const T Queue<T>::Dequeue() {
18
+ if (Empty ()) {
19
+ std::cerr << " Queue is empty. Cannot dequeue." << std::endl;
20
+ exit (EXIT_FAILURE);
21
+ }
22
+
23
+ T value = data_[pop_];
24
+ data_[pop_] = 0 ;
25
+ pop_++;
26
+
27
+ return value;
28
+ }
29
+
30
+ template <class T >
31
+ bool Queue<T>::Empty() const {
32
+ return insert_ == pop_;
33
+ }
34
+
35
+ template <class T >
36
+ bool Queue<T>::Full() const {
37
+ return pop_ == (insert_ + 1 ) % kQueuePositions ;
38
+ }
39
+
40
+ } // namespace jw
Original file line number Diff line number Diff line change
1
+ #include < iostream>
2
+
3
+ #ifndef PROJECT_QUEUE_ARRAY_H
4
+ #define PROJECT_QUEUE_ARRAY_H
5
+
6
+ namespace jw {
7
+
8
+ template <class T >
9
+ class Queue {
10
+ static const int kQueueCapacity = 5 ;
11
+ static const int kQueuePositions = kQueueCapacity + 1 ;
12
+
13
+ public:
14
+ explicit Queue () : insert_(0 ), pop_(0 ) {}
15
+ ~Queue () = default ;
16
+ Queue (const Queue &) = delete ;
17
+ Queue &operator =(const Queue &) = delete ;
18
+ // Adds value to queue.
19
+ void Enqueue (T value);
20
+ // Removes least recently added item from queue, returning its value.
21
+ const T Dequeue ();
22
+ // Returns true if queue is empty.
23
+ bool Empty () const ;
24
+ // Returns true if queue cannot accept another enqueue.
25
+ bool Full () const ;
26
+
27
+ private:
28
+ int insert_;
29
+ int pop_;
30
+ T data_[kQueuePositions ];
31
+ };
32
+
33
+ } // namespace jw
34
+
35
+ #endif // PROJECT_QUEUE_ARRAY_H
Original file line number Diff line number Diff line change
1
+ project (queue_array_tests)
2
+
3
+ add_executable (queue_array_tests queue_array_tests.cc)
4
+
5
+ target_link_libraries (queue_array_tests queue_array_lib)
Original file line number Diff line number Diff line change
1
+ #include < assert.h>
2
+ #include < iostream>
3
+ #include < string>
4
+ #include " queue_array.cc"
5
+ #include " queue_array.h"
6
+
7
+ void run_all_tests ();
8
+ void test_empty ();
9
+ void test_full ();
10
+ void test_dequeue ();
11
+
12
+ int main (int argc, char *argv[]) {
13
+ run_all_tests ();
14
+
15
+ return EXIT_SUCCESS;
16
+ }
17
+
18
+ void run_all_tests () {
19
+ test_empty ();
20
+ test_full ();
21
+ test_dequeue ();
22
+ }
23
+
24
+ void test_empty () {
25
+ jw::Queue<int > q;
26
+
27
+ assert (q.Empty ());
28
+
29
+ q.Enqueue (3 );
30
+
31
+ assert (!q.Empty ());
32
+ }
33
+
34
+ void test_full () {
35
+ jw::Queue<double > q;
36
+
37
+ q.Enqueue (12.3 );
38
+ q.Enqueue (4.235 );
39
+ q.Enqueue (5.4 );
40
+ q.Enqueue (7.0 );
41
+ q.Enqueue (885314.32214 );
42
+
43
+ assert (q.Full ());
44
+ }
45
+
46
+ void test_dequeue () {
47
+ jw::Queue<int > q;
48
+
49
+ q.Enqueue (100 );
50
+ q.Enqueue (200 );
51
+ assert (q.Dequeue () == 100 );
52
+ q.Enqueue (300 );
53
+ assert (q.Dequeue () == 200 );
54
+ q.Enqueue (400 );
55
+ q.Enqueue (500 );
56
+ q.Enqueue (600 );
57
+ q.Enqueue (700 );
58
+ assert (q.Dequeue () == 300 );
59
+ assert (q.Dequeue () == 400 );
60
+ assert (q.Dequeue () == 500 );
61
+ assert (q.Dequeue () == 600 );
62
+ assert (q.Dequeue () == 700 );
63
+
64
+ assert (q.Empty ());
65
+ }
You can’t perform that action at this time.
0 commit comments