forked from czertyaka/prosoft-c-stack
-
Notifications
You must be signed in to change notification settings - Fork 0
Мельников Григорий #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MelnikovGYu
wants to merge
4
commits into
master
Choose a base branch
from
development
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
В заголовочном файле не должно быть изменений, мы не раскрываем детали реализации пользователю