Cleaning up and adding comments

This commit is contained in:
Christopher Sanden
2025-11-20 15:24:00 +01:00
parent e77d7ff21e
commit a014e4ca73
7 changed files with 102 additions and 27 deletions

View File

@@ -3,7 +3,19 @@
#include "SharedLib.h"
TDoublyLinkedList::~TDoublyLinkedList()
{
const Node* cur = head;
while (cur) {
const Node* next = cur->GetNext();
delete cur;
cur = next;
}
head = tail = nullptr;
size = 0;
}
// Append a new line at the end of the list.
void TDoublyLinkedList::Append(const std::string& line)
{
auto* newNode = new Node(line);
@@ -17,6 +29,7 @@ void TDoublyLinkedList::Append(const std::string& line)
size++;
}
// Insert a new line at the beginning.
void TDoublyLinkedList::Prepend(const std::string& line)
{
auto* newNode = new Node(line);
@@ -43,6 +56,7 @@ TDoublyLinkedList::Node* TDoublyLinkedList::NavigateToNode(const int index) cons
return node;
}
// Removes the node at the given index and updates head/tail if needed.
void TDoublyLinkedList::Remove(const int index)
{
auto* node = NavigateToNode(index);

View File

@@ -2,13 +2,16 @@
#define TDOUBLYLINKEDLIST_H
#include <string>
#include <utility>
#include "SharedLib.h"
// Doubly linked list used to store document lines.
// Supports insertion, removal, and indexed access.
// Chosen because it allows efficient updates in the middle of the document.
class TDoublyLinkedList {
private:
// Internal node storing a single line of text
// and links to previous and next nodes.
struct Node {
std::string line;
Node* next;
@@ -45,12 +48,21 @@ private:
public:
TDoublyLinkedList() : head(nullptr), tail(nullptr), size(0) {}
~TDoublyLinkedList() = default;
~TDoublyLinkedList();
void Append(const std::string &line);
void Prepend(const std::string& line);
// Returns pointer to node at given index.
// Linear traversal; used internally by Remove and InsertAtIndex.
[[nodiscard]] Node* NavigateToNode(int index) const;
// Removes a node at the given index.
// Updates links and frees the removed node.
void Remove(int index);
// Returns the text stored at the given index.
// Uses NavigateToNode internally.
[[nodiscard]] std::string GetAtIndex(int index) const;
void InsertAtIndex(int index, const std::string &line);
[[nodiscard]] int GetSize() const;

View File

@@ -10,8 +10,13 @@ enum EnumActionType {
DELETE
};
// Simple fixed-size stack used for undo/redo.
// Stores actions describing line insert/delete operations.
class TStack {
private:
// Describes a single text-edit action.
// 'action' indicates INSERT or DELETE,
// 'text' stores the affected line, and 'index' is the line position.
struct TAction {
EnumActionType action;
std::string text;
@@ -24,9 +29,8 @@ private:
public:
TStack() = default;
~TStack() = default;
void Push(const TAction& action);
TAction Pop();
void Push(const TAction& action); // Adds a new action to the top of the stack.
TAction Pop(); // Removes and returns the most recent action.
[[nodiscard]] TAction Peek() const;
[[nodiscard]] bool IsEmpty() const;
void Clear();

View File

@@ -5,7 +5,9 @@
#include "TDoublyLinkedList.h"
// Circular array-based queue implementation.
// Used in Cat 1 to store print jobs (each job is a full document snapshot).
// Demonstrates FIFO behavior through enqueue/dequeue operations.
class TTreeQueue {
private:
std::string queue[MAX_SIZE];
@@ -17,8 +19,8 @@ public:
TTreeQueue() = default;
~TTreeQueue() = default;
void Enqueue(const std::string& text);
std::string Dequeue();
void Enqueue(const std::string& text); // Adds a new job at the tail of the queue.
std::string Dequeue(); // Removes and returns the next job in FIFO order.
[[nodiscard]] int GetTail() const;
[[nodiscard]] std::string Peek() const;
[[nodiscard]] bool IsEmpty() const;

View File

@@ -5,11 +5,11 @@
#include <ctime>
#include <iostream>
#include <limits>
#include <numbers>
// Displays the main menu and reads user choice.
int Utils::Choice()
{
std::cout << "========\n1. Add line\n2. Remove line\n3. Print current document\n4. Print queue\n5. Undo\n6. Redo\n0. Exit"
std::cout << "========\n1. Add line\n2. Remove line\n3. View current1 document\n4. Print queue\n5. Process print job\n6. Undo\n7. Redo\n0. Exit"
"\n\nChoice: ";
int choice;
std::cin >> choice;
@@ -17,6 +17,9 @@ int Utils::Choice()
return choice;
}
// Inserts a new line into the document.
// Records the action into the undo stack.
// 'index' determines insert location; defaults to end of document.
int Utils::Insert(TDoublyLinkedList &document, TStack &undoStack, TStack &redoStack, int index)
{
for (int i = 0; i < document.GetSize(); i++) {
@@ -58,6 +61,8 @@ void Utils::PrintList(const TDoublyLinkedList &document)
std::cout << "\n\n";
}
// Removes a line chosen by the user.
// Action is pushed to undo stack for reversibility.
int Utils::RemoveLine(TDoublyLinkedList &document, TStack &undoStack, TStack &redoStack, int index)
{
std::cout << "Enter the number of the line you want to remove" <<std::endl;