Answer to Question #233669 in C++ for Ahmad

Question #233669
Task 1:

Complete the body of this function. You do not need to check the precondition. You
may use the stack template class
bool balanced(const char p[ ], size_t n)
// Precondition: p[0]...p[n-1] contains n characters, each of which
// is '(', ')', '{' or '}'.
// Postcondition: The function returns true if the characters form a
// sequence of correctly balanced parentheses with each '(' matching
// a ')' and each '{' matching a '}'. Note that a sequence such as
// ( { ) } is NOT balanced because when we draw lines to match the
// parentheses to their partners, the lines cross each other. On the
// other hand, ( { } ) and { ( ) } are both balanced.
Task2:
Add the function to Task 1,Write a function that will swap pairs of elements in a given singlylinked list. Note that you have to actually swap the elements, not just the values, and that you
should modify the list in place (i.e. you should not create a copy of the list). For instance, the list
1->2->3->4->5->6->… becomes 2->1->4->3->6->5->…
1
Expert's answer
2021-09-07T04:26:54-0400

Task 1



#include <bits/stdc++.h>
using namespace std;
 


bool Balanced(string expression)
{ 
    stack<char> sentence;
    char ch;
 
   
    for (int n= 0; n < expression.length(); n++)
    {
        if (expression[n] == '(' || expression[n] == '['
            || expression[n] == '{')
        {
            
            sentence.push(expression[n]);
            continue;
        }
 
  
        if (sentence.empty())
            return false;
 
        switch (expression[n]) {
        case ')':
             
           
            ch = sentence.top();
            sentence.pop();
            if (ch == '{' || ch == '[')
                return false;
            break;
 
        case '}':
 
            
            ch = sentence.top();
            sentence.pop();
            if (ch == '(' || ch == '[')
                return false;
            break;
 
        case ']':
 
            
            ch = sentence.top();
            sentence.pop();
            if (ch == '(' || ch == '{')
                return false;
            break;
        }
    }
 
    
    return (sentence.empty());
}
 
// Testing code
int main()
{
    string expression = "{()}[]";
 
    
    if (Balanced(expression))
        cout << "Balanced";
    else
        cout << "Not Balanced";
    return 0;
}

Task 2



#include <iostream>
using namespace std;
 


class List {
public:
    int data;
    List* nextNode;
};


void pairWiseSwap(List* node)
{
    List* tempNode = node;
 
   
    while (tempNode != NULL && tempNode->nextNode != NULL) {
        
        swap(tempNode->data,
             tempNode->nextNode->data);
 
        
        tempNode = tempNode->nextNode->nextNode;
    }
}
 


void insert(List** node, int data)
{
    
    List* newNode = new List();
 
   
    newNode->data = data;
 
    
    newNode->nextNode = (*node);
 
    
    (*node) = newNode;
}
 


void displayList(List* head)
{
    while (head != NULL) {
        cout << head->data << " ";
        head = head->nextNode;
    }
}


int main()
{
    List* startNode = NULL;
 
    
    insert(&startNode, 5);
    insert(&startNode, 4);
    insert(&startNode, 3);
    insert(&startNode, 2);
    insert(&startNode, 1);
 
    cout << "Before swapping the linked list\n";
    displayList(startNode);
 
    pairWiseSwap(startNode);
 
    cout << "\nAfter swapping the linked list \n";
    displayList(startNode);
 
    return 0;
}
 

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS