File tree 4 files changed +133
-25
lines changed
4 files changed +133
-25
lines changed Original file line number Diff line number Diff line change 1
1
#include < cstdlib>
2
2
#include < iostream>
3
3
4
- #include " queue_array.h"
5
- #include " queue_array.cc"
4
+ #include " src/ queue_array.h"
5
+ #include " src/ queue_array.cc"
6
6
7
7
// for stdlib experiments:
8
- // #include <forward_list >
9
- // #include <list >
8
+ #include < queue >
9
+ #include < stack >
10
10
11
11
// void stdlib_forward_list_experiment();
12
12
// void stdlib_doubly_linked_list_experiment();
13
13
14
14
int main (int argc, char *argv[]) {
15
15
16
- // jw::LinkedList<int> ilist{};
17
- // ilist.PushFront(4);
18
- // ilist.PushFront(9);
19
- //
20
- // std::cout << "Size of int list is: " << ilist.Size() << std::endl;
21
- //
22
- // jw::LinkedList<std::string> strlist{};
23
- // strlist.PushFront("string 1");
24
- // strlist.PushFront("another");
25
- // strlist.PushFront("and another");
26
- // std::cout << "Size of string list is: " << strlist.Size() << std::endl;
27
- //
28
- // std::cout << "Hello friends - look in tests directory." << std::endl;
29
- //
30
- // stdlib_forward_list_experiment();
31
- // stdlib_doubly_linked_list_experiment();
32
-
33
16
std::cout << " Here we go." << std::endl;
34
17
35
18
return EXIT_SUCCESS;
Original file line number Diff line number Diff line change
1
+ #include " queue_array.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
+
1
3
#ifndef PROJECT_QUEUE_ARRAY_H
2
4
#define PROJECT_QUEUE_ARRAY_H
3
5
4
- #endif //PROJECT_QUEUE_ARRAY_H
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
- #include < string >
1
+ #include < assert.h >
2
2
#include < iostream>
3
+ #include < string>
3
4
#include " queue_array.cc"
4
5
#include " queue_array.h"
5
6
6
- int main (int argc, char *argv[]) {
7
+ void run_all_tests ();
8
+ void test_empty ();
9
+ void test_full ();
10
+ void test_dequeue ();
7
11
8
- std::cout << " out" << std::endl;
12
+ int main (int argc, char *argv[]) {
13
+ run_all_tests ();
9
14
10
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 ());
11
65
}
You can’t perform that action at this time.
0 commit comments