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
143 changes: 129 additions & 14 deletions cstack.c
Original file line number Diff line number Diff line change
@@ -1,42 +1,157 @@
#include "cstack.h"
#include <stddef.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define UNUSED(VAR) (void)(VAR)
#define TABLE_SIZE 20

struct node
{
struct node* prev; /* ��������� �� ���������� ������� �����*/
unsigned int size; /*������ ������ �������� ���� � ������*/
char data[0]; /*��������� �� ������ � �������� ����������� ��� �������� ����� ����� ������*/
};
typedef struct node* stack_t; /* ���������� ��������� ���� �����*/


struct stack_entries_table /*��� ������ � ������� �������� �������� ��������� �� ��������� struct node*/
{
unsigned int size;
stack_t* entries[TABLE_SIZE];
};


/* ������������� ������� ������� - ������� �����*/

struct stack_entries_table g_table = {
TABLE_SIZE,
{NULL}
};

hstack_t stack_new(void)
{
for (unsigned int i = 0; i < g_table.size; i++){
if (!g_table.entries[i]) {
g_table.entries[i] = malloc(sizeof(stack_t)); /* ���� ��������, �� �������� ��� � ��������� ������ i ��� �������*/
*g_table.entries[i] = NULL; /* ���� �����, �� ���� ��� �� �������� �� ������ ����(node) � ���� */
return i;
}
}
//printf("Error stack_new: Memory of table is full\n");
return -1;
}

void stack_free(const hstack_t hstack)
{
UNUSED(hstack);
/* �������� �� ������������� �����*/
if (hstack<0) {
//printf("Error stack_free: hstack can not be < 0\n");
return;
}

if (!g_table.entries[hstack]) {
//printf("Error stack_free: Stack does not exist\n");
return;
}
stack_t head = *g_table.entries[hstack]; /* head ��������� �� ������� ����� � ������� �� ������ hstack*/
while (head != NULL) { /* ���� head �� ��������� �� Null */
stack_t tmp = head; /* ��������� ����� ������� ������� �� ��������� ��������� */
head = head->prev; /* ������ ��������� ������� �� ����� ����������� �������� � ����� */
free(tmp); /* ����������� ������ ������� �������*/
}
free(g_table.entries[hstack]); /* ����������� ����� � ������� ��� ����� ������*/
g_table.entries[hstack] = NULL;
//printf("stack_free: Stack %d is free\n", hstack);
}

int stack_valid_handler(const hstack_t hstack)
{
UNUSED(hstack);
return 1;
if (hstack < 0) {
//printf("Error stack_free: hstack can not be < 0\n");
return 1;
}
if (!g_table.entries[hstack]) {
//printf("Stack does not exist\n");
return 1;
}
return 0;
}

unsigned int stack_size(const hstack_t hstack)
{
UNUSED(hstack);
return 0;
/* �������� �� ������������� �����*/
if (hstack < 0) {
//printf("Error stack_free: hstack can not be < 0\n");
return 0;
}
if (!g_table.entries[hstack]) {
//printf("Error stack_size: Stack does not exist\n");
return 0;
}

stack_t head = *g_table.entries[hstack];
unsigned int stack_size = 0; /* ������ �����*/
while (head != NULL) { /* ���� �� ���� */
head = head->prev; /* ��������� � ���������� */
stack_size++; /* ����������� ������ */
}
return stack_size;
}

void stack_push(const hstack_t hstack, const void* data_in, const unsigned int size)
{
UNUSED(hstack);
UNUSED(data_in);
UNUSED(size);
/* �������� �� ������������ ����������*/
if (hstack < 0) {
//printf("Error stack_free: hstack can not be < 0\n");
return;
}
if (!g_table.entries[hstack]) {
//printf("Error stack_push:Stack does not exist\n");
return;
}
if (!data_in || !size) {
//printf("Error stack_push: Wrong arg\n");
return;
}
stack_t new_node = malloc(sizeof(struct node) + size); // �������� ������ ��� ����
memcpy(new_node->data, data_in, size); // �������� ������ � ����� ����
new_node->size = size; // ������ ������ � ������ data
new_node->prev = *g_table.entries[hstack]; // ��������� � ����-�� ��������
*g_table.entries[hstack] = new_node; // ��������� ������� �����
//printf("stack_push: Element was added\n");
}

unsigned int stack_pop(const hstack_t hstack, void* data_out, const unsigned int size)
{
UNUSED(hstack);
UNUSED(data_out);
UNUSED(size);
return 0;
/* �������� �� ������������ ����������*/
if (hstack < 0) {
//printf("Error stack_free: hstack can not be < 0\n");
return 0;
}
if (!g_table.entries[hstack]) {
//printf("Error stack_pop: Stack does not exist\n");
return 0;
}
if (!data_out || !size) {
//printf("Error stack_pop: Wrong arg\n");
return 0;
}
stack_t head = *g_table.entries[hstack]; // �������� � head ������� ����� �� ������� ������
if (!head){
//printf("Error stack_pop: stack is empty\n");
return 0;
}
if (head->size > size) { // ��������� ��������� �� ������ ������ �������� ����� � �������� ������ data_out
//printf("Error stack_pop: The buffer size is smaller than the data being written\n");
return 0;
}
memcpy(data_out, head->data, head->size); // ���������� � data_out ������ �� �������� �����
unsigned int recorded_data = head->size;
stack_t tmp = head; /* ��������� ����� ������� ������� �� ��������� ��������� */
head = head->prev; /* ������ ��������� ������� �� ����� ����������� �������� � ����� */
free(tmp); /* ����������� ������ ������� �������*/
*g_table.entries[hstack] = head; /* ������ ������� ����� ��� ������� hstack � ������� ������*/
return recorded_data;
}

2 changes: 2 additions & 0 deletions cstack.h
Copy link
Copy Markdown
Collaborator

@czertyaka czertyaka Nov 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

В заголовочном файле не должно быть изменений, мы не раскрываем детали реализации пользователю

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

typedef int hstack_t;



hstack_t stack_new(void);
void stack_free(const hstack_t stack);
int stack_valid_handler(const hstack_t stack);
Expand Down