Skip to content

Commit 1588a8c

Browse files
committed
Update Queue
1 parent 172f474 commit 1588a8c

File tree

7 files changed

+131
-15
lines changed

7 files changed

+131
-15
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
strategy:
1717
matrix:
1818
# Add your projects from 'data-structures'
19-
project: [LinkedList, DoublyLinkedList]
19+
project: [LinkedList, DoublyLinkedList, Queue]
2020

2121
steps:
2222
# Step 1: Checkout the repository

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ Remember that each data has its own trade-offs. And you need to pay attention mo
2121

2222
`B` - Beginner, `A` - Advanced
2323

24-
* `B` [Linked List](data-structures/linked-list)
25-
* `B` [Doubly Linked List](data-structures/doubly-linked-list)
24+
* `B` [Linked List](data-structures/LinkedList)
25+
* `B` [Doubly Linked List](data-structures/DoublyLinkedList)
26+
* `B` [Queue](data-structures/Queue)
2627

2728
## Algorithms
2829

README.zh-CN.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818

1919
`B` - 初学者, `A` - 进阶
2020

21-
* `B` [链表](data-structures/linked-list)
22-
* `B` [双链表](data-structures/doubly-linked-list)
21+
* `B` [单链表](data-structures/LinkedList)
22+
* `B` [双链表](data-structures/DoublyLinkedList)
23+
* `B` [队列](data-structures/Queue)
24+
2325

2426
## 算法
2527

data-structures/Queue/CMakeLists.txt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,8 @@ include_directories(include)
1414
# Output executables to the 'bin' directory
1515
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/../bin)
1616

17-
# Add the main library (LinkedList)
18-
add_library(Queue src/Queue.cpp)
19-
2017
# Add the test executable
21-
add_executable(test_queue __test__/test_Queue.cpp)
18+
add_executable(test_Queue __test__/test_Queue.cpp)
2219

2320
# Link the LinkedList library to the test executable
24-
target_link_libraries(test_queue Queue)
21+
target_link_libraries(test_Queue)
Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,50 @@
1-
#include "../include/Queue.h"
1+
#include "../include/Queue.h"
2+
#include <cassert>
3+
#include <iostream>
4+
5+
int main(int argc, char* argv[]){
6+
Queue<int> que;
7+
assert(que.empty() == true);
8+
assert(que.size() == 0);
9+
std::cout << "Initial test PASSED!" << std::endl;
10+
try{
11+
que.pop();
12+
std::cerr << "pop() throw exception FAILED!" << std::endl;
13+
} catch(const std::underflow_error &e){
14+
std::cout << "pop() throw an exception PASSED! " << e.what() << std::endl;
15+
}
16+
que.push(37);
17+
assert(que.empty() == false);
18+
assert(que.size() == 1);
19+
assert(que.front() == que.back());
20+
que.pop();
21+
assert(que.empty() == true);
22+
assert(que.size() == 0);
23+
try{
24+
que.front();
25+
std::cerr << "front() throw exception FAILED!" << std::endl;
26+
} catch(const std::underflow_error &e){
27+
std::cout << "front() throw an exception PASSED! " << e.what() << std::endl;
28+
}
29+
try{
30+
que.back();
31+
std::cerr << "back() throw exception FAILED!" << std::endl;
32+
} catch(const std::underflow_error &e){
33+
std::cout << "back() throw an exception PASSED! " << e.what() << std::endl;
34+
}
35+
que.push(13);
36+
que.push(23);
37+
que.push(31);
38+
que.push(7); // 13 23 31 7
39+
assert(que.back() == 7);
40+
assert(que.front() == 13);
41+
assert(que.size() == 4);
42+
assert(que.empty() == false);
43+
que.pop();
44+
que.push(19);
45+
que.pop(); // 31 7 19
46+
assert(que.back() == 19);
47+
assert(que.front() == 31);
48+
std::cout << "ALL TEST PASSED!" << std::endl;
49+
return 0;
50+
}
Lines changed: 71 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,75 @@
1+
#ifndef QUEUE_H
2+
#define QUEUE_H
3+
4+
#include <stdexcept>
5+
#include <cstddef>
6+
#include "../../LinkedList/include/LinkedList.h"
7+
8+
/*
9+
We're going to implement Queue based on LinkedList since the two
10+
structures are quite similar. Namely, they both operate mostly on
11+
the elements at the beginning and the end. Compare push/pop
12+
operations of Queue with push_back/pop_front operations of LinkedList.
13+
*/
14+
115
template <typename T>
216
class Queue{
317
public:
4-
Queue();
5-
~Queue();
18+
//Capacity
19+
bool empty() const;
20+
size_t size() const;
21+
// Add
22+
void push(const T& value);
23+
// Delete
24+
void pop();
25+
// Check
26+
T& front();
27+
T& back();
628
private:
7-
};
29+
LinkedList<T> list;
30+
};
31+
32+
//Capacity
33+
template <typename T>
34+
bool Queue<T>::empty() const{
35+
return list.empty();
36+
}
37+
38+
template <typename T>
39+
size_t Queue<T>::size() const{
40+
return list.size();
41+
}
42+
43+
// Add
44+
template <typename T>
45+
void Queue<T>::push(const T &value){
46+
list.push_back(value);
47+
}
48+
49+
// Delete
50+
template <typename T>
51+
void Queue<T>::pop(){
52+
if(empty()){
53+
throw std::underflow_error("This Queue is empty");
54+
}
55+
list.pop_front();
56+
}
57+
58+
// Check
59+
template <typename T>
60+
T& Queue<T>::front(){
61+
if(empty()){
62+
throw std::underflow_error("This Queue is empty");
63+
}
64+
return list.front();
65+
}
66+
67+
template <typename T>
68+
T& Queue<T>::back(){
69+
if(empty()){
70+
throw std::underflow_error("This Queue is empty");
71+
}
72+
return list.back();
73+
}
74+
75+
#endif // QUEUE_H

data-structures/Queue/src/Queue.cpp

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)