Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions CPP/data-structures/stack.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include <iostream>

using namespace std;

template <typename T>
class stack {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/stack/ArrayStack

public:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

variabile publice? why?

T *arr;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tab-uri de 2/4 spatii, te rog

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/arr/array

int top;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ai nevoie si de top si de size?

size_t capacity;
size_t size;

stack (size_t capacity) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

creeaza doar un constructor simplu fara capacitate; nimeni nu foloseste Stack(123), ci Stack()

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

poti pune capacitatea initiala ca o constanta

this->capacity = capacity;
arr = new T[capacity];
top = -1;
size = 0;
}

void push(T data) {
if (size < capacity) {
arr[++top] = data;
++size;
} else {
T *arr2 = new T[capacity * 2];
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/arr2/new_array

for (int i = 0; i < capacity; ++i) {
arr2[i] = arr[i];
}
arr2[++top] = data;
++size;
capacity *= 2;

delete []arr;
arr = arr2;
}
}

void pop() {
if (is_empty()) {
cout << "Stack is empty" << endl;
return;
}
--top;
--size;
}

T peek() {
if (is_empty()) {
exit(-1);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
return arr[top];
}

bool is_empty() {
if (size != 0) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return size == 0;

return false;
}
return true;
}

~stack() {
delete []arr;
}
};

int main()
{
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

un mic proof of concept aici

return 0;
}