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...
IN JAVA LANGUAGE Linked List-Based Stack Implementation Implement Stack using a Linked List Use the language...
IN JAVA LANGUAGE Linked List-Based Stack Implementation Implement Stack using a Linked List Use the language library LinkedList Stack methods will call the LinkedList methods You can use string as the object Instead of using an array, as the StackLab did, here you will use a Linked List from your language's library. Implement all the methods of Stack : push(), pop(), size(), printStackDown(), etc, using calls to the linked list methods that correspond to the actions need. In the array...
Please write in Python(Python3) Stack: The following was done already in the Lab in the Stacks...
Please write in Python(Python3) Stack: The following was done already in the Lab in the Stacks Module with Doubly Linked List.(***Code Below***) 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:(*This is what...
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) {...
towers of hanoi c++ program using stacks and singly linked lists.
towers of hanoi c++ program using stacks and singly linked lists.
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT