--- "Nawlins Nightmare" <paulnorri...@...> posted code which I fixed and
incremented it:
/**
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
*/
#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.
return (list1->getHead()==list2->getHead());
}
//**************** 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
{
private:
IntSLLNode *head, *tail;
public:
IntSLList()
{
head = tail = 0;
}
int isEmpty()
{
return head == 0;
}
IntSLLNode *getHead ()
{
return head;
}
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;
void printAll ()
{
IntSLLNode *p = head;
while (p)
{
std::cout << p->info << " ";
}
std::cout << std::endl;
}
};
#endif