Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Concept Entry] C++ List #5337

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions content/cpp/concepts/list/list.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
---
Title: 'List'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Title: 'List'
Title: 'List'

Description: 'In C++, list is a doubly linked list from the STL. It allows efficient insertions and deletions of data'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Description: 'In C++, list is a doubly linked list from the STL. It allows efficient insertions and deletions of data'
Description: 'List is a sequential container that stores elements non-contiguous memory locations.'

Subjects:
- 'Game Development'
- 'Mobile Development'
- 'Machine Learning'
Tags:
- 'Data Structures'
- 'Doubly Linked Lists'
- 'Lists'
CatalogContent:
- 'learn-c-plus-plus'
- 'paths/computer-science'
---

The **List** in C++ is a part of the Standard Template Library (STL) and represents a doubly linked list. It allows proficient insertion and deletion of elelments at any position with constant time complexity.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The **List** in C++ is a part of the Standard Template Library (STL) and represents a doubly linked list. It allows proficient insertion and deletion of elelments at any position with constant time complexity.
**List** in C++ is a sequential container and a part of the Standard Template Library (STL) that stores elements in non-contiguous memory locations. The list is implemented using a doubly linked list. It allows proficient insertion and deletion of elements at any known position with constant time complexity.


## Syntax

To implement std::list in C++, you need to include the following header:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mention this part as a note


```pseudo
#include <list>
```
Comment on lines +23 to +25
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this part mention pseudo code for list

### Example

```cpp
#include <iostream>
#include <list>
int main() {
Comment on lines +30 to +31
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Give a blank line after line 30

// Declare a list of integers

std::list<int> myList;
// Adding elements to the list

myList.push_back(10); // Adds 10 to the back
myList.push_back(20); // Adds 20 to the back
myList.push_front(5); // Adds 5 to the front
// Displaying elements in the list
std::cout << "List elements: ";
for (const auto& value : myList) {
std::cout << value << " "; // Output: 5 10 20
}
std::cout << std::endl;
// Removing an element
myList.remove(10); // Removes the element with value 10
// Displaying the updated list
std::cout << "Updated list elements after deletion: ";
for (const auto& value : myList) {
std::cout << value << " "; // Output: 5 20
}
std::cout << std::endl;
return 0;
}
Comment on lines +29 to +55
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#include <iostream>
#include <list>
int main() {
// Declare a list of integers
std::list<int> myList;
// Adding elements to the list
myList.push_back(10); // Adds 10 to the back
myList.push_back(20); // Adds 20 to the back
myList.push_front(5); // Adds 5 to the front
// Displaying elements in the list
std::cout << "List elements: ";
for (const auto& value : myList) {
std::cout << value << " "; // Output: 5 10 20
}
std::cout << std::endl;
// Removing an element
myList.remove(10); // Removes the element with value 10
// Displaying the updated list
std::cout << "Updated list elements after deletion: ";
for (const auto& value : myList) {
std::cout << value << " "; // Output: 5 20
}
std::cout << std::endl;
return 0;
}
#include <iostream>
#include <list>
int main() {
// Declare a list of integers
std::list < int > myList;
// Adding elements to the list
myList.push_back(10); // Adds 10 to the back
myList.push_back(20); // Adds 20 to the back
myList.push_front(5); // Adds 5 to the front
// Displaying elements in the list
std::cout << "List elements: ";
for (const auto & value: myList) {
std::cout << value << " "; // Output: 5 10 20
}
std::cout << std::endl;
// Removing an element
myList.remove(10); // Removes the element with value 10
// Displaying the updated list
std::cout << "Updated list elements after deletion: ";
for (const auto & value: myList) {
std::cout << value << " "; // Output: 5 20
}
std::cout << std::endl;
return 0;
}


Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this line

```
The output for the above code is:
Comment on lines +57 to +58
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Give a blank line after line 50


Here `5`,`10`,and `20` are the numbers that are inserted in the list and also printed the updated list after deletion of `10`.

```shell
List elements: 5 10 20
Updated list elements after deletion: 5 20

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this blank line

```
## Codebyte Example

```codenyte/cpp
#include <iostream>
#include <list>
#include <string>

int main() {
// Create a list of strings
std::list<std::string> fruits = {"apple", "banana", "cherry"};

// Add a fruit to the list
fruits.push_back("orange");

// Access elements in the list using an iterator
for (const auto& fruit : fruits) {
std::cout << fruit << std::endl;
}

return 0;
}

```




Comment on lines +90 to +93
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove these blank lines

Loading