-
Notifications
You must be signed in to change notification settings - Fork 0
ДЗ № 5.1 Продвинутый баланс скобок, Шестаков Николай #9
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
NicholayShestakov
wants to merge
15
commits into
main
Choose a base branch
from
advanced_bracket_balance
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
15 commits
Select commit
Hold shift + click to select a range
345bc50
Add stack library header file
NicholayShestakov 4737d32
Add stack library realisation file
NicholayShestakov 405bf4c
Update stack header file commentaries
NicholayShestakov ce89c99
Update stack realisation file
NicholayShestakov f4d1e69
Update stack realisation file
NicholayShestakov d389c7d
Update stack header file
NicholayShestakov ceeeb02
Update stack header file
NicholayShestakov f344512
Update stack realisation file
NicholayShestakov caa1bf9
Update stack header file
NicholayShestakov 0d99e19
Update stack realisation file
NicholayShestakov 7f111b6
Add advanced bracket balance homework
NicholayShestakov f70cccd
Update advanced bracket balance homework
NicholayShestakov a64578d
Update bracket balance checker
NicholayShestakov 0af264a
Update bracket balance checker
NicholayShestakov 41b1bd6
Add compile instruction for advanced bracket balance
NicholayShestakov 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,4 @@ | ||
| ## Compile inctruction for advanced brackets balance | ||
| gcc -Wall -Wextra -pedantic -O2 stack.c -c | ||
|
|
||
| gcc -Wall -Wextra -pedantic -O2 stack.o advanced_bracket_balance.c |
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,79 @@ | ||
| #include "stack.h" | ||
| #include <stdbool.h> | ||
| #include <stdio.h> | ||
| #include <string.h> | ||
|
|
||
| bool isBracket(char c) | ||
| { | ||
| return (c == '(' || c == ')' || c == '[' || c == ']' || c == '{' || c == '}'); | ||
| } | ||
|
|
||
| // Checks brackets balance. | ||
| // Returns true if brackets balanced and false if not. | ||
| // Takes string. | ||
| bool isBracketsBalanced(char* string) | ||
| { | ||
| int size = strlen(string); | ||
| Stack* brackets = newStack(); | ||
|
|
||
| for (int i = 0; i < size; i++) { | ||
| // If char is opening bracket, pushes individual for all bracket types number in the stack. | ||
| // If char is closing bracket, pops number from the stack. If number is not right, returns false. | ||
| if (isBracket(string[i])) { | ||
| if (string[i] == '(') { | ||
| push(brackets, 1); | ||
NicholayShestakov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| if (string[i] == '[') { | ||
| push(brackets, 2); | ||
| } | ||
| if (string[i] == '{') { | ||
| push(brackets, 3); | ||
| } | ||
| if (string[i] == ')') { | ||
| if (pop(brackets) != 1) { | ||
| deleteStack(brackets); | ||
| return false; | ||
NicholayShestakov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| if (string[i] == ']') { | ||
| if (pop(brackets) != 2) { | ||
| deleteStack(brackets); | ||
| return false; | ||
| } | ||
| } | ||
| if (string[i] == '}') { | ||
| if (pop(brackets) != 3) { | ||
| deleteStack(brackets); | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| // Check for all brackets closed. | ||
| if (!isEmpty(brackets)) { | ||
| deleteStack(brackets); | ||
| return false; | ||
| } | ||
|
|
||
| deleteStack(brackets); | ||
| return true; | ||
| } | ||
|
|
||
| int main() | ||
| { | ||
| char a[] = "T(e){s}[t]"; | ||
| char b[] = "123"; | ||
| char c[] = ""; | ||
| char d[] = "("; | ||
| char e[] = "({a)}"; | ||
| char f[] = "((({{{[[[]]]}}})))"; | ||
|
|
||
| printf("%s - %d\n", a, isBracketsBalanced(a)); | ||
| printf("%s - %d\n", b, isBracketsBalanced(b)); | ||
| printf("%s - %d\n", c, isBracketsBalanced(c)); | ||
| printf("%s - %d\n", d, isBracketsBalanced(d)); | ||
| printf("%s - %d\n", e, isBracketsBalanced(e)); | ||
| printf("%s - %d\n", f, isBracketsBalanced(f)); | ||
|
|
||
| return 0; | ||
| } | ||
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,64 @@ | ||
| #include "stack.h" | ||
|
|
||
| #include <stdbool.h> | ||
| #include <stdio.h> | ||
| #include <stdlib.h> | ||
|
|
||
| typedef struct StackNode { | ||
| int value; | ||
| struct StackNode* next; | ||
| } StackNode; | ||
|
|
||
| struct Stack { | ||
| StackNode* head; | ||
| }; | ||
|
|
||
| Stack* newStack() | ||
| { | ||
| Stack* stack = malloc(sizeof(*stack)); | ||
| stack->head = NULL; | ||
| return stack; | ||
| } | ||
|
|
||
| void deleteStack(Stack* stack) | ||
| { | ||
| while (stack->head != NULL) { | ||
| pop(stack); | ||
| } | ||
| free(stack); | ||
| } | ||
|
|
||
| void push(Stack* stack, int value) | ||
| { | ||
| StackNode* newNode = malloc(sizeof(*newNode)); | ||
| newNode->value = value; | ||
| newNode->next = stack->head; | ||
| stack->head = newNode; | ||
| } | ||
|
|
||
| int pop(Stack* stack) | ||
| { | ||
| if (isEmpty(stack)) { | ||
| return -1; | ||
| } | ||
NicholayShestakov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| StackNode* poppedNode = stack->head; | ||
| int value = poppedNode->value; | ||
| stack->head = poppedNode->next; | ||
| free(poppedNode); | ||
| return value; | ||
| } | ||
|
|
||
| int peek(Stack* stack) | ||
| { | ||
| if (isEmpty(stack)) { | ||
| return -1; | ||
| } | ||
|
|
||
| return stack->head->value; | ||
| } | ||
|
|
||
| bool isEmpty(Stack* stack) | ||
| { | ||
| return stack->head == NULL; | ||
| } | ||
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,31 @@ | ||
| #pragma once | ||
|
|
||
| #include <stdbool.h> | ||
|
|
||
| typedef struct Stack Stack; | ||
NicholayShestakov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Creates new stack. | ||
| // Returns pointer on the created stack. | ||
| Stack* newStack(); | ||
|
|
||
| // Deletes stack. Frees all memory used in it. | ||
| // Takes pointer on the stack. | ||
NicholayShestakov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| void deleteStack(Stack* stack); | ||
|
|
||
| // Pushes new value on the top of the stack. | ||
| // Takes pointer on the stack and value for push. | ||
| void push(Stack* stack, int value); | ||
|
|
||
| // Deletes the top value of the stack and returns it. Frees memory used for top node. | ||
| // Takes pointer on the stack. | ||
| // If the stack is empty returns -1. | ||
| int pop(Stack* stack); | ||
|
|
||
| // Returns the top value of the stack. | ||
| // Takes pointer on the stack. | ||
| // If the stack is empty returns -1. | ||
| int peek(Stack* stack); | ||
|
|
||
| // Returns true if the stack is empty and false if not. | ||
| // Takes pointer on the stack. | ||
| bool isEmpty(Stack* stack); | ||
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.