Hi I am trying to implement a sequence using a linked list. I need to have the following functionalities: insert, remove and concatenate. The concatenate should not be destructive and the function call should be like. That is Seq1 and Seq2 should be preserved post function call.
Seq3 = Seq1.concatenate(seq2)
I have implemented the insert and remove functions, the constructors and the copy constructors successfully. But am having trouble with the concatenate function. For starters I have the following error after compiling:
169 C:\Users\...\sequence.cpp 'class Sequence' has no member named 'conacatenate'
So I can't even check my code. Please help. I am pasting my entire code, the header and the cpp in two parts
sequence.h
#include <iostream>
using namespace std;
typedef int ElementType;
struct Node
{
ElementType data;
Node* next;
};
class Sequence
{
public:
Sequence();
Sequence(ElementType s);
~Sequence();
Sequence(Sequence &rhs);
Sequence concatenate(Sequence &seq);
void insert(ElementType s);
ElementType remove();
bool isEmpty();
void printSeq();
private:
Node* head;
};
sequence.cpp
#include "sequence.h"
Sequence::Sequence()
{
head = NULL;
}
Sequence::~Sequence()
{
Node* iterator = head;
Node* delnode;
if (head!=NULL){
do {
delnode = iterator;
iterator = iterator->next;
delete delnode;
} while (iterator != head);
}
head = NULL;
}
Sequence::Sequence(Sequence &rhs)
{
head = NULL;
Node*tail = head;
Node* iterRHS = rhs.head;
if(iterRHS != NULL)
{
head = new Node;
head->data = iterRHS->data;
head->next = NULL;
iterRHS = iterRHS->next;
tail = head->next;
while (iterRHS != NULL)
{
tail->next = new Node;
tail = tail -> next;
tail->data = iterRHS->data;
tail->next = NULL;
iterRHS = iterRHS->next;
}
tail->next = head;
}
}
Sequence::Sequence(ElementType s)
{
Node *temp = new Node;
temp->data = s;
head = temp;
head->next = head;
temp->next = head;
head = temp->next;
}
void Sequence::insert(ElementType s)
{
Node *temp = new Node;
temp->data = s;
temp->next = head;
head = temp;
}
ElementType Sequence::remove()
{
ElementType delnode;
Node* temp;
if (head != NULL)
{
temp = head;
head = head->next;
delete temp;
}
return delnode;
}
Sequence Sequence::concatenate(Sequence &seq2)
{
Sequence seq3;
Node* seq3head = new Node;
seq3head = NULL;
Node* seq3tail = seq3head;
Node* iterSeq1 = head;
if(iterSeq1 != NULL)
{
seq3head = new Node;
seq3head -> data = iterSeq1->data;
seq3head->next = NULL;
}
seq3tail = seq3tail->next;
iterSeq1 = iterSeq1->next;
while (iterSeq1 != NULL)
{
seq3tail -> next = new Node;
seq3tail = seq3tail->next;
seq3tail->data = iterSeq1->data;
seq3tail->next = NULL;
iterSeq1 = iterSeq1->next;
}
seq3tail->next = NULL;
Node* iterSeq2 = seq2.head;
if(iterSeq2 != NULL)
{
seq3tail->next = new Node;
seq3tail = seq3tail->next;
seq3tail->data = iterSeq2->data;
seq3tail->next = NULL;
iterSeq2 = iterSeq2->next;
}
seq3tail->next = NULL;
return seq3;
}
void Sequence::printSeq()
{
Node* iterator = head;
if (head != NULL) {
do
{
cout << iterator->data << " ";
iterator = iterator->next;
}while(iterator != NULL);
}
cout << endl;
}
int main()
{
cout << "Some simple normal tests" << endl;
Sequence s1;
//some inserts
s1.insert(4);
s1.insert(8);
s1.insert(15);
s1.insert(16);
s1.insert(23);
s1.insert(42);
//Should print out list
cout << "List printed is: ";
s1.printSeq();
cout << "It should be 42 23 16 15 8 4" <<endl;
//a couple of removes
s1.remove();
s1.remove();
s1.remove();
//Should print out list with three removed
cout << "List printed is: ";
s1.printSeq();
cout << "It should be 15 8 4" <<endl;
//This is the copy constructor
Sequence s2 = s1;
cout << "Should be same list again." <<endl;
//this should print same list
s1.printSeq();
cout << "Should be same list again." << endl;
s1 = s2;
s1.printSeq();
s1.insert(8);
s1.insert(15);
s1.insert(16);
cout << "These two should be different" << endl;
s1.printSeq();
s2.printSeq();
Sequence s3 = s1.conacatenate(s2);
cout << "This is the concatenated list" << endl;
s3.printSeq();
}