From 28e3742406937ea40b83a7065f2c418c0d2ffcfa Mon Sep 17 00:00:00 2001 From: Belov Dmitry Date: Sun, 22 Dec 2024 17:08:57 +0300 Subject: [PATCH 1/6] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D1=8B=20=D1=82=D0=B5=D1=81=D1=82=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 05_hw/CMakeLists.txt | 4 + 05_hw/test_stack.c | 171 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 175 insertions(+) create mode 100644 05_hw/test_stack.c diff --git a/05_hw/CMakeLists.txt b/05_hw/CMakeLists.txt index 6f5e398..c7af9c9 100644 --- a/05_hw/CMakeLists.txt +++ b/05_hw/CMakeLists.txt @@ -5,3 +5,7 @@ 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) diff --git a/05_hw/test_stack.c b/05_hw/test_stack.c new file mode 100644 index 0000000..9fc125b --- /dev/null +++ b/05_hw/test_stack.c @@ -0,0 +1,171 @@ +#include +#include +#include +#include "stack.h" + + +// Тесты +void test_initStack() { + Stack stack; + initStack(&stack); + assert(stack.top == NULL); + printf("Test initStack passed.\n"); +} + +void test_createNode() { + Node* node = createNode(42); + assert(node != NULL && node->data == 42 && node->next == NULL); + free(node); + printf("Test createNode passed.\n"); +} + +void test_push() { + Stack stack; + initStack(&stack); + + push(&stack, 10); + push(&stack, 20); + push(&stack, 30); + + assert(stack.top != NULL); + assert(stack.top->data == 30); + assert(stack.top->next->data == 20); + assert(stack.top->next->next->data == 10); + + printf("Test push passed.\n"); +} + +void test_pop() { + Stack stack; + initStack(&stack); + + push(&stack, 10); + push(&stack, 20); + + pop(&stack); + + assert(stack.top != NULL); + assert(stack.top->data == 10); + + pop(&stack); + + assert(stack.top == NULL); + + printf("Test pop passed.\n"); +} + +void test_searchByValue() { + Stack stack; + initStack(&stack); + + push(&stack, 10); + push(&stack, 20); + push(&stack, 30); + + Node* result = searchByValue(&stack, 20); + assert(result != NULL); + assert(result->data == 20); + + result = searchByValue(&stack, 40); + assert(result == NULL); + + printf("Test searchByValue passed.\n"); +} + +void test_getTop() { + Stack stack; + initStack(&stack); + + push(&stack, 10); + push(&stack, 20); + + Node* topElement = getTop(&stack); + assert(topElement != NULL); + assert(topElement->data == 20); + + pop(&stack); + topElement = getTop(&stack); + assert(topElement != NULL); + assert(topElement->data == 10); + + printf("Test getTop passed.\n"); +} + +void test_traverseStack() { + Stack stack; + initStack(&stack); + + push(&stack, 10); + push(&stack, 20); + push(&stack, 30); + + traverseStack(&stack); + + printf("Test traverseStack passed.\n"); +} + +void test_isEmpty() { + Stack stack; + initStack(&stack); + + assert(isEmpty(&stack) == true); + + push(&stack, 10); + assert(isEmpty(&stack) == false); + + pop(&stack); + assert(isEmpty(&stack) == true); + + printf("Test isEmpty passed.\n"); +} + +void test_searchByIndex() { + Stack stack; + initStack(&stack); + push(&stack, 10); + push(&stack, 20); + push(&stack, 30); + + Node* result = searchByIndex(&stack, 1); + assert(result != NULL && result->data == 20); // Элемент по индексу 1 найден + + result = searchByIndex(&stack, 0); + assert(result != NULL && result->data == 30); // Элемент по индексу 0 найден + + result = searchByIndex(&stack, 5); + assert(result == NULL); // Индекс вне пределов + + // Проверка на пустом стеке + Stack emptyStack; + initStack(&emptyStack); + result = searchByIndex(&emptyStack, 0); + assert(result == NULL); + printf("Test search_by_index passed\n"); + destroyStack(&stack); +} + +void test_DestroyStack() { + Stack stack; + initStack(&stack); + push(&stack, 10); + push(&stack, 20); + destroyStack(&stack); + assert(stack.top == NULL); // После уничтожения вершина должна быть NULL + printf("Test destroy_stack passed\n"); +} + +int main() { + test_createNode(); + test_initStack(); + test_push(); + test_pop(); + test_searchByValue(); + test_searchByIndex(); + test_DestroyStack(); + test_getTop(); + test_traverseStack(); + test_isEmpty(); + + printf("All tests passed!\n"); + return 0; +} From 6f1933c28e51c396300367563134c7848d124240 Mon Sep 17 00:00:00 2001 From: Belov Dmitry Date: Sun, 22 Dec 2024 18:14:06 +0300 Subject: [PATCH 2/6] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 05_hw/stack.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/05_hw/stack.c b/05_hw/stack.c index 0964440..d82a58a 100644 --- a/05_hw/stack.c +++ b/05_hw/stack.c @@ -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) { @@ -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) { @@ -40,6 +46,7 @@ Node* searchByValue(Stack* stack, int value) { if (current->data == value) { return current; } + current = current->next; // Переход к следующему узлу } return NULL; } @@ -72,7 +79,5 @@ void traverseStack(Stack* stack) { } bool isEmpty(Stack* stack) { - free(stack->top); return stack->top == NULL; -} - +} \ No newline at end of file From 83767fc91bd936143a5b4568df09c2cc617d8aaa Mon Sep 17 00:00:00 2001 From: Belov Dmitry Date: Sun, 22 Dec 2024 22:39:58 +0300 Subject: [PATCH 3/6] =?UTF-8?q?=D0=9F=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=B8?= =?UTF-8?q?=D0=BB=20=D0=BF=D0=BE=D0=BA=D1=80=D1=8B=D1=82=D0=B8=D0=B5=20?= =?UTF-8?q?=D0=BA=D0=BE=D0=B4=D0=B0=20=D1=82=D0=B5=D1=81=D1=82=D0=B0=D0=BC?= =?UTF-8?q?=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 05_hw/stack.c.gcov | 87 ++++++++++++++++++++ 05_hw/test_stack.c.gcov | 175 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 262 insertions(+) create mode 100644 05_hw/stack.c.gcov create mode 100644 05_hw/test_stack.c.gcov diff --git a/05_hw/stack.c.gcov b/05_hw/stack.c.gcov new file mode 100644 index 0000000..d5c8c81 --- /dev/null +++ b/05_hw/stack.c.gcov @@ -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 + -: 2:#include + -: 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:} diff --git a/05_hw/test_stack.c.gcov b/05_hw/test_stack.c.gcov new file mode 100644 index 0000000..69d6b67 --- /dev/null +++ b/05_hw/test_stack.c.gcov @@ -0,0 +1,175 @@ + -: 0:Source:test_stack.c + -: 0:Graph:test_stack-test_stack.gcno + -: 0:Data:test_stack-test_stack.gcda + -: 0:Runs:1 + -: 1:#include + -: 2:#include + -: 3:#include + -: 4:#include "stack.h" + -: 5: + -: 6: + -: 7:// Тесты + 1: 8:void test_initStack() { + -: 9: Stack stack; + 1: 10: initStack(&stack); + 1*: 11: assert(stack.top == NULL); + 1: 12: printf("Test initStack passed.\n"); + 1: 13:} + -: 14: + 1: 15:void test_createNode() { + 1: 16: Node* node = createNode(42); + 1*: 17: assert(node != NULL && node->data == 42 && node->next == NULL); + 1: 18: free(node); + 1: 19: printf("Test createNode passed.\n"); + 1: 20:} + -: 21: + 1: 22:void test_push() { + -: 23: Stack stack; + 1: 24: initStack(&stack); + -: 25: + 1: 26: push(&stack, 10); + 1: 27: push(&stack, 20); + 1: 28: push(&stack, 30); + -: 29: + 1*: 30: assert(stack.top != NULL); + 1*: 31: assert(stack.top->data == 30); + 1*: 32: assert(stack.top->next->data == 20); + 1*: 33: assert(stack.top->next->next->data == 10); + -: 34: + 1: 35: printf("Test push passed.\n"); + 1: 36:} + -: 37: + 1: 38:void test_pop() { + -: 39: Stack stack; + 1: 40: initStack(&stack); + -: 41: + 1: 42: push(&stack, 10); + 1: 43: push(&stack, 20); + -: 44: + 1: 45: pop(&stack); + -: 46: + 1*: 47: assert(stack.top != NULL); + 1*: 48: assert(stack.top->data == 10); + -: 49: + 1: 50: pop(&stack); + -: 51: + 1*: 52: assert(stack.top == NULL); + -: 53: + 1: 54: printf("Test pop passed.\n"); + 1: 55:} + -: 56: + 1: 57:void test_searchByValue() { + -: 58: Stack stack; + 1: 59: initStack(&stack); + -: 60: + 1: 61: push(&stack, 10); + 1: 62: push(&stack, 20); + 1: 63: push(&stack, 30); + -: 64: + 1: 65: Node* result = searchByValue(&stack, 20); + 1*: 66: assert(result != NULL); + 1*: 67: assert(result->data == 20); + -: 68: + 1: 69: result = searchByValue(&stack, 40); + 1*: 70: assert(result == NULL); + -: 71: + 1: 72: printf("Test searchByValue passed.\n"); + 1: 73:} + -: 74: + 1: 75:void test_getTop() { + -: 76: Stack stack; + 1: 77: initStack(&stack); + -: 78: + 1: 79: push(&stack, 10); + 1: 80: push(&stack, 20); + -: 81: + 1: 82: Node* topElement = getTop(&stack); + 1*: 83: assert(topElement != NULL); + 1*: 84: assert(topElement->data == 20); + -: 85: + 1: 86: pop(&stack); + 1: 87: topElement = getTop(&stack); + 1*: 88: assert(topElement != NULL); + 1*: 89: assert(topElement->data == 10); + -: 90: + 1: 91: printf("Test getTop passed.\n"); + 1: 92:} + -: 93: + 1: 94:void test_traverseStack() { + -: 95: Stack stack; + 1: 96: initStack(&stack); + -: 97: + 1: 98: push(&stack, 10); + 1: 99: push(&stack, 20); + 1: 100: push(&stack, 30); + -: 101: + 1: 102: traverseStack(&stack); + -: 103: + 1: 104: printf("Test traverseStack passed.\n"); + 1: 105:} + -: 106: + 1: 107:void test_isEmpty() { + -: 108: Stack stack; + 1: 109: initStack(&stack); + -: 110: + 1*: 111: assert(isEmpty(&stack) == true); + -: 112: + 1: 113: push(&stack, 10); + 1*: 114: assert(isEmpty(&stack) == false); + -: 115: + 1: 116: pop(&stack); + 1*: 117: assert(isEmpty(&stack) == true); + -: 118: + 1: 119: printf("Test isEmpty passed.\n"); + 1: 120:} + -: 121: + 1: 122:void test_searchByIndex() { + -: 123: Stack stack; + 1: 124: initStack(&stack); + 1: 125: push(&stack, 10); + 1: 126: push(&stack, 20); + 1: 127: push(&stack, 30); + -: 128: + 1: 129: Node* result = searchByIndex(&stack, 1); + 1*: 130: assert(result != NULL && result->data == 20); // Элемент по индексу 1 найден + -: 131: + 1: 132: result = searchByIndex(&stack, 0); + 1*: 133: assert(result != NULL && result->data == 30); // Элемент по индексу 0 найден + -: 134: + 1: 135: result = searchByIndex(&stack, 5); + 1*: 136: assert(result == NULL); // Индекс вне пределов + -: 137: + -: 138: // Проверка на пустом стеке + -: 139: Stack emptyStack; + 1: 140: initStack(&emptyStack); + 1: 141: result = searchByIndex(&emptyStack, 0); + 1*: 142: assert(result == NULL); + 1: 143: printf("Test search_by_index passed\n"); + 1: 144: destroyStack(&stack); + 1: 145:} + -: 146: + 1: 147:void test_DestroyStack() { + -: 148: Stack stack; + 1: 149: initStack(&stack); + 1: 150: push(&stack, 10); + 1: 151: push(&stack, 20); + 1: 152: destroyStack(&stack); + 1*: 153: assert(stack.top == NULL); // После уничтожения вершина должна быть NULL + 1: 154: printf("Test destroy_stack passed\n"); + 1: 155:} + -: 156: + 1: 157:int main() { + 1: 158: test_createNode(); + 1: 159: test_initStack(); + 1: 160: test_push(); + 1: 161: test_pop(); + 1: 162: test_searchByValue(); + 1: 163: test_searchByIndex(); + 1: 164: test_DestroyStack(); + 1: 165: test_getTop(); + 1: 166: test_traverseStack(); + 1: 167: test_isEmpty(); + -: 168: + 1: 169: printf("All tests passed!\n"); + 1: 170: return 0; + -: 171:} From d22bc7df038436aaeaf93aac6c778979c7b26423 Mon Sep 17 00:00:00 2001 From: Belov Dmitry Date: Mon, 23 Dec 2024 01:10:15 +0300 Subject: [PATCH 4/6] =?UTF-8?q?=D0=97=D0=B0=D0=BF=D1=83=D1=81=D1=82=D0=B8?= =?UTF-8?q?=D0=BB=20=D1=81=D1=82=D0=B0=D1=82=D0=B8=D1=87=D0=B5=D1=81=D0=BA?= =?UTF-8?q?=D0=B8=D0=B9=20=D0=B0=D0=BD=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0=D1=82?= =?UTF-8?q?=D0=BE=D1=80=20=D0=B8=20=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=B8=D0=BB=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 05_hw/cppcheck_report.txt | 58 +++++++++++++++++++++++++++++++++++++++ 05_hw/main.c | 4 +-- 05_hw/stack.c | 4 +-- 05_hw/test_stack.c | 6 ++-- 4 files changed, 65 insertions(+), 7 deletions(-) create mode 100644 05_hw/cppcheck_report.txt diff --git a/05_hw/cppcheck_report.txt b/05_hw/cppcheck_report.txt new file mode 100644 index 0000000..f2d296f --- /dev/null +++ b/05_hw/cppcheck_report.txt @@ -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 diff --git a/05_hw/main.c b/05_hw/main.c index fded814..917a284 100644 --- a/05_hw/main.c +++ b/05_hw/main.c @@ -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); } diff --git a/05_hw/stack.c b/05_hw/stack.c index d82a58a..ce29294 100644 --- a/05_hw/stack.c +++ b/05_hw/stack.c @@ -64,7 +64,7 @@ Node* searchByIndex(Stack* stack, int index) { return NULL; } -Node* getTop(Stack* stack) { +const Node* getTop(const Stack* stack) { return stack->top; } @@ -78,6 +78,6 @@ void traverseStack(Stack* stack) { printf("\n"); } -bool isEmpty(Stack* stack) { +bool isEmpty(const Stack* stack) { return stack->top == NULL; } \ No newline at end of file diff --git a/05_hw/test_stack.c b/05_hw/test_stack.c index 9fc125b..b7d5e42 100644 --- a/05_hw/test_stack.c +++ b/05_hw/test_stack.c @@ -62,7 +62,7 @@ void test_searchByValue() { push(&stack, 20); push(&stack, 30); - Node* result = searchByValue(&stack, 20); + const Node* result = searchByValue(&stack, 20); assert(result != NULL); assert(result->data == 20); @@ -79,7 +79,7 @@ void test_getTop() { push(&stack, 10); push(&stack, 20); - Node* topElement = getTop(&stack); + const Node* topElement = getTop(&stack); assert(topElement != NULL); assert(topElement->data == 20); @@ -126,7 +126,7 @@ void test_searchByIndex() { push(&stack, 20); push(&stack, 30); - Node* result = searchByIndex(&stack, 1); + const Node* result = searchByIndex(&stack, 1); assert(result != NULL && result->data == 20); // Элемент по индексу 1 найден result = searchByIndex(&stack, 0); From 42d2b8377833cf1f549e11464258135287420613 Mon Sep 17 00:00:00 2001 From: Belov Dmitry Date: Mon, 23 Dec 2024 18:33:59 +0300 Subject: [PATCH 5/6] =?UTF-8?q?=D0=97=D0=B0=D0=BF=D1=83=D1=81=D1=82=D0=B8?= =?UTF-8?q?=D0=BB=20=D1=82=D0=B5=D1=81=D1=82=20=D1=81=20=D1=81=D0=B0=D0=BD?= =?UTF-8?q?=D0=B8=D1=82=D0=B0=D0=B9=D0=B7=D0=B5=D1=80=D0=BE=D0=BC=20=D0=B8?= =?UTF-8?q?=20=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=B8=D0=BB=20=D0=BE?= =?UTF-8?q?=D1=88=D0=B8=D0=B1=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 05_hw/stack.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/05_hw/stack.h b/05_hw/stack.h index ef0734e..d9111e5 100644 --- a/05_hw/stack.h +++ b/05_hw/stack.h @@ -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 } From 7d2495abdd59e49717fcfb0d644b761820327146 Mon Sep 17 00:00:00 2001 From: Belov Dmitry Date: Tue, 24 Dec 2024 01:22:59 +0300 Subject: [PATCH 6/6] =?UTF-8?q?=D0=94=D0=BB=D1=8F=20=D0=B1=D0=B0=D0=B7?= =?UTF-8?q?=D0=BE=D0=B2=D1=8B=D1=85=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8?= =?UTF-8?q?=D0=B9=20(push=20/=20pop)=20=D0=BD=D0=B0=D0=BF=D0=B8=D1=81?= =?UTF-8?q?=D0=B0=D0=BB=20=D0=B1=D0=B5=D0=BD=D1=87=D0=BC=D0=B0=D1=80=D0=BA?= =?UTF-8?q?=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 05_hw/CMakeLists.txt | 3 +++ 05_hw/benchmark.c | 46 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 05_hw/benchmark.c diff --git a/05_hw/CMakeLists.txt b/05_hw/CMakeLists.txt index c7af9c9..47259eb 100644 --- a/05_hw/CMakeLists.txt +++ b/05_hw/CMakeLists.txt @@ -9,3 +9,6 @@ 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) \ No newline at end of file diff --git a/05_hw/benchmark.c b/05_hw/benchmark.c new file mode 100644 index 0000000..3180f22 --- /dev/null +++ b/05_hw/benchmark.c @@ -0,0 +1,46 @@ +#include +#include +#include +#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; +}