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
19 changes: 19 additions & 0 deletions 05_hw/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
cmake_minimum_required(VERSION 3.10)
project(stack)

# ������������� ����� ��� �������� ����
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fprofile-arcs -ftest-coverage")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/../)
set(CMAKE_COMPILE_PDB_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/../)

# ��������� ���������� ��� ���������� �����
add_library(stack stack.c)

# ��������� ����������� ���� main
add_executable(main main.c)
target_link_libraries(main stack)

# ��������� ����������� ���� ��� ������
add_executable(test test.c stack.c)
target_link_libraries(test stack)

# ��������� ����������� ���� ��� ����������
add_executable(benchmark benchmark.c stack.c)
target_link_libraries(benchmark stack)


41 changes: 41 additions & 0 deletions 05_hw/benchmark.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "stack.h"

#define NUM_OPERATIONS 10000000 // Количество операций push/pop

void benchmarkPush(Stack* stack) {
clock_t start = clock(); // Записываем начальное время
for (int i = 0; i < NUM_OPERATIONS; i++) {
push(stack, i); // Выполняем операцию push
}
clock_t end = clock(); // Записываем конечное время
double elapsed_time = ((double)(end - start)) / CLOCKS_PER_SEC; // Считаем время
printf("Benchmark PUSH: %d operations took %.6f seconds\n", NUM_OPERATIONS, elapsed_time);
}

void benchmarkPop(Stack* stack) {
clock_t start = clock(); // Записываем начальное время
for (int i = 0; i < NUM_OPERATIONS; i++) {
pop(stack); // Выполняем операцию pop
}
clock_t end = clock(); // Записываем конечное время
double elapsed_time = ((double)(end - start)) / CLOCKS_PER_SEC; // Считаем время
printf("Benchmark POP: %d operations took %.6f seconds\n", NUM_OPERATIONS, elapsed_time);
}

