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();

 
}

Recommended Answers

All 8 Replies

169 C:\Users\...\sequence.cpp 'class Sequence' has no member named 'conacatenate'

There may be other things wrong but did you read this error message carefully? You have a typo in there somewhere. Sequence s3 = s1.conacatenate(s2); Right there.

There may be other things wrong but did you read this error message carefully? You have a typo in there somewhere.

Oops! That was embarrassing...Thanks!

But now I am getting the following error(s)

203 C:\Users\...\sequence.cpp no matching function for call to `Sequence::Sequence(Sequence)'

note C:\Users\...\sequence.cpp:25 candidates are: Sequence::Sequence(Sequence&)

note C:\Users\Samujjal\...\sequence.cpp:25 Sequence::Sequence(ElementType)


Truly appreciate the help.

I think your concatenate method should return a reference to a sequence object so that once you've concatenated s3 or s2 onto s1 then your copy constructor can work on the reference to the new sequence. As it is without the reference it's trying to use Sequence::Sequence(Sequence) but there is no such copy constructor.

concatenate() is kind of flimsy as it is. You declare a sequence (local to the method, so it will go poof at the end) seq3, but then you declare the head and the tail and you do the manipulations with those but then at the end you return seq3 after nothing (that I could see anyway) is happening to it.

Thanks for your suggestions. So do you think I should approach the concatenate method in a different way? Could you give me pointer (no pun intended ;)) please? I have been working on it all night and seems like have reached a roadblock. I would truly appreciate the help.

Use *this to access the calling object. Simply attach head of the passed in sequence to the tail of *this (and connect tail of the passed in sequence to the head -- I think that's what you did in the other methods). Then at the end of the method just return *this (and make sure you change the return type to (Sequence &) in the definition and declaration.

Your copy constructor is crashing also. I've had a little bit of luck with the concat method. Are you sure that you want your tail node pointing to your head node? I think it might be easier if it pointed to null but that's just me.

Your process makes a lot of sense. But the problem is that the function has to be non-destructive. So I can't modify wither seq1 or seq2. So the best way to implement seems to be to copy seq1 into seq3 and append seq2 to seq3.

That would make sense. I'm still messed up with the circular list hehe.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.