diff --git a/Documentation/deque_examples.md b/Documentation/deque_examples.md new file mode 100644 index 000000000..75b3e6eb3 --- /dev/null +++ b/Documentation/deque_examples.md @@ -0,0 +1,41 @@ + +# Deque Examples + +A `Deque` (double-ended queue) allows you to efficiently add and remove elements from both ends. + +## Creating a Deque + +```swift +// Importing the library +import SwiftCollections + +// Creating a deque and adding elements +var deque = Deque() +deque.append(10) // Add to the back +deque.append(20) +deque.prepend(5) // Add to the front + +// Accessing elements +print(deque.first) // Output: Optional(5) +print(deque.last) // Output: Optional(20) +``` + +## Removing Elements + +```swift +// Removing elements from the deque +deque.popFirst() // Removes 5 +deque.popLast() // Removes 20 +``` + +## Iterating Over a Deque + +```swift +// Iterating through the deque +deque.append(contentsOf: [1, 2, 3]) +for element in deque { + print(element) +} +``` + +These examples demonstrate basic usage of `Deque` in Swift. diff --git a/Documentation/orderedset_examples.md b/Documentation/orderedset_examples.md new file mode 100644 index 000000000..73d0e86d8 --- /dev/null +++ b/Documentation/orderedset_examples.md @@ -0,0 +1,42 @@ + +# OrderedSet Examples + +An `OrderedSet` maintains the order of elements while ensuring each element is unique. + +## Creating an OrderedSet + +```swift +// Importing the library +import SwiftCollections + +// Creating an OrderedSet and adding elements +var orderedSet: OrderedSet = ["apple", "banana", "cherry"] +orderedSet.insert("date", at: 1) +print(orderedSet) // Output: ["apple", "date", "banana", "cherry"] +``` + +## Accessing Elements + +```swift +// Accessing elements by index +let firstElement = orderedSet[0] +print(firstElement) // Output: "apple" +``` + +## Checking for Existence + +```swift +// Checking if an element exists +print(orderedSet.contains("banana")) // Output: true +print(orderedSet.contains("grape")) // Output: false +``` + +## Removing Elements + +```swift +// Removing elements +orderedSet.remove("banana") +print(orderedSet) // Output: ["apple", "date", "cherry"] +``` + +These examples illustrate how to use `OrderedSet` effectively in Swift.