simple and works

This commit is contained in:
lda committed 2025-06-30 09:38:26 +07:00
1 parent a329f16cec
commit febea5cd30
5 files changed
+432

No files matched your search

+68
View File
@@ -0,0 +1,68 @@
#include <iostream>
#include <vector>
// Array-based list class
class ArrayList
{
private:
std::vector<int> data;
public:
ArrayList() {}
// Adds an element to the end of the list (O(1) amortized)
void push_back(int value)
{
data.push_back(value);
}
// Removes the last element from the list (O(1))
int pop_back()
{
if (data.empty())
{
throw std::runtime_error("List is empty");
}
int value = data.back();
data.pop_back();
return value;
}
// Gets the element at a specific index (O(1))
int get(int index) const
{
if (index < 0 || index >= data.size())
{
throw std::runtime_error("Index out of bounds");
}
return data[index];
}
// Prints the list (O(n))
void print() const
{
for (int value : data)
{
std::cout << value << " ";
}
std::cout << std::endl;
}
int get_size() const { return data.size(); }
};
int main()
{
ArrayList list;
list.push_back(10);
list.push_back(20);
list.push_back(30);
list.print();
std::cout << "Popped: " << list.pop_back() << std::endl;
list.print();
std::cout << "Element at index 1: " << list.get(1) << std::endl;
return 0;
}
+78
View File
@@ -0,0 +1,78 @@
#include <iostream>
#include <deque>
// Deque class using std::deque
class Deque
{
private:
std::deque<int> data;
public:
Deque() {}
// Adds an element to the front of the deque (O(1))
void push_front(int value)
{
data.push_front(value);
}
// Adds an element to the back of the deque (O(1))
void push_back(int value)
{
data.push_back(value);
}
// Removes an element from the front of the deque (O(1))
int pop_front()
{
if (data.empty())
{
throw std::runtime_error("Deque is empty");
}
int value = data.front();
data.pop_front();
return value;
}
// Removes an element from the back of the deque (O(1))
int pop_back()
{
if (data.empty())
{
throw std::runtime_error("Deque is empty");
}
int value = data.back();
data.pop_back();
return value;
}
// Prints the deque (O(n))
void print() const
{
for (int value : data)
{
std::cout << value << " ";
}
std::cout << std::endl;
}
int get_size() const { return data.size(); }
};
int main()
{
Deque deque;
deque.push_back(10);
deque.push_back(20);
deque.push_front(30);
deque.push_front(40);
deque.print();
std::cout << "Popped from front: " << deque.pop_front() << std::endl;
deque.print();
std::cout << "Popped from back: " << deque.pop_back() << std::endl;
deque.print();
return 0;
}
+133
View File
@@ -0,0 +1,133 @@
#include <iostream>
#include <memory>
// Node structure for doubly linked list
struct Node
{
int data;
Node *prev;
std::unique_ptr<Node> next;
Node(int value) : data(value), prev(nullptr), next(nullptr) {}
};
// Doubly linked list class
class DoublyLinkedList
{
private:
std::unique_ptr<Node> head;
Node *tail;
int size;
public:
DoublyLinkedList() : head(nullptr), tail(nullptr), size(0) {}
// Adds a value to the front of the list (O(1))
void push_front(int value)
{
auto new_node = std::make_unique<Node>(value);
if (!head)
{
head = std::move(new_node);
tail = head.get();
}
else
{
new_node->next = std::move(head);
head->prev = new_node.get();
head = std::move(new_node);
}
size++;
}
// Adds a value to the end of the list (O(1))
void push_back(int value)
{
auto new_node = std::make_unique<Node>(value);
if (!tail)
{
head = std::move(new_node);
tail = head.get();
}
else
{
tail->next = std::move(new_node);
tail->next->prev = tail;
tail = tail->next.get();
}
size++;
}
// Removes a value from the front of the list (O(1))
int pop_front()
{
if (!head)
{
throw std::runtime_error("List is empty");
}
int value = head->data;
head = std::move(head->next);
if (head)
{
head->prev = nullptr;
}
else
{
tail = nullptr;
}
size--;
return value;
}
// Removes a value from the end of the list (O(1))
int pop_back()
{
if (!tail)
{
throw std::runtime_error("List is empty");
}
int value = tail->data;
tail = tail->prev;
if (tail)
{
tail->next.reset();
}
else
{
head.reset();
}
size--;
return value;
}
// Prints the list from front to back (O(n))
void print() const
{
Node *current = head.get();
while (current)
{
std::cout << current->data << " ";
current = current->next.get();
}
std::cout << std::endl;
}
int get_size() const { return size; }
};
int main()
{
DoublyLinkedList list;
list.push_front(10);
list.push_front(20);
list.push_back(30);
list.print();
std::cout << "Popped from front: " << list.pop_front() << std::endl;
list.print();
std::cout << "Popped from back: " << list.pop_back() << std::endl;
list.print();
return 0;
}
+85
View File
@@ -0,0 +1,85 @@
#include <iostream>
#include <memory>
// Node structure for singly linked list
struct Node
{
int data;
std::unique_ptr<Node> next;
Node(int value) : data(value), next(nullptr) {}
};
// Singly linked list class
class SinglyLinkedList
{
private:
std::unique_ptr<Node> head;
Node *tail;
int size;
public:
SinglyLinkedList() : head(nullptr), tail(nullptr), size(0) {}
// Adds a value to the end of the list (O(1))
void push_back(int value)
{
auto new_node = std::make_unique<Node>(value);
if (!head)
{
head = std::move(new_node);
tail = head.get();
}
else
{
tail->next = std::move(new_node);
tail = tail->next.get();
}
size++;
}
// Removes a value from the front of the list (O(1))
int pop_front()
{
if (!head)
{
throw std::runtime_error("List is empty");
}
int value = head->data;
head = std::move(head->next);
if (!head)
{
tail = nullptr;
}
size--;
return value;
}
// Prints the list (O(n))
void print() const
{
Node *current = head.get();
while (current)
{
std::cout << current->data << " ";
current = current->next.get();
}
std::cout << std::endl;
}
int get_size() const { return size; }
};
int main()
{
SinglyLinkedList list;
list.push_back(10);
list.push_back(20);
list.push_back(30);
list.print();
std::cout << "Popped: " << list.pop_front() << std::endl;
list.print();
return 0;
}
+68
View File
@@ -0,0 +1,68 @@
#include <iostream>
#include <vector>
// Stack class using std::vector
class Stack
{
private:
std::vector<int> data;
public:
Stack() {}
// Pushes an element onto the stack (O(1))
void push(int value)
{
data.push_back(value);
}
// Pops the top element from the stack (O(1))
int pop()
{
if (data.empty())
{
throw std::runtime_error("Stack is empty");
}
int value = data.back();
data.pop_back();
return value;
}
// Peeks at the top element of the stack (O(1))
int peek() const
{
if (data.empty())
{
throw std::runtime_error("Stack is empty");
}
return data.back();
}
// Prints the stack (O(n))
void print() const
{
for (int value : data)
{
std::cout << value << " ";
}
std::cout << std::endl;
}
int get_size() const { return data.size(); }
};
int main()
{
Stack stack;
stack.push(10);
stack.push(20);
stack.push(30);
stack.print();
std::cout << "Popped: " << stack.pop() << std::endl;
stack.print();
std::cout << "Peek: " << stack.peek() << std::endl;
return 0;
}