Data Structures with C++ Using STL”, William Ford and William Topp, 2002, Prenti
ID: 3758876 • Letter: D
Question
Data Structures with C++ Using STL”, William Ford and William Topp, 2002, Prentice Hall, 0-13-085850-1
CH11
Question 14. page 639
Set A is a subset of set B provided that each element of A is also an element of B. Implement the set relation as a function subset() that take sets lhs and rhs as argument and returns true if lhs is a subset of rhs. In the review exercises, the implementation uses set operations. Your implementation should use iterations lhsIter to
scan the two ordered sets.
template <typename T>
bool subset (const set<T>& lhs, const set<T>& rhs);
Explanation / Answer
#include <iostream.h> // input output stream header file
#include <stdio.h> // standard input output header file
#include <stdlib.h> // standard library header file
#include <string.h> // header file with string function
#include <conio.h> // console input output header file
struct tag {
int data;
struct tag *prev; // pointers
struct tag *next;
} ;
typedef struct tag *setA, *setB;
char * funcSubset(int lhs[10], int rhs[10]) {
char res[20];
strcpy(res, "It is a subset");
int l,r, j, count = 0;
// if lhs is a subset of rhs return true;
for (int i = 0; i< 10; i++) {
for (j=0; j<10; j++) {
if (rhs[j] == lhs[i]) break;
if ( j == 9) strcpy(res,"Not a subset"); // not found so far hence not a subset
} // end for j
} // end for i
return res;
}
int main() // start main
{
clrscr();
setA sa, tempA, headA;
setB sb, tempB, headB;
// int lhs[10], rhs[10];
int lhs[10] = {1,4,2,5,7,9,24,25,56,99};
int rhs[10] = {1,5,7,9,24,25};
funcSubset(lhs,rhs);
sa = (struct tag *) malloc(sizeof(setA));
sb = (struct tag *) malloc(sizeof(setB));
sa->prev = NULL;
sa->next = NULL;
sb->prev = NULL;
sb->next = NULL;
sa->data = 5;
sb->data = 6;
headA=sa; headB=sb;
cout << sa->data << sb->data << endl;
for (int i = 1; i <= 10; i++) {
tempA = (struct tag *) malloc(sizeof(setA));
tempB = (struct tag *) malloc(sizeof(setB));
tempA->data = i*5;
tempB->data = i*7;
tempA->prev = sa;
tempB->prev = sb;
tempA->next = NULL;
tempB->next = NULL;
sa->next = tempA;
sb->next = tempB;
} // end for i loop
sa=headA; sb=headB;
for (i = 1; i <= 10; i++) {
cout << " sa = " << sa->data << " , sb = " << sb->data;
sa = sa->next; sb = sb->next;
} // end for
return 0; // exit
} // end of main()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.