Question

In C programming, Modify the function Pop in the example so that it has the signature...

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;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

# 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 :)

Add a comment
Know the answer?
Add Answer to:
In C programming, Modify the function Pop in the example so that it has the signature...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Help with c++ program. The following code takes an infix expression and converts it to postfix....

    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...

    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() =...

    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...

    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...

  • I need to implement raw array Stack for create empty stack, isEmpty, isFull, push, pop, and...

    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...

    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,...

    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...

  • I need help fixing my code.   My output should be the following. Hello, world! : false...

    I need help fixing my code.   My output should be the following. Hello, world! : false A dog, a panic in a pagoda : true A dog, a plan, a canal, pagoda : true Aman, a plan, a canal--Panama! : true civic : true If I had a hi-fi : true Do geese see God? : true Madam, I’m Adam. : true Madam, in Eden, I’m Adam. : true Neil, a trap! Sid is part alien! : true Never odd...

  • I need to modify my C++ code so it can get the min value of the...

    I need to modify my C++ code so it can get the min value of the stack code is as follows: #include <iostream> using namespace std; #define MAX_SIZE 100 class Stack { private: int S[MAX_SIZE]; int top; public: Stack() {   top = -1; } void push(int x) {   if (top == MAX_SIZE - 1) {    cout << "Stack is Full." << endl;    return;   }   S[++top] = x; } void pop() {   if (top == -1) {    cout << "Stack is...

  • Finish function to complete code. #include <stdio.h> #include <stdlib.h> #include<string.h> #define Max_Size 20 void push(char S[],...

    Finish function to complete code. #include <stdio.h> #include <stdlib.h> #include<string.h> #define Max_Size 20 void push(char S[], int *p_top, char value); char pop(char S[], int *p_top); void printCurrentStack(char S[], int *p_top); int validation(char infix[], char S[], int *p_top); char *infix2postfix(char infix[], char postfix[], char S[], int *p_top); int precedence(char symbol); int main() { // int choice; int top1=0; //top for S1 stack int top2=0; //top for S2 stack int *p_top1=&top1; int *p_top2=&top2; char infix[]="(2+3)*(4-3)"; //Stores infix string int n=strlen(infix); //length of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT