Программы на C++. Форум разработчиков
-
Гость
У меня возникла ошибка: «Джордж» не называет тип в первой строке текстового файла. Мой профессор не отвечает, и я не уве
Сообщение
Гость »
заголовок стека, включающий реализацию
Код: Выделить всё
#ifndef STACK_H
#define STACK_H
#include
template
class Stack {
private:
struct Node {
T data;
Node* next;
Node(const T& value) : data(value), next(nullptr) {}
};
Node* topPtr;
public:
Stack() : topPtr(nullptr) {}
~Stack() {
makeEmpty();
}
void push(const T& value) {
Node* newNode = new Node(value);
if (isEmpty()) {
topPtr = newNode;
} else {
newNode->next = topPtr;
topPtr = newNode;
}
}
T pop() {
if (isEmpty()) {
// Handle error when stack is empty
} else {
T poppedValue = topPtr->data;
Node* temp = topPtr;
topPtr = topPtr->next;
delete temp;
return poppedValue;
}
}
T peek() const {
if (isEmpty()) {
// Handle error when stack is empty
} else {
return topPtr->data;
}
}
bool isEmpty() const {
return topPtr == nullptr;
}
void makeEmpty() {
while (!isEmpty()) {
pop();
}
}
void print() const {
Node* current = topPtr;
while (current != nullptr) {
std::cout data next;
}
std::cout
Подробнее здесь: [url]https://stackoverflow.com/questions/78183914/im-having-an-error-that-george-does-not-name-a-type-on-line-one-of-txt-file-m[/url]
1710814399
Гость
заголовок стека, включающий реализацию
[code]#ifndef STACK_H
#define STACK_H
#include
template
class Stack {
private:
struct Node {
T data;
Node* next;
Node(const T& value) : data(value), next(nullptr) {}
};
Node* topPtr;
public:
Stack() : topPtr(nullptr) {}
~Stack() {
makeEmpty();
}
void push(const T& value) {
Node* newNode = new Node(value);
if (isEmpty()) {
topPtr = newNode;
} else {
newNode->next = topPtr;
topPtr = newNode;
}
}
T pop() {
if (isEmpty()) {
// Handle error when stack is empty
} else {
T poppedValue = topPtr->data;
Node* temp = topPtr;
topPtr = topPtr->next;
delete temp;
return poppedValue;
}
}
T peek() const {
if (isEmpty()) {
// Handle error when stack is empty
} else {
return topPtr->data;
}
}
bool isEmpty() const {
return topPtr == nullptr;
}
void makeEmpty() {
while (!isEmpty()) {
pop();
}
}
void print() const {
Node* current = topPtr;
while (current != nullptr) {
std::cout data next;
}
std::cout
Подробнее здесь: [url]https://stackoverflow.com/questions/78183914/im-having-an-error-that-george-does-not-name-a-type-on-line-one-of-txt-file-m[/url]