All the values show up.just in reverse order. I also have an overloaded assignment operator and a destructor (though to be honest the assignment operator is the same exact code, I've just included a test for self assignment). These are my Stack class's data members for reference private:Īnd this is my copy constructor as is template This is the structure so you have an idea of what I've named things template From what I've seen with other questions, I understand that this isn't always the best considering standard library implementations already existing in C++ as mentioned in this first answer BUT this is for an assignment so it's all in the name of science. Mine is a template class and I've also created a Node structure. * Returns the next element in the iteration.So I've combed through what is already on this site about linked list stacks. Throw new ConcurrentModificationException() Public boolean hasNext() throws ConcurrentModificationException * ConcurrentModificationException if the collection has changed * true if this iterator has at least one more element to deliver * Returns true if this iterator has at least one more element * size the integer size of the collection * collection the collection the iterator will move over * Sets up this iterator using the specified items. Private LinearNode current // the current position Private int iteratorModCount // the number of elements in the collection Private class LinkedListIterator implements Iterator * LinkedIterator represents an iterator for a linked list of linear nodes. * an iterator over the elements of the list * Returns an iterator for the elements in this list. Result = result + current.getElement() + "\n" * Returns a string representation of this list. * Returns the number of elements in this list. * true if the list is empty, false otherwise * Returns true if this list is empty and false otherwise. * Changes the element at a specific index. If (size() = 1) // only one element in the listĮlse if (current.equals(head)) // target is at the headĮlse if (current.equals(tail)) // target is at the tail Throw new ElementNotFoundException("LinkedList") If (targetElement.equals(current.getElement())) * Returns an element from a specific index, without removing it. Assign the new element to precede the old element. Assign the old element to follow the new element. Assign prev to precede the new element. Create a new node for the new element. * Adds a new element at a specific index, bumping the current element atįor(int i = 1 i prev = desiredIndex.getPrevious() Assigns the old tail to precede the new node. Assigns the new node to follow the old tail. Assigns the new node to be the head and the tail. LinearNode newElement = new LinearNode(element) Creates a new node for the new element. * Adds a new element to the end of the list. Public class LinkedList implements ListADT, Iterable * LinkedList represents a linked implementation of a list. I can't myself see anything wrong with my pop/dequeue implementation, but perhaps you fine folks can help me out. toString() on the list, even RIGHT before the pop/dequeue command, I can see that they're not. Whenever I run them however, I get an error letting me know that the list is empty when I try and pop/dequeue anything. I've been trying to program a LinkedList implementation of a Stack and of a Queue.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |