Question

In: Computer Science

In C language using stacks: Activity: Using Stack, check whether the following is balanced or not...

In C language using stacks:

Activity: Using Stack, check whether the following is
balanced or not : “{ ( ) } ) ”
-Assume a stack of Max_Size = 6
-Draw the stack for each step and show the value of “top”
-If any decision is made (valid/invalid), then write the
reason 11

Solutions

Expert Solution

code:

#include<stdio.h>
char stack[6];//stack of size 6
int top=-1;
void push(char a)
{
stack[++top]=a;
}
char pop()
{
return stack[top--];
}
int main()
{
char str[20],t;
int i,f=1;
//f is flag bit
scanf("%s",str);
for(i=0;str[i]!='\0';i++)
{
if(str[i]=='('||str[i]=='{'||str[i]=='[')
push(str[i]);//push for a opening paranthesis
if(str[i]==')'||str[i]=='}'||str[i]==']')//pop for a closing parenthesis
{
if(top==-1)//check underflow
f=0;
else
{
t=pop();
//ckecking for mismatch of parenthesis
if(str[i]==')'&&(t=='['||t=='{'))
f=0;
if(str[i]=='}'&&(t=='('||t=='['))
f=0;
if(str[i]==']'&&(t=='{'||t=='('))
f=0;
}
}
}
if(top>=0)//unbalanced case
f=0;
if(f==0)
printf("Unbalanced\n");
else
printf("Balanced\n");
return 0;
}

output:

explanation in each step:


Related Solutions

Using STL stack class, implement in C++ a function that checks for balanced braces { },...
Using STL stack class, implement in C++ a function that checks for balanced braces { }, in a given string / arithmetic expressions.
Stacks & Queues C++ You are given a stack of N integers such that the first...
Stacks & Queues C++ You are given a stack of N integers such that the first element represents the top of the stack and the last element represents the bottom of the stack. You need to pop at least one element from the stack. At any one moment, you can convert stack into a queue. The bottom of the stack represents the front of the queue. You cannot convert the queue back into a stack. Your task is to remove...
TASK: Using stack functions, write a program in C++ language that acts as a simple calculator,...
TASK: Using stack functions, write a program in C++ language that acts as a simple calculator, reading an infix algebraic expression with numbers and simple operations: +, -, *, / , (, and ). The program converts an infix expression into an equivalent postfix expression, and then evaluates the postfix expression, and then prints the result if input expression is correct otherwise prints error messages. Your program must interact with the user until the user quits.    REQUIREMENTS: - Your...
Python Stack: The following was done already in the Lab in the Stacks Module with Doubly...
Python Stack: The following was done already in the Lab in the Stacks Module with Doubly Linked List. Create an application to help you stack and un-stack containers in the ship. Create a class called container which will have the object (data), the link (next) Create a class called Pod which is Stack. Include methods addContainer and removeContainer Implement these classes by creating multiple containers to go inside the pod. ADD the Following feature: Include a class attribute in the...
Write a C++ class that implement two stacks using a single C++ array. That is, it...
Write a C++ class that implement two stacks using a single C++ array. That is, it should have functions pop_first(), pop_second(), push_first(…), push_second(…), size_first(), size_second(), …. When out of space, double the size of the array (similarly to what vector is doing). Notes: Complete all the functions in exercise_2.cpp, then submit this cpp file. pop_first() and pop_second() should throw std::out_of_range exception when stack is empty. CODE: #include <cstdio> #include <stdexcept> template <class T> class TwoStacks { public:   // Constructor, initialize...
Write a C function to implement operation of a stack using the following format: /** *...
Write a C function to implement operation of a stack using the following format: /** * function: *       push * * expects: *       pointer to the stack *       pointer to the size *       the value to push * * returns: *     true when value has been pushed *       false otherwise * * The push function push a value to the passed in stack */ bool push(int *stack, int *size, int max_size, int to_push) {...
Implement a stack in C++ using an array, not an array list. Make your stack size...
Implement a stack in C++ using an array, not an array list. Make your stack size 5 when you test it, but do not hardcode this! You should be able to change the size for testing purposes with the change of one variable. DO NOT use Stack class defined in C++ Implement the following methods in your stack class. stack() creates an empty stacks, stack s is new and empty. push(item) adds a new item to the stack s, stacks...
USING C++ Study the scenario and complete the question(s) that follow: Postfix using Stacks The rules...
USING C++ Study the scenario and complete the question(s) that follow: Postfix using Stacks The rules to convert an infix expression into an equivalent postfix expression are as follows: Suppose infx represents the infix expression and pfx represents the postfix expression. The rules to convert infx into pfx are as follows: 1. Initialize pfx to an empty expression and also initialize the stack. 2. Get the next symbol, sym, from infx. a. If sym is an operand, append sym to...
Using STL stack class, implement in C++ the following pseudocodefunctioncheckBraces(aString: string) that checks for...
Using STL stack class, implement in C++ the following pseudocode functioncheckBraces(aString: string) that checks for balanced braces { } in a given string /arithmetic expressions. Write a main() program to test the function.// Checks the string aString to verify that braces match.// Returns true if aString contains matching braces, false otherwise.checkBraces(aString: string): boolean{aStack = a new empty stackbalancedSoFar = truei =0// Tracks character position in stringwhile (balancedSoFar and i < length of aString) {ch = character at position i in...
Assembly Language. Write a procedure that reverses order of a 1-D array using the stack. The...
Assembly Language. Write a procedure that reverses order of a 1-D array using the stack. The array is a string array, and the address along with the number of elements are passed through the stack. Then write a complete program to test your procedure.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT