Question
This is the assignment1 package Javalab; import java.util.Scanner; public class Assignment1 { static class stack { int top = -1; //Counter the number in the
This is the assignment1
package Javalab;
import java.util.Scanner;
public class Assignment1
{
static class stack
{
int top = -1;
//Counter the number in the array
char items[] = new char [1000];
//Set the max of the array is 1000
void push(char a)
{
if(top == 999)
{
System.out.println("The stack is full ");
}
else
{
//Just make sure the first element of the stack is the place of the 0
items[++top] = a;
}
}
char pop()
{
if (top
{
System.out.println("The stack is underflow ");
//check the stack is empty or not
return '\0';
}
else
{
char element = items[top];
top--;
return element;
}
}
boolean isEmpty()
{
return(top == -1) ? true: false;
//If the top is not equal -1, it will return false. Otherwise it will return true
}
}
//Matching left and right bracket
static boolean isMatch(char character1, char character2)
{
if(character1 == '(' && character2 == ')')//have to satisfy two option
{
return true;
}
else if(character1 == '{' && character2 == '}')
{
return true;
}
else if(character1 == '[' && character2 == ']')
{
return true;
}
else
{
return false;
}
}
static boolean balanced (char a[])
{
stack s = new stack();
for(int i = 0;i
{
//we put all bracket into one stack then we matching them
if(a[i] == '(' || a[i] == '{' || a[i] == '[')
{
s.push(a[i]);
}
if(a[i] == ')' || a[i] == '}' || a[i] == ']')
{
//first we check the stack is empty or not
if(s.isEmpty())
{
return false;
}
else if( !isMatch(s.pop(),a[i]))//if the result of the function is false
{
return false;
}
}
}
if(s.isEmpty())
{
return true;
}
else
{
return false;
}
}
public static void main(String[] args)
{
System.out.println("Enter the bracket you want to match: ");
Scanner sc= new Scanner(System.in);
String str = sc.nextLine();
// Creating array of string length
char[] ch = new char[str.length()];
// Copy character by character into array
for (int i = 0; i
{
ch[i] = str.charAt(i);
}
if (balanced(ch))
{
System.out.println("Balanced ");
}
else {
System.out.println("Not Balanced ");
}
sc.close();
}
}
3. Implement a stack on the singly linked list with the operations of Lab Assignment 1. Hint: Using the same Stack class you implemented, change the array to an object of the singly linked list class. The functionality of push and pop is now based on the methods of the linked list class. 4. Practical application 1: Balanced parentheses. (a) Implement the balanced-bracket checker algorithm of Lab Assignment 1 using the singly linked list implementation of the stack. (b) Explain how your algorithm checks for balanced parenthesis in O(n), when the input is a string of length n. 5. Implement a queue on the singly linked list of Item #1, with the following operations: enqueue, dequeue, front, size, isEmpty. Note: both operations, enqueue and dequeue, must run in O(1)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started