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 140
|
struct Node
{
int data;
Node *next;
Node *prev;
};
#include <iostream>
#include <vector>
void fillList( const std::vector<int> &initialVec, Node *listToBuild );
void insertAtEnd( int value, Node *tail );
void printLots( Node *positionListBegin, Node *printListBegin );
void deleteAll( Node *firstNode );
int main()
{
const std::vector<int> vecP = {1, 3, 4, 6, 7, 11};
const std::vector<int> vecL = {0, 7, 4, 3, 2, 1, 9, 8, 10, 40, 32, 31};
Node *headP = new Node;
Node *tailP = new Node;
headP->next = tailP;
headP->prev = nullptr;
tailP->prev = headP;
tailP->next = nullptr;
Node *headL = new Node;
Node *tailL = new Node;
headL->next = tailL;
headP->prev = nullptr;
tailL->prev = headL;
tailP->next = nullptr;
fillList( vecP, tailP );
fillList( vecL, tailL );
printLots( headP, headL );
deleteAll( headP );
deleteAll( headL );
return 0;
}
void fillList( const std::vector<int> &initialVec, Node *listToBuild )
{
for( int i = 0; i < initialVec.size(); ++i )
{
insertAtEnd( initialVec[i], listToBuild );
}
}
void insertAtEnd( int value, Node *tail )
{
Node *toInsert = new Node;
toInsert->data = value;
tail->prev->next = toInsert;
toInsert->prev = tail->prev;
toInsert->next = tail;
tail->prev = toInsert;
}
void printLots( Node *positionListBegin, Node *printListBegin )
{
//points to "x"
Node *positionNode = new Node;
//positionNode now points to positionListBegin->next and NOT "x"; memory leak
positionNode = positionListBegin->next;
//points to "y"
Node *printNode = new Node;
//printNode now points to printListBegin->next and NOT "y"; memory leak
printNode = printListBegin->next;
int dataIndexValue = 0;
bool hasMoreToPrint = false;
while( positionNode->next != nullptr && printNode->next != nullptr )
{
hasMoreToPrint = false;
if( positionNode->data == dataIndexValue )
{
std::cout << printNode->data << " ";
/*positionNode now points to the next node in the chain;
the previous node positionNode pointed to still exists.
So there is no memory leak here*/
positionNode = positionNode->next;
hasMoreToPrint = true;
}
else
{
printNode = printNode->next;
++dataIndexValue;
}
}
if( hasMoreToPrint == false )
{
std::cout << "\n";
std::cout << "No matching index value "
<< positionNode->data << " found in print list. Terminating printLots function" << std::endl;
}
//going to delete whatever element they are currently pointing to
delete positionNode;
delete printNode;
std::cout << "\n";
}
void deleteAll( Node *firstNode )
{
//memory leak here too
Node *moveNode = new Node;
moveNode = firstNode;
while( moveNode->next != nullptr )
{
moveNode = moveNode->next;
//this should be fine to delete entire list
delete moveNode->prev;
}
//and then the final node in the list
delete moveNode;
}
|