Skip to content
Open
Show file tree
Hide file tree
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
15 changes: 12 additions & 3 deletions 05_hw/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
cmake_minimum_required(VERSION 3.10)
cmake_minimum_required(VERSION 3.17)
project(stack)
find_package(GTest REQUIRED)
find_package(benchmark REQUIRED)

add_library(stack stack.c)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fno-omit-frame-pointer -O3")

add_executable(main main.c)
add_library(stack stack.cpp)
add_executable(main main.cpp)
target_link_libraries(main stack)

add_executable(test_suite_stack test_suite_stack.cpp)
target_link_libraries(test_suite_stack stack GTest::GTest)

add_executable(benchmark benchmark.cpp)
target_link_libraries(benchmark stack benchmark::benchmark)
41 changes: 41 additions & 0 deletions 05_hw/benchmarks.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <benchmark/benchmark.h>
#include "stack.h"

//Stress functions pop and push of class Stack by using Google Bench lib
// See more functions in user guide for lib at github https://github.com/google/benchmark/blob/main/docs/user_guide.md

class StackBench : public benchmark::Fixture {
public:
void SetUp(::benchmark::State& state) {
stack = new Stack();
}

void TearDown(::benchmark::State& state) {
delete stack;
}
Stack* stack;
};


BENCHMARK_DEFINE_F(StackBench, PushTest)(benchmark::State& st) {
for (auto _ : st) {
for(int i=0; i < st.range(0); i++){
stack->push(i);
}
}
}

BENCHMARK_DEFINE_F(StackBench, PopTest)(benchmark::State& st) {
for(int i=0; i < st.range(0); i++){
stack->push(i);
}
for (auto _ : st) {
for(int i=0; i < st.range(0); i++){
stack->pop();
}
}
}
BENCHMARK_REGISTER_F(StackBench, PushTest)->Arg(100000)->Arg(200000)->Arg(400000)->Arg(80000)->Arg(1600000)->Arg(3200000)->MeasureProcessCPUTime();
BENCHMARK_REGISTER_F(StackBench, PopTest)->Arg(100000)->Arg(200000)->Arg(400000)->Arg(80000)->Arg(1600000)->Arg(3200000)->MeasureProcessCPUTime();

BENCHMARK_MAIN();
36 changes: 0 additions & 36 deletions 05_hw/main.c

This file was deleted.

50 changes: 50 additions & 0 deletions 05_hw/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include <iostream>

#include "stack.h"

int main() {
Stack *stack = new Stack();
int index = 0;

stack->push(10);
stack->push(20);
stack->push(30);
std::cout << "After pushing elements:" << std::endl;
stack->traverseStack();

stack->pop();
std::cout << "After popping element:" << std::endl;
stack->traverseStack();

Node* searchResult = stack->searchByValue(20);
if (searchResult != nullptr) {
std::cout << "Element with value 20 found." << std::endl;
} else {
std::cout << "Element with value 20 not found." << std::endl;
}

Node* searchResult2 = stack->searchByValue(50);
if (searchResult2 != nullptr) {
std::cout << "Element with value 50 found." << std::endl;
} else {
std::cout << "Element with value 50 not found." << std::endl;
}

std::cin >> "Enter the index to search for ..." >> index >> std::endl;
Node* searchIndex = stack->searchByIndex(index);
if (searchIndex != nullptr) {
std::cout << "Element with index " << index << " found. The value is: " << searchIndex->data << std::endl;
} else {
std::cout << "Element with index " << index << " not found." << std::endl;
}

Node* topElement = stack->getTop();
if (topElement != nullptr) {
std::cout << "Top element: " << topElement->data << std::endl;
}



delete stack;
return 0;
}
78 changes: 0 additions & 78 deletions 05_hw/stack.c

This file was deleted.

102 changes: 102 additions & 0 deletions 05_hw/stack.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#include <iostream>
#include "stack.h"

void Stack::initStack()
{
this->top = nullptr;
}
void Stack::destroyStack()
{
Node *current = this->top;
while (current != nullptr)
{
Node *tmp = current;
current = current->next;
delete tmp;
}
}

Stack::Stack(){
initStack();
}
Stack::~Stack(){
destroyStack();
}

Node *Stack::createNode(int data)
{
Node *newNode = new Node();
newNode->data = data;
newNode->next = nullptr;
return newNode;
}

bool Stack::isEmpty()
{
return this->top == nullptr;
}

void Stack::push(int data)
{
Node *newNode = createNode(data);
newNode->next = this->top;
this->top = newNode;
}

void Stack::pop()
{
if (isEmpty()) return;
Node *temp = this->top;
this->top = this->top->next;
delete temp;
}

Node *Stack::searchByValue(int value)
{
Node *current = this->top;
while (current != nullptr)
{
if (current->data == value)
{
return current;
}
current = current->next;
}
return nullptr;
}

Node *Stack::searchByIndex(int index)
{
if (index < 0){return nullptr;}
Node *current = this->top;
for (int i = 0; current != nullptr; i++)
{
if (i == index)
{
return current;
}
current = current->next;
}
return nullptr;
}

Node *Stack::getTop()
{
return this->top;
}

void Stack::traverseStack()
{
if (isEmpty())
{
std::cout << "Stack is empty! Please, add value to stack :( " << std::endl;
return;
}
Node *current = this->top;
std::cout << "Elements: ";
while (current != nullptr)
{
std::cout << " [" << current->data << "] ";
current = current->next;
}
}
53 changes: 21 additions & 32 deletions 05_hw/stack.h
Original file line number Diff line number Diff line change
@@ -1,40 +1,29 @@
#pragma once

#include <stdbool.h>
#ifndef STACK_H
#define STACK_H

typedef struct Node {
int data;
struct Node* next;
struct Node *next;
} Node;

typedef struct Stack {
Node* top;
} Stack;

#ifdef __cplusplus
extern "C" {
#endif

Node* createNode(int data);

void initStack(Stack* stack);

void destroyStack(Stack *stack);

void push(Stack* stack, int data);

void pop(Stack* stack);

Node* searchByValue(Stack* stack, int value);

Node* searchByIndex(Stack* stack, int index);

Node* getTop(Stack* stack);

void traverseStack(Stack* stack);

bool isEmpty(Stack* stack);

#ifdef __cplusplus
}
//The Stack class stores data about the stack, as well as methods for working with the stack.
class Stack{
private:
Node* top;
Node* createNode(int data);
void initStack();
void destroyStack();
public:
void push(int data);
void pop();
Node *searchByValue(int value);
Node *searchByIndex(int index);
Node *getTop();
void traverseStack();
bool isEmpty();
Stack();
~Stack();
};
#endif
Loading