There's a few stylistic or design things that I noticed, but please pay special attention to the first two (especially the first item). The first two are not style or design related but rather actual bugs that are very likely to happen (well, the second one not so much).
NOTE: This item is a major bug. It's a "this code could never actually be used" level bug.
Why are you placement-new'ing on top of the default-constructed element of ? This is unnecessary unless you're trying to get around some rather odd aversion to a copy constructor or assignment operator.
If you're going to do it this way (and I highly suggest that you don't), you need to manually destruct the element () before you placement new over it.
To illustrate the problem, imagine that your're containing a type that sets up some kind of resources in its constructor. As the destructor never gets called for the initial member of (the one that gets created when node is created), those resources will leak.
I would just pass the object to node through a constructor and let the compiler work its magic before trying to unnecessarily placement-new things:
When you're dealing with memory details, you must be very conscious of exceptions.
In particular, if your statements throw exceptions after your statements, the length is then incorrect. Luckily the solution is simple: just allocate the memory before incrementing .
Your indenting is inconsistent (4 spaces sometimes, and 2 spaces others)
The void returns in removeFront and removeBack that do not affect control flow are a bit odd.
and could (and probably should) return a reference instead of a copy (as all of the standard library containers do). Note that you would then want two versions: a const one that returns a const reference, and a non-const one that returns a mutable reference.
You've implemented more of a deque than a list. How do you iterate over the list?
I suggest going the style of the standard library and making iterators.
As this is just an academic exercise (if not, use std::list), there's no need to put in all of the effort, but if you wanted, you could even do standard style iterators: this and this should prove helpful if you go this route.
This will allow you to iterate over the list, and, provided you write a method, your insertFront and insertBack methods become trivial ( for example).
Your printing methods don't particularly belong in the class.
What if instead of printing a message when the list is empty, I want to do nothing? What if instead of a space separator, I want a newline? There's so many decisions that you've hard coded that it makes the method useless to a consumer of the class. Let consumers of the class handle output formatting.
(Fun bonus with iterators: just into a and you have your print methods already written.)
createEmpty() shouldn't exist. That should be part of initialization lists.
Copy Constructor and Assignment Operator
I have a circular dependency of a Stack, then implement a queue in terms of a stack, then a list in terms of a queue.
here is the error I am getting.
I tried dynamic binding but could not get it
|But I was taught that makes sure you dont copy itself|
Yes. You must prevent the copying if and are the same.
is a pointer to the current object (this means it holds the current object's memory address).
You check it against the object's memory address.
You do not check it against the memory address of , which is a member variable of .
Also, be warned that in the C++ library there exists a container named .
You should probably not use the name "queue" in your code, especially if you are .
here I am attempting to print the results, but not deleting the queue.But it seems like list.queue(num); does not work.I am confused now guys.What am I really doing wrong
Then you have memory leaks because you don't deallocate the memory by 'ing the pointers.
Another thing: the output operation should not modify the object which is output.
In this case, the code should work even if we pass by reference.
(Also just as in the case of vs , there's vs , you've been warned. Twice.)
Why did you not implement a copy constructor (and copy assignment operator) for your queues?
Then you would not have to modify the parameter, you could simply do:
Edit: you are overusing dynamic memory allocation in your class template as well.
Why is a pointer?
It seems to be working now.The reason I dont use the assignment operator is because for some reason the List one is not working well.
I am confused as to how can that occur.The stack and the queue one are working fine, but not the List one.
The Stack and the queue is fine, but the List is not.How is that possible, because it uses the other two.
Does it not seem a little perverse that Stack uses a linked list for it's implementation, and you're ultimately using a Stack for a linked list implementation? It would make a lot more sense for the List to stand alone, and to implement Queue in terms of the List, and Stack in terms of the Queue. You usually want to start with the general and proceed to the specific (or more restricted) which is the opposite of what you're doing here.