Skip to content
This repository has been archived by the owner on Nov 8, 2023. It is now read-only.

Commit

Permalink
Merge pull request #53 from itodotimothy6/add-queue-front-back-functions
Browse files Browse the repository at this point in the history
Add queue front back functions
  • Loading branch information
Bhupesh-V authored May 30, 2019
2 parents 2c3baed + fc728bb commit 23b041d
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 3 deletions.
48 changes: 45 additions & 3 deletions queue.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
<summary>View contents</summary>
<ol>
<li><a href="#pop"><code>pop</code></a></li>
<li><a href="#back-queue"><code>back</code></a></li>
<li><a href="#back"><code>back</code></a></li>
<li><a href="#push"><code>push</code></a></li>
<li><a href="#size-queue"><code>size</code></a></li>
<li><a href="#swap-queue"><code>swap</code></a></li>
<li><a href="#empty-queue"><code>empty</code></a></li>
<li><a href="#front-queue"><code>front</code></a></li>
<li><a href="#front"><code>front</code></a></li>
<li><a href="#emplace-queue"><code>emplace</code></a></li>
<li><a href="#queue"><code>queue</code></a></li>
<li><a href="#~queue"><code>~queue</code></a>
Expand Down Expand Up @@ -41,6 +41,27 @@
```
**[Run Code](https://rextester.com/XACN77371)**
# back
**Description** : back() function is used to reference the last element (i.e the newest element) of the queue. This function can be used to fetch the last element of a queue.
**Example**:
```cpp
// Empty queue
std::queue<int> myqueue;
// Add elements to queue using push()
myqueue.push(2);
myqueue.push(5);
myqueue.push(4);
myqueue.push(1);
// Queue becomes 2, 5, 4, 1
// Print the last/newest element in the queue
std::cout << myqueue.back();
```
**[Run Code](https://rextester.com/MJAK44967)**

# push
**Description** : push() function is used to insert an element at the back of the queue. The element is added to the queue container and the size of the queue is increased by 1.

Expand All @@ -60,4 +81,25 @@
myqueue.pop();
}
```
**[Run Code](https://rextester.com/OEC31098)**
**[Run Code](https://rextester.com/OEC31098)**
# front
**Description** : front() function is used to reference the first element (ie. the oldest element) of the queue. This function can be used to fetch the first element of a queue.
**Example**:
```cpp
// Empty queue
std::queue<int> myqueue;
// Add elements to queue using push()
myqueue.push(2);
myqueue.push(5);
myqueue.push(4);
myqueue.push(1);
// Queue becomes 2, 5, 4, 1
// Print the first/oldest element in the queue
std::cout << myqueue.front();
```
**[Run Code](https://rextester.com/NHSYM95891)**
25 changes: 25 additions & 0 deletions queue/back.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Style Guide => https://github.com/Bhupesh-V/30-Seconds-Of-STL/blob/master/CONTRIBUTING.md/#Style Guide
/*
Author : Timothy Itodo
Date : 28/05/2019
Time : 1:00 PM
Description : back() function is used to reference the last element (i.e the newest element) of the queue. This function can be used to fetch the last element of a queue.
*/
#include <iostream>
#include <queue>

int main(){
// Empty queue
std::queue<int> myqueue;

// Add elements to queue using push()
myqueue.push(2);
myqueue.push(5);
myqueue.push(4);
myqueue.push(1);

// Queue becomes 2, 5, 4, 1

// Print the last/newest element in the queue
std::cout << myqueue.back();
}
25 changes: 25 additions & 0 deletions queue/front.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Style Guide => https://github.com/Bhupesh-V/30-Seconds-Of-STL/blob/master/CONTRIBUTING.md/#Style Guide
/*
Author : Timothy Itodo
Date : 28/05/2019
Time : 1:00 PM
Description : front() function is used to reference the first element (ie. the oldest element) of the queue. This function can be used to fetch the first element of a queue.
*/
#include <iostream>
#include <queue>

int main(){
// Empty queue
std::queue<int> myqueue;

// Add elements to queue using push()
myqueue.push(2);
myqueue.push(5);
myqueue.push(4);
myqueue.push(1);

// Queue becomes 2, 5, 4, 1

// Print the first/oldest element in the queue
std::cout << myqueue.front();
}

0 comments on commit 23b041d

Please sign in to comment.