#include <iostream>
using namespace std;
template <typename T>
class Stack{
private:
static int constexpr maxSize = 100;
T data[maxSize]; // Array mit Daten
int sz; // Stapelzeiger
void error(const char* s) {
cerr << s << endl;
exit(1);
}
public:
Stack(): sz{-1} {
}
void push(T& x) {
if (full()) {
error("voll");
}
data[++sz] = x;
}
void pop() {
if (empty()) {
error("leer");
}
sz--;
}
T top() {
if (empty()) {
error("leer");
}
return data[sz];
}
bool empty() {
return (sz == -1);
}
bool full() {
return (sz == maxSize-1);
}
};
int main() {
Stack<int> s;
int i = 1;
while (!s.full()) {
s.push(i);
i++;
}
cout << s.top() << endl;
for (int i = 1; i <= 90; ++i) {
s.pop();
}
while (!s.empty()) {
cout << s.top() << endl;
s.pop();
}
s.pop();
return 0;
}
#include <iostream>
using namespace std;
template <typename T>
class Queue {
private:
struct Element {
T data;
Element *next;
} *head, *tail;
void error(char const *info) {
cerr << info << endl;
exit(1);
}
public:
Queue() {
head = tail = nullptr;
}
#ifndef NOCOPY
Queue(Queue<T> const & list) {
head = tail = nullptr;
for (Element* e = list.head; e != nullptr; e = e->next) {
enqueue(e->data);
}
}
#endif
T front() {
if (empty()) {
error("leer");
}
return head->data;
}
void enqueue(T& x) {
Element* e = new Element{x, nullptr};
if (empty()) {
head = tail = e;
} else {
tail->next = e;
tail = e;
}
}
void dequeue() {
if (empty()) {
error("leer");
}
Element *e = head;
head = head->next;
if (head == nullptr) {
tail = nullptr;
}
delete e;
}
void debug_print(const char* name) {
cout << name << ": ";
cout << "head = " << head << ", tail = " << tail << endl;
}
void print(const char* name) {
cout << name << ": ";
Element* e = head;
while (e != nullptr){
cout << e->data << "->";
e = e->next;
}
cout << "nullptr" << endl;
}
bool empty() {
return (head == nullptr);
}
bool is_elem(T& x) {
Element* e = head;
while (e != nullptr){
if (e->data == x) {
return true;
}
e = e->next;
}
return false;
}
void clear() {
while (head != nullptr){
Element * e = head;
head = head->next;
delete e;
}
tail = nullptr;
}
Queue<T>& operator=(Queue const & q){
if (this == &q) {
return *this;
}
clear();
Element *e = q.head;
while (e != nullptr) {
enqueue(e->data);
e = e->next;
}
return *this;
}
bool operator==(Queue const & q) {
if (this == &q) {
return true;
}
Element *e1 = head;
Element *e2 = q.head;
// vergleiche alle Elemente:
while (e1 != nullptr && e2 != nullptr) {
if (e1->data != e2->data) {
return false;
}
e1 = e1->next;
e2 = e2->next;
}
return (e1 == e2); // beide müssen nullptr sein
}
Queue<T>& operator+=(Queue const & q){
if (this == &q) {
return *this;
}
Element *e = q.head;
while (e != nullptr) {
enqueue(e->data);
e = e->next;
}
return *this;
}
~Queue(){
clear();
}
};
int main() {
Queue<int> q;
for (int i = 2; i <= 11; ++i) {
q.enqueue(i);
}
for (int i = 1; i <= 5; ++i) {
cout << q.front() << endl;
q.dequeue();
}
while (!q.empty()) {
cout << q.front() << endl;
q.dequeue();
}
if (q.empty()) {
cout << "q ist leer" << endl;
}
return 0;
}
#include <iostream>
#define NOCOPY
using namespace std;
template <typename T>
class Queue {
private:
struct Element {
T data;
Element *next;
} *head, *tail;
void error(char const *info) {
cerr << info << endl;
exit(1);
}
public:
Queue() {
head = tail = nullptr;
}
#ifndef NOCOPY
Queue(Queue<T> const & list) {
head = tail = nullptr;
for (Element* e = list.head; e != nullptr; e = e->next) {
enqueue(e->data);
}
}
#endif
T front() {
if (empty()) {
error("leer");
}
return head->data;
}
void enqueue(T& x) {
Element* e = new Element{x, nullptr};
if (empty()) {
head = tail = e;
} else {
tail->next = e;
tail = e;
}
}
void dequeue() {
if (empty()) {
error("leer");
}
Element *e = head;
head = head->next;
if (head == nullptr) {
tail = nullptr;
}
delete e;
}
void debug_print(const char* name) {
cout << name << ": ";
cout << "head = " << head << ", tail = " << tail << endl;
}
void print(const char* name) {
cout << name << ": ";
Element* e = head;
while (e != nullptr){
cout << e->data << "->";
e = e->next;
}
cout << "nullptr" << endl;
}
bool empty() {
return (head == nullptr);
}
bool is_elem(T& x) {
Element* e = head;
while (e != nullptr){
if (e->data == x) {
return true;
}
e = e->next;
}
return false;
}
void clear() {
while (head != nullptr){
Element * e = head;
head = head->next;
delete e;
}
tail = nullptr;
}
Queue<T>& operator=(Queue const & q){
if (this == &q) {
return *this;
}
clear();
Element *e = q.head;
while (e != nullptr) {
enqueue(e->data);
e = e->next;
}
return *this;
}
bool operator==(Queue const & q) {
if (this == &q) {
return true;
}
Element *e1 = head;
Element *e2 = q.head;
// vergleiche alle Elemente:
while (e1 != nullptr && e2 != nullptr) {
if (e1->data != e2->data) {
return false;
}
e1 = e1->next;
e2 = e2->next;
}
return (e1 == e2); // beide müssen nullptr sein
}
Queue<T>& operator+=(Queue const & q){
if (this == &q) {
return *this;
}
Element *e = q.head;
while (e != nullptr) {
enqueue(e->data);
e = e->next;
}
return *this;
}
~Queue(){
clear();
}
};
int main() {
Queue<int> q;
for (int i = 1; i <=5; ++i) {
q.enqueue(i);
}
Queue<int> q2 = q;
cout << "q.front() = " << q.front() << ", q2.front() = " << q2.front() << endl;
q.debug_print("q ");
q2.debug_print("q2");
cout << endl;
q.dequeue();
cout << "q.front() = " << q.front() << ", q2.front() = " << q2.front() << endl;
q.debug_print("q ");
q2.debug_print("q2");
return 0;
}
#include <iostream>
using namespace std;
template <typename T>
class Queue {
private:
struct Element {
T data;
Element *next;
} *head, *tail;
void error(char const *info) {
cerr << info << endl;
exit(1);
}
public:
Queue() {
head = tail = nullptr;
}
#ifndef NOCOPY
Queue(Queue<T> const & list) {
head = tail = nullptr;
for (Element* e = list.head; e != nullptr; e = e->next) {
enqueue(e->data);
}
}
#endif
T front() {
if (empty()) {
error("leer");
}
return head->data;
}
void enqueue(T& x) {
Element* e = new Element{x, nullptr};
if (empty()) {
head = tail = e;
} else {
tail->next = e;
tail = e;
}
}
void dequeue() {
if (empty()) {
error("leer");
}
Element *e = head;
head = head->next;
if (head == nullptr) {
tail = nullptr;
}
delete e;
}
void debug_print(const char* name) {
cout << name << ": ";
cout << "head = " << head << ", tail = " << tail << endl;
}
void print(const char* name) {
cout << name << ": ";
Element* e = head;
while (e != nullptr){
cout << e->data << "->";
e = e->next;
}
cout << "nullptr" << endl;
}
bool empty() {
return (head == nullptr);
}
bool is_elem(T& x) {
Element* e = head;
while (e != nullptr){
if (e->data == x) {
return true;
}
e = e->next;
}
return false;
}
void clear() {
while (head != nullptr){
Element * e = head;
head = head->next;
delete e;
}
tail = nullptr;
}
Queue<T>& operator=(Queue const & q){
if (this == &q) {
return *this;
}
clear();
Element *e = q.head;
while (e != nullptr) {
enqueue(e->data);
e = e->next;
}
return *this;
}
bool operator==(Queue const & q) {
if (this == &q) {
return true;
}
Element *e1 = head;
Element *e2 = q.head;
// vergleiche alle Elemente:
while (e1 != nullptr && e2 != nullptr) {
if (e1->data != e2->data) {
return false;
}
e1 = e1->next;
e2 = e2->next;
}
return (e1 == e2); // beide müssen nullptr sein
}
Queue<T>& operator+=(Queue const & q){
if (this == &q) {
return *this;
}
Element *e = q.head;
while (e != nullptr) {
enqueue(e->data);
e = e->next;
}
return *this;
}
~Queue(){
clear();
}
};
int main() {
Queue<int> q;
for (int i = 1; i <=5; ++i) {
q.enqueue(i);
}
q.print("q");
Queue<int> q2{q};
q2.print("q2");
cout << "q == q2: " << (q == q2) << endl;
cout << endl;
q2 += q;
q2.print("q2 += q");
cout << "q == q2: " << (q == q2) << endl;
cout << endl;
q2 = q;
q2.print("q2 = q");
return 0;
}
#include <iostream>
using namespace std;
class MyClass {
int m_x;
public:
MyClass() = default;
MyClass(int y): m_x(y) {}
MyClass(MyClass const & mc) : m_x(mc.m_x + 3) {}
MyClass& operator=(MyClass const &mc) {
m_x = mc.m_x + 10;
return *this;
}
~MyClass() = default;
int getX() { return m_x;}
};
int main() {
MyClass a(3);
cout << a.getX() << endl;
MyClass b(3);
b = a;
cout << b.getX() << endl;
return 0;
}
sz
vs. sz, ez
#include <iostream>
using namespace std;
template <typename T>
class BinTree {
private:
struct Node{
T data;
Node *left, *right;
} *root;
Node* insert(Node *node, T x) {
if (node == nullptr) {
Node *node = new Node{x, nullptr, nullptr};
return node;
}
if (node->data > x) {
node->left = insert(node->left, x);
} else if (node->data < x) {
node->right = insert(node->right, x);
}
return node;
}
bool is_elem(Node *node, T x) {
if (node == nullptr) {
return false;
}
if (node->data == x) {
return true;
}
if (node->data > x) {
return is_elem(node->left, x);
}
return is_elem(node->right, x);
}
void clear(Node *node) {
if (node == nullptr) {
return;
}
clear(node->left);
clear(node->right);
delete node;
}
void print(Node *node) {
if (node == nullptr) {
return;
}
print(node->left);
cout << node->data << " ";
print(node->right);
}
int height(Node *node) {
if (node == nullptr) {
return 0;
}
int left = height(node->left);
int right = height(node->right);
if (left > right) {
return left + 1;
} else {
return right + 1;
}
}
public:
BinTree() {
root = nullptr;
}
void insert(T x) {
root = insert(root, x);
}
bool is_elem(T x) {
return is_elem(root, x);
}
void clear() {
clear(root);
root = nullptr;
}
void print() {
print(root);
cout << endl;
}
int height() {
return height(root);
}
~BinTree() {
clear();
}
};
int main() {
BinTree<int> tree;
tree.insert(20);
tree.insert(40);
tree.insert(42);
tree.insert(55);
tree.insert(25);
tree.insert(11);
tree.insert(18);
tree.insert(19);
tree.insert(8);
tree.insert(9);
tree.insert(5);
cout << "Höhe des Baums: " << tree.height() << endl;
tree.print();
return 0;
}