// CS 250 question 3 - Fall, 2015 // 1. Write an iterative (i.e. while loop) fun
ID: 3770682 • Letter: #
Question
// CS 250 question 3 - Fall, 2015
// 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
// CS 250 question 3 - Fall, 2015
// 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 *************************
node * e, * f;
e = f = NULL;
if( equal(e,f) ) cout << "e, f equal ";
else cout << "e, f not equal ";
if( equal(a,e) ) cout << "a ,e equal ";
else cout << "a, e not equal ";
// compare a and a - Your equal function
// should immediately return a true answer
// STUDENT WRITES THE CODE BELOW *******************
if( equal(a,a) ) cout << "a, a equal ";
else cout << "a, a not equal ";
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
if(first==NULL&&second==NULL)
return true;
else if(first!=NULL&&second==NULL)
return false;
else if(first==NULL&&second!=NULL)
return false;
node * walker1 = first->p;
node * walker2 = second->p;
while ( walker1!= NULL && walker2!= NULL)
{
if(walker1->data!=walker2->data)
return false;
walker1 = walker1->p;
walker2 = walker2->p;
}
if( walker1 == NULL && walker2 == NULL)
return true;
else
return false;
}
int main()
{
q3();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.