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
7 changes: 7 additions & 0 deletions 05_hw/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,10 @@ add_library(stack stack.c)

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


add_executable(test_stack test_stack.c)
target_link_libraries(test_stack stack)

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

// ��������� ������� ��� push
void benchmark_push(int num_operations) {
Stack stack;
clock_t start_time = clock();

for (int i = 0; i < num_operations; ++i) {
push(&stack, i);
}

clock_t end_time = clock();
double elapsed_time = (double)(end_time - start_time) / CLOCKS_PER_SEC;
printf("Push: %d operations took %f seconds\n", num_operations, elapsed_time);
}

// ��������� ������� ��� pop
void benchmark_pop(int num_operations) {
Stack stack;
for (int i = 0; i < num_operations; ++i) {
push(&stack, i); // ��������� ����
}

clock_t start_time = clock();

for (int i = 0; i < num_operations; ++i) {
pop(&stack);
}

clock_t end_time = clock();
double elapsed_time = (double)(end_time - start_time) / CLOCKS_PER_SEC;
printf("Pop: %d operations took %f seconds\n", num_operations, elapsed_time);
}

int main() {
int num_operations = 1000000; // ���������� ��������

printf("Benchmarking stack operations...\n");
benchmark_push(num_operations);
benchmark_pop(num_operations);

return 0;
}
58 changes: 58 additions & 0 deletions 05_hw/cppcheck_report.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
Checking main.c ...
Defines:
Undefines:
Includes:
Platform:native
1/6 files checked 1% done
Checking out\build\x64-Debug\CMakeFiles\3.29.5-msvc4\CompilerIdC\CMakeCCompilerId.c ...
Defines:
Undefines:
Includes:
Platform:native
Checking out\build\x64-Debug\CMakeFiles\3.29.5-msvc4\CompilerIdC\CMakeCCompilerId.c: INT_178B;__INTEGRITY...
Checking out\build\x64-Debug\CMakeFiles\3.29.5-msvc4\CompilerIdC\CMakeCCompilerId.c: MPRAS;_MPRAS...
Checking out\build\x64-Debug\CMakeFiles\3.29.5-msvc4\CompilerIdC\CMakeCCompilerId.c: SCO_SV;_SCO_SV;sco_sv...
Checking out\build\x64-Debug\CMakeFiles\3.29.5-msvc4\CompilerIdC\CMakeCCompilerId.c: SDCC;__SDCC_VERSION_MAJOR...
Checking out\build\x64-Debug\CMakeFiles\3.29.5-msvc4\CompilerIdC\CMakeCCompilerId.c: SDCC;__SDCC_VERSION_MAJOR;__SDCC_VERSION_MAJOR...
Checking out\build\x64-Debug\CMakeFiles\3.29.5-msvc4\CompilerIdC\CMakeCCompilerId.c: SIMULATE_VERSION_TWEAK...
Checking out\build\x64-Debug\CMakeFiles\3.29.5-msvc4\CompilerIdC\CMakeCCompilerId.c: WIN32;_WIN32;__WIN32__...
Checking out\build\x64-Debug\CMakeFiles\3.29.5-msvc4\CompilerIdC\CMakeCCompilerId.c: XENIX;_XENIX;__XENIX__...
Checking out\build\x64-Debug\CMakeFiles\3.29.5-msvc4\CompilerIdC\CMakeCCompilerId.c: _ADI_COMPILER...
Checking out\build\x64-Debug\CMakeFiles\3.29.5-msvc4\CompilerIdC\CMakeCCompilerId.c: _ADI_COMPILER;__VERSIONNUM__...
Checking out\build\x64-Debug\CMakeFiles\3.29.5-msvc4\CompilerIdC\CMakeCCompilerId.c: _AIX;__AIX;__AIX__;__aix;__aix__...
2/6 files checked 46% done
Checking out\build\x64-Debug\CMakeFiles\3.29.5-msvc4\CompilerIdCXX\CMakeCXXCompilerId.cpp ...
Defines:
Undefines:
Includes:
Platform:native
Checking out\build\x64-Debug\CMakeFiles\3.29.5-msvc4\CompilerIdCXX\CMakeCXXCompilerId.cpp: INT_178B;__INTEGRITY...
Checking out\build\x64-Debug\CMakeFiles\3.29.5-msvc4\CompilerIdCXX\CMakeCXXCompilerId.cpp: MPRAS;_MPRAS...
Checking out\build\x64-Debug\CMakeFiles\3.29.5-msvc4\CompilerIdCXX\CMakeCXXCompilerId.cpp: SCO_SV;_SCO_SV;sco_sv...
Checking out\build\x64-Debug\CMakeFiles\3.29.5-msvc4\CompilerIdCXX\CMakeCXXCompilerId.cpp: SIMULATE_VERSION_TWEAK...
Checking out\build\x64-Debug\CMakeFiles\3.29.5-msvc4\CompilerIdCXX\CMakeCXXCompilerId.cpp: WIN32;_WIN32;__WIN32__...
Checking out\build\x64-Debug\CMakeFiles\3.29.5-msvc4\CompilerIdCXX\CMakeCXXCompilerId.cpp: XENIX;_XENIX;__XENIX__...
Checking out\build\x64-Debug\CMakeFiles\3.29.5-msvc4\CompilerIdCXX\CMakeCXXCompilerId.cpp: _ADI_COMPILER...
Checking out\build\x64-Debug\CMakeFiles\3.29.5-msvc4\CompilerIdCXX\CMakeCXXCompilerId.cpp: _ADI_COMPILER;__VERSIONNUM__...
Checking out\build\x64-Debug\CMakeFiles\3.29.5-msvc4\CompilerIdCXX\CMakeCXXCompilerId.cpp: _AIX;__AIX;__AIX__;__aix;__aix__...
Checking out\build\x64-Debug\CMakeFiles\3.29.5-msvc4\CompilerIdCXX\CMakeCXXCompilerId.cpp: _BEOS;__BEOS__;__BeOS...
Checking out\build\x64-Debug\CMakeFiles\3.29.5-msvc4\CompilerIdCXX\CMakeCXXCompilerId.cpp: _CRAYC...
3/6 files checked 91% done
Checking out\build\x64-Debug\CMakeFiles\ShowIncludes\main.c ...
Defines:
Undefines:
Includes:
Platform:native
4/6 files checked 91% done
Checking stack.c ...
Defines:
Undefines:
Includes:
Platform:native
5/6 files checked 94% done
Checking test_stack.c ...
Defines:
Undefines:
Includes:
Platform:native
6/6 files checked 100% done
4 changes: 2 additions & 2 deletions 05_hw/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ int main() {
printf("After popping an element:\n");
traverseStack(&stack);

Node* searchResult = searchByValue(&stack, 20);
const Node* searchResult = searchByValue(&stack, 20);
if (searchResult != NULL) {
printf("Element with value 20 found.\n");
} else {
printf("Element with value 20 not found.\n");
}

Node* topElement = getTop(&stack);
const Node* topElement = getTop(&stack);
if (topElement != NULL) {
printf("Top element: %d\n", topElement->data);
}
Expand Down
19 changes: 12 additions & 7 deletions 05_hw/stack.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ void initStack(Stack* stack) {
void destroyStack(Stack* stack) {
Node* current = stack->top;
while (current != NULL) {
Node *tmp = current;
Node* tmp = current;
current = current->next;
free(tmp);
free(tmp);
}
stack->top = NULL; // ��������, ��� ��������� �� ������� �������
}

void push(Stack* stack, int data) {
Expand All @@ -30,8 +31,13 @@ void push(Stack* stack, int data) {
}

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

Node* searchByValue(Stack* stack, int value) {
Expand All @@ -40,6 +46,7 @@ Node* searchByValue(Stack* stack, int value) {
if (current->data == value) {
return current;
}
current = current->next; // ������� � ���������� ����
}
return NULL;
}
Expand All @@ -57,7 +64,7 @@ Node* searchByIndex(Stack* stack, int index) {
return NULL;
}

Node* getTop(Stack* stack) {
const Node* getTop(const Stack* stack) {
return stack->top;
}

Expand All @@ -71,8 +78,6 @@ void traverseStack(Stack* stack) {
printf("\n");
}

bool isEmpty(Stack* stack) {
free(stack->top);
bool isEmpty(const Stack* stack) {
return stack->top == NULL;
}

}
87 changes: 87 additions & 0 deletions 05_hw/stack.c.gcov
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
-: 0:Source:stack.c
-: 0:Graph:test_stack-stack.gcno
-: 0:Data:test_stack-stack.gcda
-: 0:Runs:1
-: 1:#include <stdio.h>
-: 2:#include <stdlib.h>
-: 3:
-: 4:#include "stack.h"
-: 5:
20: 6:Node* createNode(int data) {
20: 7: Node* newNode = (Node*)malloc(sizeof(Node));
20: 8: newNode->data = data;
20: 9: newNode->next = NULL;
20: 10: return newNode;
-: 11:}
-: 12:
10: 13:void initStack(Stack* stack) {
10: 14: stack->top = NULL;
10: 15:}
-: 16:
2: 17:void destroyStack(Stack* stack) {
2: 18: Node* current = stack->top;
7: 19: while (current != NULL) {
5: 20: Node* tmp = current;
5: 21: current = current->next;
5: 22: free(tmp);
-: 23: }
2: 24: stack->top = NULL; // ��������, ��� ��������� �� ������� �������
2: 25:}
-: 26:
19: 27:void push(Stack* stack, int data) {
19: 28: Node* newNode = createNode(data);
19: 29: newNode->next = stack->top;
19: 30: stack->top = newNode;
19: 31:}
-: 32:
4: 33:void pop(Stack* stack) {
4: 34: if (stack->top == NULL) {
#####: 35: fprintf(stderr, "Stack underflow\n");
#####: 36: return;
-: 37: }
4: 38: Node* temp = stack->top;
4: 39: stack->top = stack->top->next;
4: 40: free(temp); // ����������� ������ ����
-: 41:}
-: 42:
2: 43:Node* searchByValue(Stack* stack, int value) {
2: 44: Node* current = stack->top;
6: 45: while (current != NULL) {
5: 46: if (current->data == value) {
1: 47: return current;
-: 48: }
4: 49: current = current->next; // ������� � ���������� ����
-: 50: }
1: 51: return NULL;
-: 52:}
-: 53:
4: 54:Node* searchByIndex(Stack* stack, int index) {
4: 55: Node* current = stack->top;
4: 56: int count = 0;
8: 57: while (current != NULL) {
6: 58: if (count == index) {
2: 59: return current;
-: 60: }
4: 61: count++;
4: 62: current = current->next;
-: 63: }
2: 64: return NULL;
-: 65:}
-: 66:
2: 67:Node* getTop(Stack* stack) {
2: 68: return stack->top;
-: 69:}
-: 70:
1: 71:void traverseStack(Stack* stack) {
1: 72: Node* current = stack->top;
1: 73: printf("Stack elements: ");
4: 74: while (current != NULL) {
3: 75: printf("%d ", current->data);
3: 76: current = current->next;
-: 77: }
1: 78: printf("\n");
1: 79:}
-: 80:
3: 81:bool isEmpty(Stack* stack) {
3: 82: return stack->top == NULL;
-: 83:}
4 changes: 2 additions & 2 deletions 05_hw/stack.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ Node* searchByValue(Stack* stack, int value);

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

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

void traverseStack(Stack* stack);

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

#ifdef __cplusplus
}
Expand Down
Loading