From fba5ae638e6c3af82d550051f1038c9f0c210e39 Mon Sep 17 00:00:00 2001 From: Javier Fernandez Morata Date: Fri, 26 Sep 2025 14:08:04 +0200 Subject: [PATCH 1/2] [ISSUE 7631] Queue Back entry writed and checked locally --- .../cpp/concepts/queues/terms/back/back.md | 116 ++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 content/cpp/concepts/queues/terms/back/back.md diff --git a/content/cpp/concepts/queues/terms/back/back.md b/content/cpp/concepts/queues/terms/back/back.md new file mode 100644 index 00000000000..6c215e09c9f --- /dev/null +++ b/content/cpp/concepts/queues/terms/back/back.md @@ -0,0 +1,116 @@ +--- +Title: '.back()' +Description: 'Returns a reference to the element at the back of the queue.' +Subjects: # Please only use Subjects in the subjects.md file (https://github.com/Codecademy/docs/blob/main/documentation/subjects.md). If that list feels insufficient, feel free to create a new Subject and add it to subjects.md in your PR! + - 'Computer Science' + - 'Game Development' +Tags: # Please only use Tags in the tags.md file (https://github.com/Codecademy/docs/blob/main/documentation/tags.md). If that list feels insufficient, feel free to create a new Tag and add it to tags.md in your PR! + - 'Classes' + - 'Methods' + - 'Objects' + - 'OOP' +CatalogContent: # Please use course/path landing page slugs, rather than linking to individual content items. If listing multiple items, please put the most relevant one first + - 'learn-c-plus-plus' + - 'paths/computer-science' +--- + +The C++ **`.back()`** [method](https://www.codecademy.com/resources/docs/cpp/methods) is used to access the element at the back of the queue without removing it. + +## C++ `.back()` Syntax + +```pseudo +queueName.back(); +``` + +**Parameters:** + +The C++ `.back()` method takes no parameters. + +**Return value:** + +Returns a reference to the element at the end of the queue. + +## Example 1: Basic usage of C++ `.back()` + +This example uses C++ `.back()` to access the last element of the queue: + +```cpp +#include +#include +using namespace std; + +int main() { + // Declaring a queue + queue q; + + // Inserting int elements into the queue + q.push(10); + q.push(20); + q.push(30); + + // Print the last element of the queue + cout <<"Last element: " << q.back() << endl; +} +``` + +Here is the output: + +```shell +30 +``` + +## Example 2: Using C++ `.back()` with `.push()` + +This example uses C++ `.back()` to return the last element of the queue after we used `.push()` + +```cpp +#include +#include +using namespace std; + +int main () { + queue q; + + q.push("Jhon"); + q.push("Bob"); + q.push("Doe"); + + cout <<"Last element: " << q.back() << endl; + q.push("Alice"); + cout <<"Last element after push: " << q.back() << endl; + +} +``` + +Here is the output: + +```shell +Doe +Alice +``` + +## Codebyte Example: Modifying the Last Element Using C++ `.back()` + +This codebyte example uses C++ `.back()` to modify the last element of the queue: + +```codebyte/cpp +#include +#include +using namespace std; + +int main() { + queue q; + + q.push(10); + q.push(20); + + cout << q.back() << endl; + + // Modify the first element of the queue + q.back() = 100; + + cout << q.front() << endl; + + return 0; +} +``` \ No newline at end of file From cfcb7b567d0cfac36f388f4acfc4c44a1d8aa8e8 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Mon, 29 Sep 2025 12:26:02 +0530 Subject: [PATCH 2/2] minor output fixes --- .../cpp/concepts/queues/terms/back/back.md | 69 ++++++++++--------- 1 file changed, 35 insertions(+), 34 deletions(-) diff --git a/content/cpp/concepts/queues/terms/back/back.md b/content/cpp/concepts/queues/terms/back/back.md index 6c215e09c9f..328a844b6e6 100644 --- a/content/cpp/concepts/queues/terms/back/back.md +++ b/content/cpp/concepts/queues/terms/back/back.md @@ -1,20 +1,20 @@ --- Title: '.back()' Description: 'Returns a reference to the element at the back of the queue.' -Subjects: # Please only use Subjects in the subjects.md file (https://github.com/Codecademy/docs/blob/main/documentation/subjects.md). If that list feels insufficient, feel free to create a new Subject and add it to subjects.md in your PR! +Subjects: - 'Computer Science' - 'Game Development' -Tags: # Please only use Tags in the tags.md file (https://github.com/Codecademy/docs/blob/main/documentation/tags.md). If that list feels insufficient, feel free to create a new Tag and add it to tags.md in your PR! +Tags: - 'Classes' - 'Methods' - 'Objects' - 'OOP' -CatalogContent: # Please use course/path landing page slugs, rather than linking to individual content items. If listing multiple items, please put the most relevant one first +CatalogContent: - 'learn-c-plus-plus' - 'paths/computer-science' --- -The C++ **`.back()`** [method](https://www.codecademy.com/resources/docs/cpp/methods) is used to access the element at the back of the queue without removing it. +The C++ **`.back()`** [method](https://www.codecademy.com/resources/docs/cpp/methods) is used to access the element at the back of the queue without removing it. ## C++ `.back()` Syntax @@ -40,28 +40,28 @@ This example uses C++ `.back()` to access the last element of the queue: using namespace std; int main() { - // Declaring a queue - queue q; - - // Inserting int elements into the queue - q.push(10); - q.push(20); - q.push(30); - - // Print the last element of the queue - cout <<"Last element: " << q.back() << endl; + // Declaring a queue + queue q; + + // Inserting int elements into the queue + q.push(10); + q.push(20); + q.push(30); + + // Print the last element of the queue + cout <<"Last element: " << q.back() << endl; } ``` -Here is the output: +Here is the output of this code: ```shell -30 +Last element: 30 ``` ## Example 2: Using C++ `.back()` with `.push()` -This example uses C++ `.back()` to return the last element of the queue after we used `.push()` +This example uses C++ `.back()` to return the last element of the queue after we used `.push()`: ```cpp #include @@ -69,24 +69,23 @@ This example uses C++ `.back()` to return the last element of the queue after we using namespace std; int main () { - queue q; - - q.push("Jhon"); - q.push("Bob"); - q.push("Doe"); - - cout <<"Last element: " << q.back() << endl; - q.push("Alice"); - cout <<"Last element after push: " << q.back() << endl; - + queue q; + + q.push("Jhon"); + q.push("Bob"); + q.push("Doe"); + + cout <<"Last element: " << q.back() << endl; + q.push("Alice"); + cout <<"Last element after push: " << q.back() << endl; } ``` -Here is the output: +Here is the output of this code: ```shell -Doe -Alice +Last element: Doe +Last element after push: Alice ``` ## Codebyte Example: Modifying the Last Element Using C++ `.back()` @@ -104,13 +103,15 @@ int main() { q.push(10); q.push(20); - cout << q.back() << endl; + // Print the last element + cout << "Last element before modification: " << q.back() << endl; - // Modify the first element of the queue + // Modify the last element q.back() = 100; - cout << q.front() << endl; + // Print the modified last element + cout << "Last element after modification: " << q.back() << endl; return 0; } -``` \ No newline at end of file +```