Stacks, Queues, Templates, & Vectors

My Lab assignment just corrupted and I hadn't finished debugging my errors. I last worked on it two days ago. I can't remember what I had after a week of debugging and tweaking. my assignment was due last night, but my professor gave me a extension for a few hours (that is almost up and I am nowhere! Its like my brain is fried for finals!) Any and all help .. please! here is the info....

code given:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Lab10a  Stacks and Queues

#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <queue>

using namespace std;

template <class 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 <class 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.*/
	
}


and documentation
[quote]Finish the code for Lab10a.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.

Output should match iVector1 same order, iVector2 Not same order, sVector1 same, and sVector2 not in same order.



and then the second part:
Trees - Part B (25 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 on page 1141-1142 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, that will use recursion to count the number of nodes in the entire tree:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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()<<endl; 
 
   return 0; 
}


Code from pages 1140-1142:

InBinaryTree.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Specification file for the IntBinaryTree class
#ifndef INTBINARYTREE_H
#define INTBINARYTREE_H

class IntBinaryTree
{
private:
   struct TreeNode
   {
      int value;         // The value in the node
      TreeNode *left;    // Pointer to left child node
      TreeNode *right;   // Pointer to right child node
   };

   TreeNode *root;       // Pointer to the root node
   
   // Private member functions
   void insert(TreeNode *&, TreeNode *&);
   void destroySubTree(TreeNode *);
   void deleteNode(int, TreeNode *&);
   void makeDeletion(TreeNode *&);
   void displayInOrder(TreeNode *) const;
   void displayPreOrder(TreeNode *) const;
   void displayPostOrder(TreeNode *) const;
   
public:
   // Constructor
   IntBinaryTree()
      { root = NULL; }
      
   // Destructor
   ~IntBinaryTree()
      { destroySubTree(root); }
      
   // Binary tree operations
   void insertNode(int);
   bool searchNode(int);
   void remove(int);
   
   void displayInOrder() const
      {  displayInOrder(root); }
      
   void displayPreOrder() const
      {  displayPreOrder(root); }
      
   void displayPostOrder() const
      {  displayPostOrder(root); }
};
#endif 




Can Anyone help me!!![/quote]
Last edited on
Oh sweet college life rofl

You should probably prioritize the part of the assignment which will save it (i.e. gives enough points) (if that even applies).

Binary trees are not complicated. A newly inserted node will have its value compared with the root node and go to the right childnode when larger and to the left if smaller than the root. I assume TreeNode has a TreeNode* leftChild and TreeNode* rightChild of some sort.

1
2
3
4
5
6
7
int  IntBinaryTree::countNodes(TreeNode *nodePtr) 
{ 
 if (nodePtr == NULL) // A nothing with no children: zero nodes here
  return 0;
 else // A node with 0, 1 or 2 children and further "offspring"
  return 1 + countNodes(nodePtr->leftChild) + countNodes(nodePtr->rightChild);
} 


The former task would require me to do some STL research which I won't right now lol

Hope this helps. Once you get the knack of recursion it's nothing scary.
Last edited on
Can anyone assist me with the first part too? I don't even know where to start??? I have less than 30 mins before i have to submit and i am still just as lost on the first part of this lab!!
Last edited on
Okay so I have figured out that i need to use two variables for part A:

frontToBack //value moving from front to middle
backToFront // value moving from end to middle
but how to use the template with this information???
Topic archived. No new replies allowed.