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
13 changes: 11 additions & 2 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)
project(stack)

set(CMAKE_C_STANDARD 99)

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)

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --coverage")

add_executable(benchmark_stack benchmark_stack.c stack.c)

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

#define NUM_ITERATIONS 1000000

void benchmark_push(Stack* stack) {
clock_t start = clock();
for (int i = 0; i < NUM_ITERATIONS; i++) {
push(stack, i);
}
clock_t end = clock();
double elapsed_time = (double)(end - start) / CLOCKS_PER_SEC;
printf("Push benchmark: %d iterations took %.5f seconds.\n", NUM_ITERATIONS, elapsed_time);
}

void benchmark_pop(Stack* stack) {
clock_t start = clock();
for (int i = 0; i < NUM_ITERATIONS; i++) {
pop(stack);
}
clock_t end = clock();
double elapsed_time = (double)(end - start) / CLOCKS_PER_SEC;
printf("Pop benchmark: %d iterations took %.5f seconds.\n", NUM_ITERATIONS, elapsed_time);
}

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

benchmark_push(&stack);

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

benchmark_pop(&stack);

destroyStack(&stack);

return 0;
}
15 changes: 10 additions & 5 deletions 05_hw/main.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "stack.h"

#include <stdio.h>
#include "stdio.h"

int main() {
Stack stack;
Expand All @@ -10,6 +9,7 @@ int main() {
push(&stack, 20);
push(&stack, 30);


printf("After pushing elements:\n");
traverseStack(&stack);

Expand All @@ -21,16 +21,21 @@ int main() {
Node* searchResult = searchByValue(&stack, 20);
if (searchResult != NULL) {
printf("Element with value 20 found.\n");
} else {
}
else {
printf("Element with value 20 not found.\n");
}

Node* topElement = getTop(&stack);
if (topElement != NULL) {
printf("Top element: %d\n", topElement->data);
}
else {
printf("Stack is empty.\n");
}

destroyStack(&stack);
printf("Stack destroyed.\n");

return 0;
}


46 changes: 40 additions & 6 deletions 05_hw/stack.c
Original file line number Diff line number Diff line change
@@ -1,50 +1,79 @@
#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");
return NULL;
}
newNode->data = data;
newNode->next = NULL;
return newNode;
}

void initStack(Stack* stack) {
if (stack == NULL) {
fprintf(stderr, "Stack initialization failed: NULL pointer\n");
return;
}
stack->top = NULL;
}

void destroyStack(Stack* stack) {
if (stack == NULL) {
return;
}
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) {
if (stack == NULL) {
fprintf(stderr, "Push failed: NULL stack pointer\n");
return;
}
Node* newNode = createNode(data);
if (newNode == NULL) {
return;
}
newNode->next = stack->top;
stack->top = newNode;
}

void pop(Stack* stack) {
if (stack == NULL || stack->top == NULL) {
return;
}
Node* temp = stack->top;
stack->top = stack->top->next;
free(temp);
}

Node* searchByValue(Stack* stack, int value) {
if (stack == NULL) {
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) {
return NULL;
}
Node* current = stack->top;
int count = 0;
while (current != NULL) {
Expand All @@ -58,10 +87,17 @@ Node* searchByIndex(Stack* stack, int index) {
}

Node* getTop(Stack* stack) {
if (stack == NULL || stack->top == NULL) {
return NULL;
}
return stack->top;
}

void traverseStack(Stack* stack) {
if (stack == NULL) {
printf("Stack is not initialized.\n");
return;
}
Node* current = stack->top;
printf("Stack elements: ");
while (current != NULL) {
Expand All @@ -72,7 +108,5 @@ void traverseStack(Stack* stack) {
}

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

Loading