Refactor code structure for improved readability and maintainability
This commit is contained in:
80
demos/lists/cpp/main.cpp
Normal file
80
demos/lists/cpp/main.cpp
Normal file
@@ -0,0 +1,80 @@
|
||||
#include <iostream>
|
||||
#include <optional>
|
||||
|
||||
// A single node containing data and a raw pointer to the next node
|
||||
template <typename T>
|
||||
struct Node {
|
||||
T data;
|
||||
Node<T>* next;
|
||||
};
|
||||
|
||||
// The main wrapper for the linked list tracking the head node
|
||||
template <typename T>
|
||||
class LinkedList {
|
||||
public:
|
||||
LinkedList() : head_(nullptr) {}
|
||||
|
||||
~LinkedList() {
|
||||
Node<T>* current = head_;
|
||||
while (current) {
|
||||
Node<T>* next = current->next;
|
||||
delete current;
|
||||
current = next;
|
||||
}
|
||||
}
|
||||
|
||||
// Raw-pointer ownership can't be safely duplicated by a shallow copy
|
||||
LinkedList(const LinkedList&) = delete;
|
||||
LinkedList& operator=(const LinkedList&) = delete;
|
||||
|
||||
// Add a new element to the front of the list
|
||||
void push_front(T data) {
|
||||
head_ = new Node<T>{std::move(data), head_};
|
||||
}
|
||||
|
||||
// Remove and return the front element of the list
|
||||
std::optional<T> pop_front() {
|
||||
if (!head_) {
|
||||
return std::nullopt;
|
||||
}
|
||||
Node<T>* old_head = head_;
|
||||
T data = std::move(old_head->data);
|
||||
head_ = old_head->next;
|
||||
delete old_head;
|
||||
return data;
|
||||
}
|
||||
|
||||
// Read the front element without removing it
|
||||
const T* peek_front() const {
|
||||
return head_ ? &head_->data : nullptr;
|
||||
}
|
||||
|
||||
private:
|
||||
Node<T>* head_;
|
||||
};
|
||||
|
||||
int main() {
|
||||
LinkedList<int> list;
|
||||
int count=0;
|
||||
char * message1 = "hello world";
|
||||
// Demonstrate pushing items
|
||||
list.push_front(10);
|
||||
count++;
|
||||
list.push_front(20);
|
||||
count++;
|
||||
list.push_front(30);
|
||||
count++;
|
||||
|
||||
// Demonstrate peeking at the top item
|
||||
if (const int* top = list.peek_front()) {
|
||||
std::cout << "Top element: " << *top << "\n"<<"count: "<<count<<"\n"; // Output: 30
|
||||
}
|
||||
std::cout<<message1<<"\n";
|
||||
|
||||
// Demonstrate popping items
|
||||
while (auto value = list.pop_front()) {
|
||||
std::cout << "Popped: " << *value << "\n";
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
63
demos/lists/python/main.py
Normal file
63
demos/lists/python/main.py
Normal file
@@ -0,0 +1,63 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
from typing import Generic, Optional, TypeVar
|
||||
|
||||
T = TypeVar("T")
|
||||
|
||||
|
||||
# A single node containing data and a reference to the next node
|
||||
@dataclass
|
||||
class Node(Generic[T]):
|
||||
data: T
|
||||
next: Optional["Node[T]"]
|
||||
|
||||
|
||||
# The main wrapper for the linked list tracking the head node
|
||||
class LinkedList(Generic[T]):
|
||||
def __init__(self) -> None:
|
||||
self._head: Optional[Node[T]] = None
|
||||
|
||||
# Add a new element to the front of the list
|
||||
def push_front(self, data: T) -> None:
|
||||
self._head = Node(data, self._head)
|
||||
|
||||
# Remove and return the front element of the list
|
||||
def pop_front(self) -> Optional[T]:
|
||||
if self._head is None:
|
||||
return None
|
||||
data = self._head.data
|
||||
self._head = self._head.next
|
||||
return data
|
||||
|
||||
# Read the front element without removing it
|
||||
def peek_front(self) -> Optional[T]:
|
||||
return self._head.data if self._head else None
|
||||
|
||||
|
||||
def main() -> None:
|
||||
lst: LinkedList[int] = LinkedList()
|
||||
count = 0
|
||||
message1 = "hello world"
|
||||
|
||||
# Demonstrate pushing items
|
||||
lst.push_front(10)
|
||||
count += 1
|
||||
lst.push_front(20)
|
||||
count += 1
|
||||
lst.push_front(30)
|
||||
count += 1
|
||||
|
||||
# Demonstrate peeking at the top item
|
||||
top = lst.peek_front()
|
||||
if top is not None:
|
||||
print(f"Top element: {top}\ncount: {count}") # Output: 30
|
||||
print(message1)
|
||||
|
||||
# Demonstrate popping items
|
||||
while (value := lst.pop_front()) is not None:
|
||||
print(f"Popped: {value}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
6
demos/lists/rust/Cargo.toml
Normal file
6
demos/lists/rust/Cargo.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "list_example"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
65
demos/lists/rust/src/main.rs
Normal file
65
demos/lists/rust/src/main.rs
Normal file
@@ -0,0 +1,65 @@
|
||||
// A single node containing data and an optional pointer to the next node
|
||||
struct Node<T> {
|
||||
data: T,
|
||||
next: Option<Box<Node<T>>>,
|
||||
}
|
||||
|
||||
// The main wrapper for the linked list tracking the head node
|
||||
pub struct LinkedList<T> {
|
||||
head: Option<Box<Node<T>>>,
|
||||
}
|
||||
|
||||
impl<T> LinkedList<T> {
|
||||
// Create an empty linked list
|
||||
pub fn new() -> Self {
|
||||
LinkedList { head: None }
|
||||
}
|
||||
|
||||
// Add a new element to the front of the list
|
||||
pub fn push_front(&mut self, data: T) {
|
||||
let new_node = Box::new(Node {
|
||||
data,
|
||||
// .take() temporarily leaves None in self.head and extracts the value
|
||||
next: self.head.take(),
|
||||
});
|
||||
self.head = Some(new_node);
|
||||
}
|
||||
|
||||
// Remove and return the front element of the list
|
||||
pub fn pop_front(&mut self) -> Option<T> {
|
||||
self.head.take().map(|node| {
|
||||
self.head = node.next;
|
||||
node.data
|
||||
})
|
||||
}
|
||||
|
||||
// Read the front element without removing it
|
||||
pub fn peek_front(&self) -> Option<&T> {
|
||||
self.head.as_ref().map(|node| &node.data)
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut list = LinkedList::new();
|
||||
let mut count = 0;
|
||||
let message1 = "hello world";
|
||||
|
||||
// Demonstrate pushing items
|
||||
list.push_front(10);
|
||||
count += 1;
|
||||
list.push_front(20);
|
||||
count += 1;
|
||||
list.push_front(30);
|
||||
count += 1;
|
||||
|
||||
// Demonstrate peeking at the top item
|
||||
if let Some(top) = list.peek_front() {
|
||||
println!("Top element: {}\ncount: {}", top, count); // Output: 30
|
||||
}
|
||||
println!("{}", message1);
|
||||
|
||||
// Demonstrate popping items
|
||||
while let Some(value) = list.pop_front() {
|
||||
println!("Popped: {}", value);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user