This a lab for C++ using visual studio 2017
Lab 10
Total 50 points
Stacks and Queues - Part A (40 points)
Finish the code for Lab10aStacksAndQueues.cpp . You will write code
for the same function which will return
true if the values of the elements of the vector that is passed to
it are the same read both forwards and
backwards. If they are not the same in both directions, it will
return false. Don’t change anything in the
main function or add any additional functions to the program.
The provided .cpp code is below
// Lab10a Stacks and Queues
#include
#include
#include
#include
#include
using namespace std;
template
bool Same(vector tVector);
int main()
{
const int SIZE_INT = 8;
const int SIZE_STRING = 6;
int iTemp1[]= {1,1,2,3,3,2,1,1};
int iTemp2[]= {1,1,2,3,2,3,1,1};
vector iVector1(iTemp1, iTemp1 + SIZE_INT);
vector iVector2(iTemp2, iTemp2+ SIZE_INT);
string sTemp1[]= {"aa", "bb", "cc", "cc", "bb", "aa"};
string sTemp2[]= {"aa", "bb", "cc", "bb", "cc", "aa"};
vector sVector1(sTemp1, sTemp1 + SIZE_STRING);
vector sVector2(sTemp2, sTemp2 + SIZE_STRING);
if (Same(iVector1))
cout << "iVector1 values are in the same order forwards and backwards\n";
else
cout << "iVector1 values are NOT in the same order forwards and backwards\n";
if (Same(iVector2))
cout << "iVector2 values are in the same order forwards and backwards\n";
else
cout << "iVector2 values are NOT in the same order forwards and backwards\n";
if (Same(sVector1))
cout << "sVector1 values are in the same order forwards and backwards\n";
else
cout << "sVector1 values are NOT in the same order forwards and backwards\n";
if (Same(sVector2))
cout << "sVector2 values are in the same order forwards and backwards\n";
else
cout << "sVector2 values are NOT in the same order forwards and backwards\n";
system("pause");
return 0;
}
template
bool Same(vector tVector)
{
/* Write the code for this function. You must use one STL stack of
* type T and one STL queue of type T. You should not have more than 15 lines of
* code total.
*/
/* steps:
create a queue
create a stack
push into stack
push into queue
compare the stack and queue "output" when you pop it
*/
}
Trees - Part B (10 points)
Please complete the program below exactly as described. Don’t add
anything extra to it (methods, extra
variables, features, etc.) and don’t leave any features described
below out.
You are going to write a function that will use recursion to count
the number of nodes in a tree. First use the code in the
text book (pg. 1141-1142 for 7th Edition or pg. 1159 for 8th
Edition) to develop the IntBinaryTree class (For the
member functions, you only need the code for the constructor and
the insert member function). Your main program will
call a public member function called numNodes. This function will
then call a private member function, countNodes,
which will use recursion to count the number of nodes in the entire
tree:
int IntBinaryTree::countNodes(TreeNode *nodePtr)
{
if (nodePtr == NULL)
//write only one line of code here
else
//write only one line of code here
}
int main()
{
IntBinaryTree tree;
tree.insertNode(14);
tree.insertNode(10);
tree.insertNode(8);
tree.insertNode(6);
tree.insertNode(5);
tree.insertNode(7);
tree.insertNode(9);
tree.insertNode(12);
tree.insertNode(11);
tree.insertNode(13);
tree.insertNode(22);
tree.insertNode(30);
tree.insertNode(32);
tree.insertNode(24);
tree.insertNode(20);
tree.insertNode(17);
tree.insertNode(15);
tree.insertNode(18);
tree.insertNode(21);
cout <<"The number of nodes in the tree is: " <<
tree.numNodes()< system (“Pause”);
return 0;
}
Note: For Lab 10 you will turn in 2 .cpp files.
One should be named Lab10a and the other Lab10b.
Stack and Queues PART A Solution:
.// the code i am writing is a running code and giving correct output.so following is the bool same() functionality
bool flag=true; // valuse to be return
//create a queue
queue<T> myqueue;
//create a stack
stack<T> mystack;
//push into stack
for (auto &entry: TVector)
mystack.push(entry);
//push into queue
for (auto &entry: TVector)
myqueue.push(entry);
//compare the stack and queue "output" when you pop it
while (!myqueue.empty() && !mystack.empty())
{
T t1=mystack.top();
mystack.pop();
T t2=myqueue.front();
myqueue.pop();
if(t1!=t2)
{
flag=false;
}
}
return flag;
}
// It is exactly 15 line code,it is running and producing correct output.I can give the whole program as well if you //require.
PART B: TREES
int IntBinaryTree::countNodes(TreeNode *nodePtr)
{
if (nodePtr == NULL)
return 0;
//write only one line of code here
else
return (1+ countNodes(nodePtr->left) +
countNodes(nodePtr->right) );
//write only one line of code here
}
IDE output for PART A
Whole Program:-
#include <iostream>
#include <queue>
#include <stack>
using namespace std;
template < typename T>
bool Same(vector<T> TVector);
int main()
{
const int SIZE_INT = 8;
const int SIZE_STRING = 6;
int iTemp1[]= {1,1,2,3,3,2,1,1};
int iTemp2[]= {1,1,2,3,2,3,1,1};
vector<int> iVector1(iTemp1, iTemp1 + SIZE_INT);
vector<int> iVector2(iTemp2, iTemp2+ SIZE_INT);
string sTemp1[]= {"aa", "bb", "cc", "cc", "bb", "aa"};
string sTemp2[]= {"aa", "bb", "cc", "bb", "cc", "aa"};
vector<string> sVector1(sTemp1, sTemp1 + SIZE_STRING);
vector<string> sVector2(sTemp2, sTemp2 + SIZE_STRING);
if (Same(iVector1))
cout << "iVector1 values are in the same order forwards and
backwards\n";
else
cout << "iVector1 values are NOT in the same order forwards
and backwards\n";
if (Same(iVector2))
cout << "iVector2 values are in the same order forwards and
backwards\n";
else
cout << "iVector2 values are NOT in the same order forwards
and backwards\n";
if (Same(sVector1))
cout << "sVector1 values are in the same order forwards and
backwards\n";
else
cout << "sVector1 values are NOT in the same order forwards
and backwards\n";
if (Same(sVector2))
cout << "sVector2 values are in the same order forwards and
backwards\n";
else
cout << "sVector2 values are NOT in the same order forwards
and backwards\n";
system("pause");
return 0;
}
template <typename T>
bool Same(vector<T> TVector)
{
/* Write the code for this function. You must use one STL stack
of
* type T and one STL queue of type T. You should not have more than
15 lines of
* code total.
*/
//steps:
bool flag=true;
//create a queue
queue<T> myqueue;
//create a stack
stack<T> mystack;
//push into stack
for (auto &entry: TVector)
mystack.push(entry);
//push into queue
for (auto &entry: TVector)
myqueue.push(entry);
//compare the stack and queue "output" when you pop it
while (!myqueue.empty() && !mystack.empty())
{
T t1=mystack.top();
mystack.pop();
T t2=myqueue.front();
myqueue.pop();
if(t1!=t2)
{
flag=false;
}
}
return flag;
}
This a lab for C++ using visual studio 2017 Lab 10 Total 50 points Stacks and...
#include <iostream> using namespace std; struct ListNode { float value; ListNode *next; }; ListNode *head; class LinkedList { public: int insertNode(float num); void deleteNode(float num); void destroyList(); void displayList(); LinkedList(void) {head = NULL;} ~LinkedList(void) {destroyList();} }; int LinkedList::insertNode(float num) { struct ListNode *newNode, *nodePtr = head, *prevNodePtr = NULL; newNode = new ListNode; if(newNode == NULL) { cout << "Error allocating memory for new list member!\n"; return 1; } newNode->value = num; newNode->next = NULL; if(head==NULL) { cout << "List...
Hello, please correct the following C++ CODE it is not working on visual studio: (I WILL RATE, NO INCOMPLETE OR WRONG SOLUTIONS PLEASE) // CPP program to evaluate a given // expression where tokens are // separated by space. #include using namespace std; // Function to find precedence of // operators. int precedence(char op){ if(op == '+'||op == '-') return 1; if(op == '*'||op == '/') return 2; return 0; } // Function to perform arithmetic operations. int applyOp(int a,...
Hi there, I am working on a binary search tree code in c++. The program must store and update students' academic records, each node includes the student name, credits attempted, credits earned and GPA. I have made some progress with the code and written most of the functions in the .cpp file (already did the .h file) but i am struggling with what to put in the main and how to put an update part in the insert function. I...
Using C++ in Visual Studios Rewrite the code to use a Stack instead of a Queue. You will need to think about the differences in the Stack and Queue. Think about how they operate and how the nodes may differ. Modify the code as necessary. You will need to push to the Stack, pop from the Stack, peek at the Stack. You will need a member functions like in the example code. Your program will need to show that everything...
PLEASE CODE IN C++ AND MAKE IT COPYABLE! In this project, you will design and implement an algorithm to determine the next greater element of an element in an array in Θ(n) time, where 'n' is the number of elements in the array. You could use the Stack ADT for this purpose. The next greater element (NGE) for an element at index i in an array A is the element that occurs at index j (i < j) such that...
In C++ Implement a queue data structure using two stacks. Remember a queue has enqueue and dequeue functions. You could use either the array or linked list implementation for stacks and queues. Source for stack array: --------------------------------------------------- #include<iostream> #define SIZE 100 #define NO_ELEMENT -999999 using namespace std; class Stack { int arr[SIZE]; // array to store Stack elements int top; public: Stack() { top = -1; } void push(int); // push an element into Stack int pop(); // pop the...
HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE 100 #define NO_ELEMENT -999999 using namespace std; class Stack { int arr[SIZE]; // array to store Stack elements int top; public: Stack() { top = -1; } void push(int); // push an element into Stack int pop(); // pop the top element from Stack int topElement(); // get the top element void display(); // display Stack elements from top to bottom }; void Stack...
A binary tree is a complete binary tree if all the internal nodes (including the root node) have exactly two child nodes and all the leaf nodes are at level 'h' corresponding to the height of the tree. Consider the code for the binary tree given to you for this question. Add code in the blank space provided for the member function checkCompleteBinaryTree( ) in the BinaryTree class. This member function should check whether the binary tree input by the...
in C++ creat a DynamicQueue class, add a new data member called count to trace the total number of node you have in current queue (you need to modify some member functions for adding count). Add a member function called displayQueue() to display values stored in each node in the current queue, also the total number of nodes in the queue. You also need to have a driver program (refer to Tester) to test your modified new class and new...
Fix the function so that it checks if the string is a palindrome #include <iostream> using namespace std; //Fix the function so that it checks if the string is a palindrome //(same forwards as backwards ex: racecar / radar) bool is_palindrome(string str, int i){ //base cases if (/* add the condition */) return false; if (/* add the condition */) return true; //recursive call return is_palindrome(str, i-1); } int main(){ string x; cin >> x; if (is_palindrome(x,x.length())){...