In C programming,
Modify the function Pop in the example so that it has the signature
bool Pop(LIST *list, char *c)
and returns false if the list is empty and returns true if not empty. On success it returns the value removed from the stack in the variable c. Modify the function CheckForBalance to accommodate this change and rerun the test program giving the same output as in the example.
:here is the CheckForBalance example code, the rest of the code is just to verify that a bracket string is balanced:
bool CheckForBalance(LIST* list, char* str, int size)
{
char c;
bool retval = true;
for (int i = 0; i < size && retval;
i++)
{
switch (str[i])
{
case '(':
case '{':
case '[':
Push(list,
str[i]);
break;
case ')':
case '}':
case ']':
if
(IsEmpty(list))
{
retval = false;
}
else
{
c = Pop(list);
if (!IsMate(c, str[i]))
{
retval = false;
}
}
break;
default:
retval =
false;
break;
}
}
return retval;
}
# FOLLOWING IS THE MODIFIED function Pop AS ASKED IN QUESTION ,
(Please read comments mentioned in code to understand the code better.)
--------------------------------------------------------
bool Pop(LIST *list, char *c)
{
if (IsEmpty(list))
{
return false;
}
else
{
/* No info about LIST data
structure is given, hence it is assumed that list contains varaible
'data'. */
/* LIST data structure can
be implemented using linked list. In C, struct can be defined...
*/
/* ... for
implementation of singly linked list */
/* Char variable c pointed
by pointer c stores value of 'data' element to be poped present in
list.*/
*c = list->data;
list = list->next; /*
Parenthesis present in list is poped for balancing.
*/
return true;
}
}
---------------------------------------------------------------------------
# FOLLOWING IS THE function CheckForBalanceTO ACCOMODATE MODIFIED POP FUNCTION,
bool CheckForBalance(LIST* list, char* str, int size)
{
char c;
c = 'a'; /* Initialized
character variable c with dummy value to avoid any errors regarding
initialization*/
bool retval = true;
for (int i = 0; i < size && retval;
i++)
{
switch (str[i])
{
case '(':
case '{':
case '[':
Push(list,
str[i]);
break;
case ')':
case '}':
case ']':
if
(IsEmpty(list))
{
retval = false;
}
else
{
/*Returns boolean values true or false which is stored in
retval variable */
/*address of char c variable is passed using call
by reference */
retval=Pop(list,
&c);
/* After execution of above line, char variable c now has
the poped element stored.*/
if (!IsMate(c, str[i]))
{
retval = false;
}
}
break;
default:
retval =
false;
break;
}
}
return retval;
}
--------------------------------------------------------
Hence all the required modifications have been answered as asked in the question with coments :)
In C programming, Modify the function Pop in the example so that it has the signature...
Help with c++ program. The following code takes an infix expression and converts it to postfix. When I compile the code it returns "Segmentation fault". However I don't know what causes this. #include #include #include using namespace std; template class Stack { public: Stack();//creates the stack bool isempty(); // returns true if the stack is empty T gettop();//returns the front of the list void push(T entry);//add entry to the top of the stack void pop();//remove the top of the stack...
QUESTION: ADT stack: resizable array-based implementation for Ch4 programming problem 4 "maintain the stacks's top entry at the end of the array" at array index N-1 where the array is currently allocated to hold up to N entries. MAKE SURE YOU IMPLEMENT the functions: bool isEmpty() const; bool push(const ItemType& newEntry); bool pop(); in ArrayStackP4.cpp //FILE StackInterface.h #ifndef STACK_INTERFACE_ #define STACK_INTERFACE_ template<class ItemType> class StackInterface { public: /** Sees whether this stack is empty. @return True if the...
template <class T> class Stack { public: /** clear * Method to clear out or empty any items on stack, * put stack back to empty state. * Postcondition: Stack is empty. */ virtual void clear() = 0; /** isEmpty * Function to determine whether the stack is empty. Needed * because it is undefined to pop from empty stack. This * function will not change the state of the stack (const). * * @returns bool true if stack is...
I have a queue and stack class program that deals with a palindrome, I need someone to help to put in templates then rerun the code. I'd greatly appreciate it. It's in C++. Here is my program: #include<iostream> #include<list> #include<iterator> #include<string> using namespace std; class Queue { public: list <char> queue; Queue() { list <char> queue; } void Push(char item) { queue.push_back(item); } char pop() { char first = queue.front(); queue.pop_front(); return first; } bool is_empty() { if(queue.empty()) { return...
Convert following code to implement linked list C++ language #include<iostream> using namespace std; int top = -1; //globally defining the value of top, as the stack is empty void push(int stack[], int x, int n) { if (top == -1) //if top position is the last of posiition of stack,means stack is full { cout << "Stack is full Overflow condition"; } else { top = top + 1; //incrementing top position stack[top] = x; //inserting element on incremented position...
This is for C Programming Write a function that checks whether a given string is a palindrome or not, recursively. Palindrome is a word, phrase, or sequence that reads the same backward as forward. You may not modify the functions signatures, that is, adding any new parameters or changing the data type of the parameters in any way. You may not use square brackets and any type of loops in your solutions. For example: isPalindrome("abcba", ...) -> true isPalindrome("abcb1", ...)...
I need to implement raw array Stack for create empty stack, isEmpty, isFull, push, pop, and size using below pseudo code. I need two classes with stack pseudo code implementation and the main method to demonstrate the correct working of each operation. pseudo code StackADT (using raw array) class StackADT { int top int items[] int max StackADT(int n) Initialize array to n capacity top = 0 max = n boolean isEmpty() if array has no elements return true else...
C++ Please ensure code is fully working, will rate Question to be answered: Repeat Exercise 1 for the class linkedStackType question one: Two stacks of the same type are the same if they have the same number of elements and their elements at the corresponding positions are the same. Overload the relational operator == for the class stackType that returns true if two stacks of the same type are the same, false otherwise. Also, write the definition...
in c++ please include all of the following " template class, template function, singly linked list, the ADT stack, copy constructor, operator overloading, "try catch"and pointers Modify the code named "Stack using a Singly Linked List" to make the ADT Stack that is a template class has the code of the destructor in which each node is directly deleted without using any member function. As each node is deleted, the destructor displays the address of the node that is being...
PART A - STACKS To complete this assignment: Please study the code posted below. Please rewrite the code implementing a template class using a linked list instead of an array. Note: The functionality should remain the same *************************************************************************************************************** /** * Stack implementation using array in C/procedural language. */ #include <iostream> #include <cstdio> #include <cstdlib> //#include <climits> // For INT_MIN #define SIZE 100 using namespace std; /// Create a stack with capacity of 100 elements int stack[SIZE]; /// Initially stack is...