Question

1) Add insert and erase methods to the Vector class. The method headers of insert and...

1) Add insert and erase methods to the Vector class. The method headers of insert and erase are specified as follows:

iterator insert(iterator pos, const Object& x);

//insert the object x at position pos in a Vector object.

//The method returns a reference to the element inserted at position pos

// in the Vecor object.

iterator erase(iterator pos);

//erase removes the object at the position pos in a Vector object.

//The method returns the the position of the element that followed pos

//prior to the call.

2. (6 points) Write a C++ program that converts an infix expression, which includes (, ), +, -, *, and / operations to postfix notation. The program should allow the user to enter an infix expression using lower case characters, then it will display the result of conversion on the screen. (Note: Use the STL stack class to accomplish the solution.)

3. (6 points) The intersection of two lists L1 and L2, L1 ∩ L2, is defined as the list containing elements in both L1 and L2 only. Given two sorted lists L1 and L2, write a function, called intersection, to compute L1 ∩ L2 using only the basic list operations. The intersection function is defined as follows

template <typename Object>

list<Object> intersection( const list<Object> & L1, const list<Object> & L2);

Homework Answers

Answer #1

2.

#include<iostream>
#include<stack>
#include<string>

using namespace std;


string InfixToPostfix(string expression);


int HigherPrecedence(char operator1, char operator2);


bool Operator(char C);


bool Operand(char C);

int main()
{
   string expression;
   cout<<"Enter Infix Expression \n";
   getline(cin,expression);
   string postfix = InfixToPostfix(expression);
   cout<<"Output = "<<postfix<<"\n";
}


string InfixToPostfix(string expression)
{

   stack<char> S;
   string postfix = "";
   for(int i = 0;i< expression.length();i++) {


       if(expression[i] == ' ' || expression[i] == ',') continue;


       else if(Operator(expression[i]))
       {
           while(!S.empty() && S.top() != '(' && HigherPrecedence(S.top(),expression[i]))
           {
               postfix+= S.top();
               S.pop();
           }
           S.push(expression[i]);
       }

       else if(Operand(expression[i]))
       {
           postfix +=expression[i];
       }

       else if (expression[i] == '(')
       {
           S.push(expression[i]);
       }

       else if(expression[i] == ')')
       {
           while(!S.empty() && S.top() != '(') {
               postfix += S.top();
               S.pop();
           }
           S.pop();
       }
   }

   while(!S.empty()) {
       postfix += S.top();
       S.pop();
   }

   return postfix;
}


bool Operand(char C)
{
   if(C >= '0' && C <= '9') return true;
   if(C >= 'a' && C <= 'z') return true;
   if(C >= 'A' && C <= 'Z') return true;
   return false;
}

bool Operator(char C)
{
   if(C == '+' || C == '-' || C == '*' || C == '/' || C== '$')
       return true;

   return false;
}


int RightAssociative(char op)
{
   if(op == '$') return true;
   return false;
}


int GetOperatorWeight(char op)
{
   int weight = -1;
   switch(op)
   {
   case '+':
   case '-':
       weight = 1;
   case '*':
   case '/':
       weight = 2;
   case '$':
       weight = 3;
       break;
   }
   return weight;
}


int HigherPrecedence(char op1, char op2)
{
   int op1Weight = GetOperatorWeight(op1);
   int op2Weight = GetOperatorWeight(op2);


   if(op1Weight == op2Weight)
   {
       if(RightAssociative(op1)) return false;
       else return true;
   }
   return op1Weight > op2Weight ? true: false;
}

1.

#include <iostream>
#include <vector>

int main ()
{
std::vector<int> myvector (3,10);
std::vector<int>::iterator it;

it = myvector.begin();
it = myvector.insert ( it , 20 );

myvector.insert (it,2,30);


it = myvector.begin();

std::vector<int> anothervector (2,40);
myvector.insert (it+2,anothervector.begin(),anothervector.end());

int myarray [] = { 205,298,350};
myvector.insert (myvector.begin(), myarray, myarray+3);

std::cout << "myvector contains:";
for (it=myvector.begin(); it<myvector.end(); it++)
std::cout << ' ' << *it;
std::cout << '\n';

return 0;
}

Know the answer?
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
Similar Questions
The output only produces the values from the first linkedlist and not the second. Please fix...
The output only produces the values from the first linkedlist and not the second. Please fix the code to produce the desired objective. Thanks for your help! Objective: Interleave Example: Define the calling list as a set of ordered nodes, $L1 = {4, 2, 8 ,5, 8}$, and define the list that is passed as a parameter as a set of ordered nodes, $L2 = {5, 1, 8, 4, 5, 9}$. L1.interleave(L2) yields the set ${4, 5, 2, 1, 8,...
c++ just one file for all of it. You are to implement a 'list' class to...
c++ just one file for all of it. You are to implement a 'list' class to handle a list with general operations. That means you can insert and delete any element anywhere in the list. your list will be an array. the list has no order, except for the order you insert or delete. The methods you are to implement are as given: constructor methods (two, default and copy constructor, a list to a newly defined list, ie 'list listA(listB)')...
based on the code below, answer the questions Question 1: The LinkedList class uses another class...
based on the code below, answer the questions Question 1: The LinkedList class uses another class called Node (which is defined inside LinkedList.java). What are the fields in the Node class? Question 2: The Node class uses generics. Why? Question 3: The linkFirst method contains the following lines of code: if (f == null) last = newNode; else f.prev = newNode; What is the value of the linked list’s size attribute at this point in the code if f ==...
You can complete this assignment individually or as a group of two people. In this assignment...
You can complete this assignment individually or as a group of two people. In this assignment you will create a ​​Sorted Singly-Linked List​ that performs basic list operations using C++. This linked list should not allow duplicate elements. Elements of the list should be of type ‘ItemType’. ‘ItemType’ class should have a private integer variable with the name ‘value’. Elements in the linked list should be sorted in the ascending order according to this ‘value’ variable. You should create a...
Task 1: You will modify the add method in the LinkedBag class.Add a second parameter to...
Task 1: You will modify the add method in the LinkedBag class.Add a second parameter to the method header that will be a boolean variable: public boolean add(T newEntry, boolean sorted) The modification to the add method will makeit possible toadd new entriesto the beginning of the list, as it does now, but also to add new entries in sorted order. The sorted parameter if set to false will result in the existing functionality being executed (it will add the...
Create a C++ project. Download and add the attached .h and .cpp to the project. Write...
Create a C++ project. Download and add the attached .h and .cpp to the project. Write an implementation file to implement the namespace declared in the attached CSCI361Proj5.h. Name the implementation file as YourNameProj5.cpp and add it to the project. Run the project to see your grade. .h file: // Provided by: ____________(your name here)__________ // Email Address: ____________(your email address here)________ // FILE: link.h // PROVIDES: A toolkit of 14 functions for manipulating linked lists. Each // node of...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT