Hello! I need help with the program below. I'm required to add the contents of
second list to the end of contents of the first list in the void concat
section.
Lastly, I'm required to return true if list1 has the same contents as list2 or
return false otherwise in the bool listCompare section.
Here's the program:
#include <iostream>
#include "intSLLst.h"
using namespace std;
void concat(IntSLList *list1, IntSLList *list2);
bool compare(IntSLList *list1, IntSLList *list2);
int main(int argc, char *argv)
{
// Declare list variables
IntSLList list1;
IntSLList list2;
IntSLList list3;
// Add some data to list 1
list1.addToTail(10);
list1.addToTail(20);
list1.addToTail(30);
list1.addToTail(40);
// Add some data to list 2
list2.addToTail(100);
list2.addToTail(200);
list2.addToTail(300);
list2.addToTail(400);
cout << "List 1: " << endl << " ";
list1.printAll();
cout << "List 2: " << endl << " ";
list2.printAll();
// Concatenate list1 to the end of list2
concat(&list2, &list1);
cout << "List 2 after concat: " << endl << " ";
list2.printAll();
// Concatenate list1 to the end of list3
concat(&list3, &list1);
cout << "List 3: " << endl << " ";
list3.printAll();
cout << "List 3 equals List 1: " << compare(&list3, &list1) << endl;
cout << "List 1 equals List 2: " << compare(&list1, &list2) << endl;
cout << "List 2 equals List 3: " << compare(&list2, &list3) << endl;
return 0;
}
void concat(IntSLList *list1, IntSLList *list2)
{
// HERE IS WHERE I NEED ASSISTANCE
// TO-DO: add the contents of second list to the end of
// contents of the first list
//
// Hint: this function should use a loop similar to the
// printAll() method. In the loop, call the addToTail()
// method from first list using the data from the second
// list.
//
// Remember that pointers are accessed using arrow notation:
// e.g., list1->getHead()
}
bool compare(IntSLList *list1, IntSLList *list2)
{
// HERE IS WHERE I NEED ASSISTANCE ALSO
// TO-DO: Return true if list1 has the same contents as list2
// Return false otherwise.
//
// Remember that pointers are accessed using arrow notation:
// e.g., list1->next and list1->info
// Remember that pointers are accessed using arrow notation:
// e.g., list1->next and list1->info
}
The output from the program should be as follows:
List 1:
10 20 30 40
List 2:
100 200 300 400
List 2 after concat:
100 200 300 400 10 20 30 40
List 3:
10 20 30 40
List 3 equals List 1: 1
List 1 equals List 2: 0
List 2 equals List 3: 0
Here is what I have started so far as a baseline:
//**************** intSLLst.h *****************
// singly-linked list class to store integers
#ifndef INT_LINKED_LIST
#define INT_LINKED_LIST
class IntSLLNode {
public:
int info;
IntSLLNode *next;
IntSLLNode(int el, IntSLLNode *ptr = 0) {
info = el; next = ptr;
}
};
class IntSLList {
public:
IntSLList() {
head = tail = 0;
}
~IntSLList();
int isEmpty() {
return head == 0;
}
void addtoHead(int);
void addtoTail(int);
int deletefromHead(); // delete the head and return its info;
int deletefromTail(); // delete the head and return its info;
void deleteNode(int);
bool isinList(int) const;
private:
IntSLLNode *head, *tail;
};
#endif