-
Notifications
You must be signed in to change notification settings - Fork 0
Freature/parentheses balance checking algorithm #11
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
Snaffys
wants to merge
3
commits into
main
Choose a base branch
from
feature/parenthesesBalance
base: main
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
3 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 |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| #include "../../modules/stack/stack.h" | ||
| #include <stdbool.h> | ||
| #include <stdio.h> | ||
|
|
||
| bool checkParenthesesBalance(const char *checkString); | ||
|
|
||
| int main() { | ||
| bool result = checkParenthesesBalance("(Hello!)("); | ||
| printf("%d\n", result); | ||
|
|
||
| result = checkParenthesesBalance(")/-l,,.s3"); | ||
| printf("%d\n", result); | ||
|
|
||
| result = checkParenthesesBalance("({[]})asd()"); | ||
| printf("%d\n", result); | ||
|
|
||
| result = checkParenthesesBalance("([{]})"); | ||
| printf("%d\n", result); | ||
| } | ||
|
|
||
| bool checkParenthesesBalance(const char *checkString) { | ||
| if (checkString == NULL) | ||
| return true; | ||
|
|
||
| Stack *head = NULL; | ||
| int errorCode = 0; | ||
|
|
||
| for (int i = 0; checkString[i] != '\0'; ++i) { | ||
| if (checkString[i] == '(' || checkString[i] == '[' || | ||
| checkString[i] == '{') { | ||
| errorCode = pushToStack(checkString[i], &head); | ||
| if (errorCode == -1) { | ||
| printf("Error: couldn't allocate memory\n"); | ||
| freeStack(head); | ||
| return false; | ||
| } | ||
| } else if (checkString[i] == ')' || checkString[i] == ']' || | ||
| checkString[i] == '}') { | ||
| if (head != NULL) { | ||
| char lastParenthesis = popFromStack(&head); | ||
| if ((checkString[i] == ')' && lastParenthesis != '(') || | ||
| (checkString[i] == ']' && lastParenthesis != '[') || | ||
| (checkString[i] == '}' && lastParenthesis != '{')) { | ||
| freeStack(head); | ||
| return false; | ||
| } | ||
| } else | ||
| return false; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. А что со стеком будет? |
||
| } | ||
| } | ||
|
|
||
| bool isBalanced = (head == NULL); | ||
| freeStack(head); | ||
| return isBalanced; | ||
| } | ||
|
|
||
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 |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| #include "queue.h" | ||
| #include <stdio.h> | ||
| #include <stdlib.h> | ||
|
|
||
| int pushToQueue(char data, Queue **head, Queue **tail) { | ||
| Queue *newQueueNode = (Queue *)malloc(sizeof(Queue)); | ||
| if (newQueueNode == NULL) | ||
| return -1; | ||
|
|
||
| newQueueNode->data = data; | ||
| newQueueNode->next = NULL; | ||
|
|
||
| if (*head == NULL) { | ||
| *head = newQueueNode; | ||
| *tail = newQueueNode; | ||
| } else { | ||
| (*tail)->next = newQueueNode; | ||
| *tail = newQueueNode; | ||
| } | ||
| return 0; | ||
| } | ||
|
|
||
| void printQueue(Queue *head) { | ||
| while (head != NULL) { | ||
| printf("%c ", head->data); | ||
| head = head->next; | ||
| } | ||
| printf("\n"); | ||
| } | ||
|
|
||
| void freeQueue(Queue *head) { | ||
| Queue *current = head; | ||
| while (current != NULL) { | ||
| Queue *next = current->next; | ||
| free(current); | ||
| current = next; | ||
| } | ||
| } | ||
|
|
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 |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| #pragma once | ||
|
|
||
| typedef struct Queue { | ||
| char data; | ||
| struct Queue *next; | ||
| } Queue; | ||
|
|
||
| /** | ||
| * @brief Push element to queue | ||
| * @param data Character to push | ||
| * @param head Pointer to pointer of queue head | ||
| * @param tail Pointer to pointer of queue tail | ||
| * @return -1 if couldn't allocate memory, 0 otherwise | ||
| */ | ||
| int pushToQueue(char data, Queue **head, Queue **tail); | ||
|
|
||
| /** | ||
| * @brief Print all elements in queue | ||
| * @param head Pointer to queue head | ||
| */ | ||
| void printQueue(Queue *head); | ||
|
|
||
| /** | ||
| * @brief Free all queue memory | ||
| * @param head Pointer to queue head | ||
| */ | ||
| void freeQueue(Queue *head); | ||
|
|
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 |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| #include "stack.h" | ||
| #include <stdlib.h> | ||
| #include <stdio.h> | ||
|
|
||
| int pushToStack(char data, Stack **head) { | ||
| Stack *newNode = | ||
| (Stack *)malloc(sizeof(Stack)); | ||
| if (newNode == NULL) | ||
| return -1; | ||
| else { | ||
| newNode->data = data; | ||
| newNode->next = *head; | ||
| *head = newNode; | ||
| return 0; | ||
| } | ||
| } | ||
|
|
||
| char popFromStack(Stack **head) { | ||
| if (*head == NULL) | ||
| return '\0'; | ||
|
|
||
| Stack *lastNode = *head; | ||
| char lastNodeData = lastNode->data; | ||
| *head = (*head)->next; | ||
| free(lastNode); | ||
| return lastNodeData; | ||
| } | ||
|
|
||
| void freeStack(Stack *head) { | ||
| Stack *current = head; | ||
| while (current != NULL) { | ||
| Stack *next = current->next; | ||
| free(current); | ||
| current = next; | ||
| } | ||
| } | ||
|
|
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 |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| #pragma once | ||
|
|
||
| typedef struct Stack { | ||
| char data; | ||
| struct Stack *next; | ||
| } Stack; | ||
|
|
||
| /** | ||
| * @brief Push element to stack | ||
| * @param data Character to push | ||
| * @param head Pointer to pointer of stack head | ||
| * @return -1 if couldn't allocate memory, 0 otherwise | ||
| */ | ||
| int pushToStack(char data, Stack **head); | ||
|
|
||
| /** | ||
| * @brief Pop element from stack | ||
| * @param head Pointer to pointer of stack head | ||
| * @return Popped character or '\0' if stack is empty | ||
| */ | ||
| char popFromStack(Stack **head); | ||
|
|
||
| /** | ||
| * @brief Free all stack memory | ||
| * @param head Pointer to stack head | ||
| */ | ||
| void freeStack(Stack *head); | ||
|
|
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.
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.
А тут же всё должно быть хорошо.