This programming problem deals with the Linked List (LLStack) implementation.
a. Create a simple user interface to allow a user to input an expression and store it as a string variable.
b. Write a function using the LLStack implementation to check if the input string is a well-formed parenthetical expression. Assume (), {}, and [] are all parenthetical characters. Recall that a parenthetical expression is well-formed if
i. All closing parentheses match the most recent, unmatched opening parentheses of the same type.
ii. All opening parentheses have matching closing parentheses of the same type by the end of the expression.
The function should return true if the expression is well-formed, and false otherwise.
#include <bits/stdc++.h>
using namespace std;
// linked list node
struct Node {
char data;
struct Node* link;
};
//linked list head
struct Node* top;
// function to add an element data in the stack
// insert at the beginning
void push(char data)
{
// create new node temp and allocate memory
struct Node* temp;
temp = new Node();
// check if stack (heap) is full. Then inserting an
element would
// lead to stack overflow
if (!temp) {
cout << "\nHeap
Overflow";
exit(1);
}
// initialize data into temp data field
temp->data = data;
// put top pointer reference into temp link
temp->link = top;
// make temp as top of Stack
top = temp;
}
// Utility function to check if the stack is empty or not
int isEmpty()
{
return top == NULL;
}
// Utility function to return top element in a stack
char peek()
{
// check for empty stack
if (!isEmpty())
return top->data;
else
exit(1);
}
// Utility function to pop top
// element from the stack
void pop()
{
struct Node* temp;
// check for stack underflow
if (top == NULL) {
cout << "\nStack Underflow"
<< endl;
exit(1);
}
else {
// top assign into temp
temp = top;
// assign second node to
top
top = top->link;
// destroy connection between
first and second
temp->link = NULL;
// release memory of top
node
free(temp);
}
}
// Function to print all the
// elements of the stack
void display()
{
struct Node* temp;
// check for stack underflow
if (top == NULL) {
cout << "\nStack
Underflow";
exit(1);
}
else {
temp = top;
while (temp != NULL) {
// print node
data
cout <<
temp->data << " ";
// assign
temp link to temp
temp =
temp->link;
}
}
}
bool balancedParanthesis(string expr)
{
char x;
// Traversing the Expression
for (int i=0; i<expr.length(); i++)
{
if (expr[i]=='('||expr[i]=='['||expr[i]=='{')
{
// Push the element in the stack
push(expr[i]);
continue;
}
// IF current current character is not opening
// bracket, then it must be closing. So stack
// cannot be empty at this point.
if (isEmpty())
return false;
switch (expr[i])
{
case ')':
// Store the top element in a
x = peek();
pop();
if (x=='{' || x=='[')
return false;
break;
case '}':
// Store the top element in b
x = peek();
pop();
if (x=='(' || x=='[')
return false;
break;
case ']':
// Store the top element in c
x = peek();
pop();
if (x =='(' || x == '{')
return false;
break;
}
}
// Check Empty Stack
return (isEmpty());
}
// Driver Code
int main()
{
string s;
cout<<"enter the expression : ";
cin>>s;
if(balancedParanthesis(s))
cout<<"true"<<endl;
else
cout<<"false"<<endl;
return 0;
}
// If you got the answer, please upvote :)
This programming problem deals with the Linked List (LLStack) implementation. a. Create a simple user interface...
Please solve the following problem with programming using proper data structures. (Programming Language: Python) A similar application to the parentheses matching problem comes from hypertext markup language (HTML). In HTML, tags exist in both opening and closing forms and must be balanced to properly describe a web document. This very simple HTML document: Example> Hello, world is intended only to show the matching and nesting structure for tags in the language. Write a program that can check an HTML document...
Insert elements into a hash table implemented using chain hashing, as an array of linked list in which each entry slot is as a linked list of key/value pairings that have the same hash (outcome value computed using certain hash function). You are allowed to use “Hash Function”, h(k) = k % x where, x is the value you will need to decide to use, that you find appropriate for this implementation. The main requirements are the following: 1. Input:...
C++ Create a User class, with a separate interface (User.h) and implementation (User.cpp), comprised of the following attributes: Data members (private): string: username int array: ratings Number of elements should be size int: numRatings Number of books in the database Int: size The capacity of the ratings array (50). Constant Member functions (public): Default constructor Sets username to an empty string, numRatings to 0, size to 50, and all the elements of ratings array to the value 0 Parameterized constructor...
In this assignment, you will implement a deterministic finite automata (DFA) using C++ programming language to extract all matching patterns (substrings) from a given input DNA sequence string. The alphabet for generating DNA sequences is {A, T, G, C}. Write a regular expression that represents all DNA strings that contains at least two ‘A’s. Note: assume empty string is not a valid string. Design a deterministic finite automaton to recognize the regular expression. Write a program which asks the user...
IN C++ MUST INCLUDE HEADER FILE, IMPLEMENTATION FILE, AND DRIVER FILE. IN C++ Create a header and implementation file to define an apartment class. Create a driver program to test the class, and create a make file to compile the driver program. Create two files called apartment.h and appartmentImp.cpp along with creating a driver program named testApartment.cpp containing the main function. Program Requirements: • Class attributes should include integers for number of rooms, monthly rent, and square feet, as well...
A palindrome is any word, phrase, or sentence that reads the same forward and backward. Here are some well-known palindromes: Able was I, ere I saw Elba A man, a plan, a canal, Panama Desserts, I stressed Kayak Write a program that determine whether an input string is a palindrome or not. Input: The program should prompt the user "Please enter a string to test for palindrome or type QUIT to exit: " and then wait for the user input....
Palindrome Detector C++ A palindrome is any word, phrase, or sentence that reads the same forward and backward. Here are some well-known palindromes: Able was I, ere I saw Elba A man, a plan, a canal, Panama Desserts, I stressed Kayak Write a program that determine whether an input string is a palindrome or not. Input: The program should prompt the user "Please enter a string to test for palindrome or type QUIT to exit: " and then wait for...
Assignment4: Evaluate Arithmetic Expressions. Requirements: Implement a concrete ArrayStack class that extends the IStack interface as we discussed in the class (any other different Stack class implementation, even if it is implemented by yourself, will not receive any credit). Write a test class called Evaluate and a method that evaluates an arithmatic expression, which is given by a string. import java.util.Scanner; public class Evaluate { public static void main(String[] args) } // your implementation // obtain user's input from keyboard...
Programming languages allow the programmer to create his or her own functions which are called __________ __________ functions. The function that returns the number of characters in a string is the __________ function. __________ files contain records that must be processed in the order in which they were created. The elements of an array are stored in __________ storage locations in the computer’s memory. A variable that is declared outside all program modules, including the main module, has __________ scope....
I need help with the Implementation of an Ordered List (Template Files) public interface OrderedStructure { public abstract int size(); public abstract boolean add( Comparable obj ) throws IllegalArgumentException; public abstract Object get( int pos ) throws IndexOutOfBoundsException; public abstract void remove( int pos ) throws IndexOutOfBoundsException; public abstract void merge( OrderedList other ); } import java.util.NoSuchElementException; public class OrderedList implements OrderedStructure { // Implementation of the doubly linked nodes (nested-class) private static class Node { private Comparable value; private...