Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Kompilacja na UBUNTU
- g++ -g -Wall -pedantic -Werror stack.h stack.c main.c -o Stack_CPP
- valgrind ./Stack_CPP
- // --------------------------- STACK.H --------------------------- //
- class Stack {
- private:
- int top;
- int *dane;
- int size;
- public:
- Stack();
- // pozostałe metody
- void Push(int element);
- int Pop();
- void Clear();
- bool isEmpty();
- int return_size();
- ~Stack(); // destruktor
- };
- // --------------------------- STACK.CPP --------------------------- //
- #include <stdlib.h>
- #include <iostream>
- #include <assert.h>
- #include "stack.h"
- using namespace std;
- void Stack::Push(int element)
- {
- if (size == top)
- dane = (int*) realloc( dane, sizeof(int) * (size+=1) );
- assert(top < size);
- dane[top++] = element;
- }
- bool Stack::isEmpty()
- {
- return top == 0 ? true : false;
- }
- int Stack::Pop()
- {
- if (isEmpty() == false){
- assert( top > 0 );
- return dane[--top];
- }
- else{
- cout << "stos pusty" << endl;
- return 0;
- }
- }
- void Stack::Clear()
- {
- top = 0;
- }
- Stack::Stack()
- {
- top = 0;
- size = 1;
- dane = (int*) malloc( sizeof(int) * size );
- }
- int Stack::return_size()
- {
- return size;
- }
- Stack::~Stack()
- {
- free(dane);
- }
- // --------------------------- MAIN.CPP --------------------------- //
- #include <iostream>
- #include "stack.h"
- using namespace std;
- int main()
- {
- Stack Stosik; // inicjalizacja stosu, uruchamia się konstruktor i zapełnia pola ( --> stack.cpp)
- for (int i = 0; i < 15; i++)
- Stosik.Push(4*i); // umieszczenie jakichś danych na stosie
- cout << endl;
- for (; Stosik.isEmpty() == 0;)
- {
- cout << " | " << Stosik.Pop() << endl; // wyświetlenie danych
- }
- Stosik.Pop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement