// 1. Write an iterative (i.e. while loop) function // to determine if two lists
ID: 3770649 • Letter: #
Question
// 1. Write an iterative (i.e. while loop) function
// to determine if two lists are equal
//
// 2. Also, follow the directions in main to set up and test lists e and f
// 3. Test if a is equal to a by calling your equal function
// Your function should immediately return a true value
// Two lists will be equal if the data elements in each corresponding
// cells are equal and both lists have the same number of cells
// When your run your program, you should get the output shown
// NOTE: If both lists are empty, they are considered to be equal
/* OUTPUT
a is
3 5 2
b is
3 5 2
c is
3 5
d is
3 10 2
a, b equal
a, c not equal
a, d not equal
PLUS ADDITIONAL OUTPUT GENERATED BY STUDENT FUNCTION CALLS
*/
#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
I have written the code for the equal function.
Please find the full code below along with comments:
#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.