#include <iostream> using std::cout; using std::cin; using std::endl; using std:
ID: 3770648 • Letter: #
Question
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
using std::ostream;
struct node {
int data;
node * p;
};
bool equal( const node * first, const node * second);
void addFront3( node * & start, int);
void print( ostream & , node *);
void cleanUp3( node *);
void q3()
{
cout << " QUESTION 3 STARTING**********" << endl;
node * a, * b, * c , *d;
a = b = c = d = NULL;
addFront3(a,2);
addFront3(a,5);
addFront3(a,3);
addFront3(b,2);
addFront3(b,5);
addFront3(b,3);
addFront3(c,5);
addFront3(c,3);
addFront3(d,2);
addFront3(d,10);
addFront3(d,3);
cout << "a is ";
print(cout,a);
cout << "b is ";
print(cout,b);
cout << "c is ";
print(cout,c);
cout << "d is ";
print(cout,d);
if( equal(a,b) ) cout << "a, b equal ";
else cout << "a, b not equal ";
if( equal(a,c) ) cout << "a, c equal ";
else cout << "a, c not equal ";
if( equal(a,d) ) cout << "a, d equal ";
else cout << "a, d not equal ";
// Setup two lists, e and f and make both of them empty
// compare e and f
// compare a and e
// *************** STUDENT WRITES THE CODE BELOW *************************
// compare a and a - Your equal function
// should immediately return a true answer
// STUDENT WRITES THE CODE BELOW *******************
cleanUp3(a);
cleanUp3(b);
cleanUp3(c);
cleanUp3(d);
}
void addFront3( node * & start, int x)
{
node * t = new node;
t->data = x;
if( start != NULL ) // NOT NECESSARY
t->p = start;
else
t->p = NULL;
start = t;
}
void print( ostream & w, node * s)
{
node * walker = s;
while ( walker != NULL)
{
w << walker->data << ' ';
walker= walker->p;
}
w << endl;
}
void cleanUp3( node * s)
{
node * walker, * prev;
walker = s;
while ( walker != NULL )
{
prev = walker;
walker = walker->p;
delete prev;
}
}
bool equal( const node * first, const node * second)
{
// STUDENT WRITES THE CODE BELOW
return false; // ONLY HERE TO GET THE PROGRAM TO COMPILE
}
Explanation / Answer
working c++ code compiled on ideone.
I have writtent the equal function for this code.
#include <iostream>
using namespace::std;
struct node {
int data;
node * p;
};
bool equal( const node * first, const node * second);
void addFront3( node * & start, int);
void print( ostream & , node *);
void cleanUp3( node *);
void q3()
{
cout << " QUESTION 3 STARTING**********" << endl;
node * a, * b, * c , *d;
a = b = c = d = NULL;
addFront3(a,2);
addFront3(a,5);
addFront3(a,3);
addFront3(b,2);
addFront3(b,5);
addFront3(b,3);
addFront3(c,5);
addFront3(c,3);
addFront3(d,2);
addFront3(d,10);
addFront3(d,3);
cout << "a is ";
print(cout,a);
cout << "b is ";
print(cout,b);
cout << "c is ";
print(cout,c);
cout << "d is ";
print(cout,d);
if( equal(a,b) ) cout << "a, b equal ";
else cout << "a, b not equal ";
if( equal(a,c) ) cout << "a, c equal ";
else cout << "a, c not equal ";
if( equal(a,d) ) cout << "a, d equal ";
else cout << "a, d not equal ";
// Setup two lists, e and f and make both of them empty
// compare e and f
// compare a and e
// *************** STUDENT WRITES THE CODE BELOW *************************
// compare a and a - Your equal function
// should immediately return a true answer
// STUDENT WRITES THE CODE BELOW *******************
cleanUp3(a);
cleanUp3(b);
cleanUp3(c);
cleanUp3(d);
}
void addFront3( node * & start, int x)
{
node * t = new node;
t->data = x;
if( start != NULL ) // NOT NECESSARY
t->p = start;
else
t->p = NULL;
start = t;
}
void print( ostream & w, node * s)
{
node * walker = s;
while ( walker != NULL)
{
w << walker->data << ' ';
walker= walker->p;
}
w << endl;
}
void cleanUp3( node * s)
{
node * walker, * prev;
walker = s;
while ( walker != NULL )
{
prev = walker;
walker = walker->p;
delete prev;
}
}
bool equal( const node * first, const node * second)
{
// STUDENT WRITES THE CODE BELOW
while (first != NULL && second != NULL)
{
if (first->data != second->data)
return false;
//if they are equal then we increment both the lists.
first = first->p;
second = second->p;
}
// this is to check if both are of same length.If they are then at this stage they both should point to NULL.
return (first == NULL && second == NULL);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.