Linked list Recursively
Apr 3, 2013 at 9:38pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
#include "LinkedList.h" // Header file
#include <cassert>
template <class ItemType>
LinkedList<ItemType>::LinkedList() : headPtr(NULL), itemCount(0)
{
} // end default constructor
template <class ItemType>
LinkedList<ItemType>::~LinkedList()
{
clear();
} // end destructor
template <class ItemType>
bool LinkedList<ItemType>::isEmpty() const
{
return itemCount == 0;
} // end isEmpty
template <class ItemType>
int LinkedList<ItemType>::getLength() const
{
return itemCount;
} // end getLength
template <class ItemType>
bool LinkedList<ItemType>::insert(int newPosition, const ItemType& newEntry)
{
bool ableToInsert = (newPosition >= 1) && (newPosition <= itemCount + 1);
if (ableToInsert)
{
// Create a new node containing the new entry
Node<ItemType>* newNodePtr = new Node<ItemType>(newEntry);
headPtr = insertNode(newPosition, newNodePtr, headPtr);
}// end if
return ableToInsert;
} // end insert
template <class ItemType>
bool LinkedList<ItemType>::remove(int position)
{
bool ableToRemove = (position >= 1) && (position <= itemCount);
if (ableToRemove)
{
Node<ItemType>* curPtr = NULL;
if (position == 1)
{
// Remove the first node in the chain
curPtr = headPtr; // Save pointer to node
headPtr = headPtr->getNext();
}
else
{
// Find node that is before the one to delete
Node<ItemType>* prevPtr = getNodeAt(position - 1);
// Point to node to delete
curPtr = prevPtr->getNext();
// Disconnect indicated node from chain by connecting the
// prior node with the one after
prevPtr->setNext(curPtr->getNext());
} // end if
// Return node to system
curPtr->setNext(NULL);
delete curPtr;
curPtr = NULL;
itemCount--; // Decrease count of entries
} // end if
return ableToRemove;
} // end remove
template <class ItemType>
void LinkedList<ItemType>::clear()
{
while (!isEmpty())
remove(1);
} // end clear
template <class ItemType>
ItemType LinkedList<ItemType>::getEntry(int position) const throw (PrecondViolatedExcep)
{
// Enforce precondition
bool ableToGet = (position >= 1) && (position <= itemCount);
if (ableToGet)
{
Node<ItemType>* nodePtr = getNodeAt(position);
return nodePtr->getItem();
}
else
{
string message = "getEntry() called with an empty list or " ;
message = message + "invalid position." ;
throw (PrecondViolatedExcep(message));
} // end if
} // end getEntry
template <class ItemType>
Node<ItemType>* LinkedList<ItemType>::getNodeAt(int position) const
{
// Debugging check of precondition
assert( (position >= 1) && (position <= itemCount) );
// Count from the beginning of the chain
Node<ItemType>* curPtr = headPtr;
for (int skip = 1; skip < position; skip++)
curPtr = curPtr->getNext();
return curPtr;
} // end getNodeAt
// RECURSIVE
template <class ItemType>
Node<ItemType>* LinkedList<ItemType>::insertNode(int position, Node<ItemType>* newNodePtr,
Node<ItemType>* subChainPtr)
{
if (position == 1)
{
// Insert new node at beginning of subchain
newNodePtr->setNext(subChainPtr);
subChainPtr = newNodePtr;
itemCount++; // Increase count of entries
}
else
{
Node<ItemType>* afterPtr = insertNode(position - 1, newNodePtr, subChainPtr->getNext());
subChainPtr->setNext(afterPtr);
} // end if
return subChainPtr;
} // end insertNode
// End of implementation file.
Hi, i need to convert these functions into recursive function. Can anyone help me?
Last edited on Apr 3, 2013 at 9:39pm UTC
Apr 3, 2013 at 9:55pm UTC
Some of them are already recursive. Which ones specifically?
Last edited on Apr 3, 2013 at 9:55pm UTC
Apr 3, 2013 at 10:26pm UTC
Also most of the operations require just one action, so there is no need for recursion for those. Any of them that use a loop could somehow be made recursive though
Apr 3, 2013 at 10:27pm UTC
Not to mention that recursion is generally slower than a loop/stack method.
Apr 4, 2013 at 4:15am UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
[code]template <class ItemType>
Node<ItemType>* LinkedList<ItemType>::getNodeAt(int position) const
{
// Debugging check of precondition
assert( (position >= 1) && (position <= itemCount) );
// Count from the beginning of the chain
Node<ItemType>* curPtr = headPtr;
int skip = 1;
curPtr = getNodeRecursive(position, skip, curPtr);
return curPtr;
}
template <class ItemType>
Node<ItemType>* LinkedList<ItemType>:: getNodeRecursive(int position, int skip, Node<ItemType>* curPtr) const
{
if (skip < position;)
{
curPtr = curPtr->getNext();
getNodeRecursive(position, skip+1, curPtr);
}
return curPtr;
}
[/code]
I changed the getNodeAt function by adding a getNodeRecursive function. but this is not showing the desired output.
Apr 4, 2013 at 4:39am UTC
Can anyone help me with the Remove function. I did the recursive part for the GetNodeAt function. Now, it seems like my remove function is not working properly.
Apr 4, 2013 at 6:17am UTC
Where is the getNext() function?
Topic archived. No new replies allowed.