int main() {
Stack stack;
initStack(&stack);

printf("Starting benchmark for PUSH...\n");
benchmarkPush(&stack);

printf("Starting benchmark for POP...\n");
benchmarkPop(&stack);

destroyStack(&stack); // Чистим стек

return 0;
}
60 changes: 60 additions & 0 deletions 05_hw/cppcheck_report.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
Checking main.c ...
1/8 files checked 0% done
Checking out\build\CMakeFiles\3.31.2\CompilerIdC\CMakeCCompilerId.c ...
Checking out\build\CMakeFiles\3.31.2\CompilerIdC\CMakeCCompilerId.c: INT_178B;__INTEGRITY...
Checking out\build\CMakeFiles\3.31.2\CompilerIdC\CMakeCCompilerId.c: MPRAS;_MPRAS...
Checking out\build\CMakeFiles\3.31.2\CompilerIdC\CMakeCCompilerId.c: SCO_SV;_SCO_SV;sco_sv...
Checking out\build\CMakeFiles\3.31.2\CompilerIdC\CMakeCCompilerId.c: SDCC;__SDCC_VERSION_MAJOR...
Checking out\build\CMakeFiles\3.31.2\CompilerIdC\CMakeCCompilerId.c: SDCC;__SDCC_VERSION_MAJOR;__SDCC_VERSION_MAJOR...
Checking out\build\CMakeFiles\3.31.2\CompilerIdC\CMakeCCompilerId.c: SIMULATE_VERSION_TWEAK...
Checking out\build\CMakeFiles\3.31.2\CompilerIdC\CMakeCCompilerId.c: WIN32;_WIN32;__WIN32__...
Checking out\build\CMakeFiles\3.31.2\CompilerIdC\CMakeCCompilerId.c: XENIX;_XENIX;__XENIX__...
Checking out\build\CMakeFiles\3.31.2\CompilerIdC\CMakeCCompilerId.c: _ADI_COMPILER...
Checking out\build\CMakeFiles\3.31.2\CompilerIdC\CMakeCCompilerId.c: _ADI_COMPILER;__VERSIONNUM__...
Checking out\build\CMakeFiles\3.31.2\CompilerIdC\CMakeCCompilerId.c: _AIX;__AIX;__AIX__;__aix;__aix__...
2/8 files checked 22% done
Checking out\build\CMakeFiles\3.31.2\CompilerIdCXX\CMakeCXXCompilerId.cpp ...
Checking out\build\CMakeFiles\3.31.2\CompilerIdCXX\CMakeCXXCompilerId.cpp: INT_178B;__INTEGRITY...
Checking out\build\CMakeFiles\3.31.2\CompilerIdCXX\CMakeCXXCompilerId.cpp: MPRAS;_MPRAS...
Checking out\build\CMakeFiles\3.31.2\CompilerIdCXX\CMakeCXXCompilerId.cpp: SCO_SV;_SCO_SV;sco_sv...
Checking out\build\CMakeFiles\3.31.2\CompilerIdCXX\CMakeCXXCompilerId.cpp: SIMULATE_VERSION_TWEAK...
Checking out\build\CMakeFiles\3.31.2\CompilerIdCXX\CMakeCXXCompilerId.cpp: WIN32;_WIN32;__WIN32__...
Checking out\build\CMakeFiles\3.31.2\CompilerIdCXX\CMakeCXXCompilerId.cpp: XENIX;_XENIX;__XENIX__...
Checking out\build\CMakeFiles\3.31.2\CompilerIdCXX\CMakeCXXCompilerId.cpp: _ADI_COMPILER...
Checking out\build\CMakeFiles\3.31.2\CompilerIdCXX\CMakeCXXCompilerId.cpp: _ADI_COMPILER;__VERSIONNUM__...
Checking out\build\CMakeFiles\3.31.2\CompilerIdCXX\CMakeCXXCompilerId.cpp: _AIX;__AIX;__AIX__;__aix;__aix__...
Checking out\build\CMakeFiles\3.31.2\CompilerIdCXX\CMakeCXXCompilerId.cpp: _BEOS;__BEOS__;__BeOS...
Checking out\build\CMakeFiles\3.31.2\CompilerIdCXX\CMakeCXXCompilerId.cpp: _CRAYC...
3/8 files checked 45% done
Checking out\build\x64-Debug\CMakeFiles\3.28.3-msvc11\CompilerIdC\CMakeCCompilerId.c ...
Checking out\build\x64-Debug\CMakeFiles\3.28.3-msvc11\CompilerIdC\CMakeCCompilerId.c: INT_178B;__INTEGRITY...
Checking out\build\x64-Debug\CMakeFiles\3.28.3-msvc11\CompilerIdC\CMakeCCompilerId.c: MPRAS;_MPRAS...
Checking out\build\x64-Debug\CMakeFiles\3.28.3-msvc11\CompilerIdC\CMakeCCompilerId.c: SCO_SV;_SCO_SV;sco_sv...
Checking out\build\x64-Debug\CMakeFiles\3.28.3-msvc11\CompilerIdC\CMakeCCompilerId.c: SDCC;__SDCC_VERSION_MAJOR...
Checking out\build\x64-Debug\CMakeFiles\3.28.3-msvc11\CompilerIdC\CMakeCCompilerId.c: SDCC;__SDCC_VERSION_MAJOR;__SDCC_VERSION_MAJOR...
Checking out\build\x64-Debug\CMakeFiles\3.28.3-msvc11\CompilerIdC\CMakeCCompilerId.c: SIMULATE_VERSION_TWEAK...
Checking out\build\x64-Debug\CMakeFiles\3.28.3-msvc11\CompilerIdC\CMakeCCompilerId.c: WIN32;_WIN32;__WIN32__...
Checking out\build\x64-Debug\CMakeFiles\3.28.3-msvc11\CompilerIdC\CMakeCCompilerId.c: XENIX;_XENIX;__XENIX__...
Checking out\build\x64-Debug\CMakeFiles\3.28.3-msvc11\CompilerIdC\CMakeCCompilerId.c: _ADI_COMPILER...
Checking out\build\x64-Debug\CMakeFiles\3.28.3-msvc11\CompilerIdC\CMakeCCompilerId.c: _ADI_COMPILER;__VERSIONNUM__...
Checking out\build\x64-Debug\CMakeFiles\3.28.3-msvc11\CompilerIdC\CMakeCCompilerId.c: _AIX;__AIX;__AIX__;__aix;__aix__...
4/8 files checked 67% done
Checking out\build\x64-Debug\CMakeFiles\3.28.3-msvc11\CompilerIdCXX\CMakeCXXCompilerId.cpp ...
Checking out\build\x64-Debug\CMakeFiles\3.28.3-msvc11\CompilerIdCXX\CMakeCXXCompilerId.cpp: INT_178B;__INTEGRITY...
Checking out\build\x64-Debug\CMakeFiles\3.28.3-msvc11\CompilerIdCXX\CMakeCXXCompilerId.cpp: MPRAS;_MPRAS...
Checking out\build\x64-Debug\CMakeFiles\3.28.3-msvc11\CompilerIdCXX\CMakeCXXCompilerId.cpp: SCO_SV;_SCO_SV;sco_sv...
Checking out\build\x64-Debug\CMakeFiles\3.28.3-msvc11\CompilerIdCXX\CMakeCXXCompilerId.cpp: SIMULATE_VERSION_TWEAK...
Checking out\build\x64-Debug\CMakeFiles\3.28.3-msvc11\CompilerIdCXX\CMakeCXXCompilerId.cpp: WIN32;_WIN32;__WIN32__...
Checking out\build\x64-Debug\CMakeFiles\3.28.3-msvc11\CompilerIdCXX\CMakeCXXCompilerId.cpp: XENIX;_XENIX;__XENIX__...
Checking out\build\x64-Debug\CMakeFiles\3.28.3-msvc11\CompilerIdCXX\CMakeCXXCompilerId.cpp: _ADI_COMPILER...
Checking out\build\x64-Debug\CMakeFiles\3.28.3-msvc11\CompilerIdCXX\CMakeCXXCompilerId.cpp: _ADI_COMPILER;__VERSIONNUM__...
Checking out\build\x64-Debug\CMakeFiles\3.28.3-msvc11\CompilerIdCXX\CMakeCXXCompilerId.cpp: _AIX;__AIX;__AIX__;__aix;__aix__...
Checking out\build\x64-Debug\CMakeFiles\3.28.3-msvc11\CompilerIdCXX\CMakeCXXCompilerId.cpp: _BEOS;__BEOS__;__BeOS...
Checking out\build\x64-Debug\CMakeFiles\3.28.3-msvc11\CompilerIdCXX\CMakeCXXCompilerId.cpp: _CRAYC...
5/8 files checked 88% done
Checking out\build\x64-Debug\CMakeFiles\ShowIncludes\main.c ...
6/8 files checked 88% done
Checking stack.c ...
7/8 files checked 93% done
Checking test.c ...
8/8 files checked 100% done
110 changes: 82 additions & 28 deletions 05_hw/stack.c
Original file line number Diff line number Diff line change
@@ -1,78 +1,132 @@
#include "stack.h"
#include <stdio.h>
#include <stdlib.h>

