Below you will find pages that utilize the taxonomy term “STACK”
Posts
Stack
INFORMATION Stack is a linear Data Structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first, comes out last.
IMPLEMENTATION C Language .h file typedef struct node_s { void *value; struct node_s *next; } node_t, *stack_t; /* informations */ unsigned int stack_get_size(stack_t stack); bool stack_is_empty(stack_t stack); /* Modifications */ bool stack_push(stack_t *stack_ptr, void *elem); bool stack_pop(stack_t *stack_ptr); void stack_clear(stack_t *stack_ptr); /* Access */ void *stack_top(stack_t stack); .
read morePosts
Valid Parentheses
PROBLEM Given a string s containing just the characters '(' , ')' , '{' , '}' , '[' and ']', determine if the input string is valid.
An input string is valid if:
Open brackets must be closed by the same type of brackets. Open brackets must be closed in the correct order. Every close bracket has a corresponding open bracket of the same type. Example1 *Input*: s = "()" *Output*: true Example2 *Input*: s = "([]){}" *Output*: true Example3 *Input*: s = "(]" *Output*: false SOLVING Steps Create a Stack Browse the string: If characters is an open parentheses push to the stack If characters is an close parentheses If the top of the stack is the open equivalent pop the stack Otherwise return false, the string isn’t closing the last open parentheses Otherwise return false, the character isn’t an open or close parentheses return true if the stack is empty otherwise false because all parentheses aren’t closed Code class Solution { public: bool isValid(string s) { const string open = "({["; const string close = ")}]"; stack<char> parenthesesOpen; for (int i = 0; i < s.
read more