-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.cpp
More file actions
112 lines (99 loc) · 3.71 KB
/
LinkedList.cpp
File metadata and controls
112 lines (99 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include "LinkedList.h"
#include <SFML/Graphics.hpp>
#include <string>
LinkedList::~LinkedList() {
while (head) {
Node* temp = head;
head = head->next;
delete temp;
}
}
void LinkedList::addAtHead(int value) {
Node* newNode = new Node(value);
newNode->next = head;
head = newNode;
}
void LinkedList::remove(int value) {
Node** current = &head;
while (*current) {
if ((*current)->value == value) {
Node* temp = *current;
*current = (*current)->next;
delete temp;
return;
}
current = &((*current)->next);
}
}
void LinkedList::draw(sf::RenderWindow &window, const sf::Font &font) const {
Node* current = head;
int x = 10;
while (current) {
sf::Text nodeValue(std::to_string(current->value), font, 20);
nodeValue.setPosition(x, 100);
nodeValue.setFillColor(sf::Color::White);
window.draw(nodeValue);
x += 50;
current = current->next;
}
}
void LinkedList::interactiveMode(sf::RenderWindow &window, const sf::Font &font) {
bool running = true;
std::string userInput;
sf::Text instructions("Press A to Add, R to Remove, Esc to Exit.", font, 20);
instructions.setPosition(10, 10);
instructions.setFillColor(sf::Color::White);
sf::Text inputText("", font, 20);
inputText.setPosition(10, 40);
inputText.setFillColor(sf::Color::Yellow);
sf::Text errorText("", font, 20);
errorText.setPosition(10, 70);
errorText.setFillColor(sf::Color::Red);
while (running && window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed)
window.close();
if (event.type == sf::Event::KeyPressed) {
if (event.key.code == sf::Keyboard::Escape) {
running = false;
} else if (event.key.code == sf::Keyboard::A) {
if (!userInput.empty() && std::all_of(userInput.begin(), userInput.end(), ::isdigit)) {
int value = std::stoi(userInput);
addAtHead(value);
userInput.clear();
errorText.setString(""); // Limpia cualquier mensaje de error
} else {
errorText.setString("Invalid input. Please enter a number.");
}
} else if (event.key.code == sf::Keyboard::R) {
if (!userInput.empty() && std::all_of(userInput.begin(), userInput.end(), ::isdigit)) {
int value = std::stoi(userInput);
remove(value);
userInput.clear();
errorText.setString(""); // Limpia cualquier mensaje de error
} else {
errorText.setString("Invalid input. Please enter a number.");
}
}
}
if (event.type == sf::Event::TextEntered) {
if (event.text.unicode == '\b' && !userInput.empty()) {
userInput.pop_back();
} else if (event.text.unicode >= 32 && event.text.unicode < 128) {
char enteredChar = static_cast<char>(event.text.unicode);
if (isdigit(enteredChar) || (enteredChar == '-' && userInput.empty())) {
userInput += enteredChar;
}
}
}
}
inputText.setString("Input: " + userInput);
window.clear();
draw(window, font);
window.draw(instructions);
window.draw(inputText);
window.draw(errorText);
window.display();
}
}