#include "stack.h"

// �������� ������ ����
Node* createNode(int data) {
Node* newNode = (Node*)malloc(sizeof(Node));
if (newNode == NULL) {
fprintf(stderr, "Memory allocation failed\n");
exit(EXIT_FAILURE);
}
newNode->data = data;
newNode->next = NULL;
return newNode;
}

// ������������� �����
void initStack(Stack* stack) {
if (stack == NULL) {
fprintf(stderr, "Cannot initialize a NULL stack\n");
return;
}
stack->top = NULL;
}

void destroyStack(Stack* stack) {
Node* current = stack->top;
while (current != NULL) {
Node *tmp = current;
current = current->next;
free(tmp);
// ��������, ���� �� ����
bool isEmpty(Stack* stack) {
if (stack == NULL) {
fprintf(stderr, "Cannot check isEmpty on a NULL stack\n");
return true;
}
return stack->top == NULL;
}

// ���������� �������� � ����
void push(Stack* stack, int data) {
if (stack == NULL) {
fprintf(stderr, "Cannot push to a NULL stack\n");
return;
}
Node* newNode = createNode(data);
newNode->next = stack->top;
stack->top = newNode;
}

// �������� �������� �� �����
void pop(Stack* stack) {
if (stack == NULL) {
fprintf(stderr, "Cannot pop from a NULL stack\n");
return;
}
if (isEmpty(stack)) {
fprintf(stderr, "Stack underflow\n");
return;
}
Node* temp = stack->top;
stack->top = stack->top->next;
free(temp);
}

Node* searchByValue(Stack* stack, int value) {
Node* current = stack->top;
while (current != NULL) {
if (current->data == value) {
return current;
}
// ������� �����
void destroyStack(Stack* stack) {
if (stack == NULL) {
fprintf(stderr, "Cannot destroy a NULL stack\n");
return;
}
return NULL;
}

Node* searchByIndex(Stack* stack, int index) {
Node* current = stack->top;
int count = 0;
while (current != NULL) {
if (count == index) {
return current;
}
count++;
Node* temp = current;
current = current->next;
free(temp);
}
return NULL;
stack->top = NULL;
}

// ��������� �������� �������� �����
Node* getTop(Stack* stack) {
if (stack == NULL) {
fprintf(stderr, "Cannot get top of a NULL stack\n");
return NULL;
}
return stack->top;
}

// ������� ��������� �����
void traverseStack(Stack* stack) {
Node* current = stack->top;
if (stack == NULL) {
fprintf(stderr, "Cannot traverse a NULL stack\n");
return;
}
printf("Stack elements: ");
Node* current = stack->top;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}

bool isEmpty(Stack* stack) {
free(stack->top);
return stack->top == NULL;
// ����� �������� �� ��������
Node* searchByValue(Stack* stack, int value) {
if (stack == NULL) {
fprintf(stderr, "Cannot search in a NULL stack\n");
return NULL;
}
Node* current = stack->top;
while (current != NULL) {
if (current->data == value) {
return current;
}
current = current->next;
}
return NULL;
}

// ����� �������� �� �������
Node* searchByIndex(Stack* stack, int index) {
if (stack == NULL) {
fprintf(stderr, "Cannot search by index in a NULL stack\n");
return NULL;
}
Node* current = stack->top;
int i = 0;
while (current != NULL) {
if (i == index) {
return current;
}
current = current->next;
i++;
}
return NULL;
}
20 changes: 10 additions & 10 deletions 05_hw/stack.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,25 @@ typedef struct Stack {
extern "C" {
#endif

Node* createNode(int data);
Node* createNode(int data);

void initStack(Stack* stack);
void initStack(Stack* stack);

void destroyStack(Stack *stack);
void destroyStack(Stack* stack);

void push(Stack* stack, int data);
void push(Stack* stack, int data);

void pop(Stack* stack);
void pop(Stack* stack);

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

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

Node* getTop(Stack* stack);
Node* getTop(Stack* stack);

void traverseStack(Stack* stack);
void traverseStack(Stack* stack);

bool isEmpty(Stack* stack);
bool isEmpty(Stack* stack);

#ifdef __cplusplus
}
Expand Down
Loading