#include
using std::cout;
using std::endl;
struct NODE {
NODE *pNext;
NODE *pPrev;
int nData;
};
NODE *pHead, *pTail;
// Appends a node to the end of the list
void AppendNode(NODE *pNode) {
if (pHead == NULL) {
pHead = pNode;
pNode->pPrev = NULL;
} else {
pTail->pNext = pNode;
pNode->pPrev = pTail;
}
pTail = pNode;
pNode->pNext = NULL;
}
// Inserts a node into the list after pAfter
void InsertNode(NODE *pNode, NODE *pAfter) {
pNode->pNext = pAfter->pNext;
pNode->pPrev = pAfter;
if (pAfter->pNext != NULL)
pAfter->pNext->pPrev = pNode;
else
pTail = pNode;
pAfter->pNext = pNode;
}
// Removes the specified node from the list
void RemoveNode(NODE *pNode) {
if (pNode->pPrev == NULL)
pHead = pNode->pNext;
else
pNode->pPrev->pNext = pNode->pNext;
if (pNode->pNext == NULL)
pTail = pNode->pPrev;
else
pNode->pNext->pPrev = pNode->pPrev;
}
// Deletes the entire list
void DeleteAllNodes() {
while (pHead != NULL)
RemoveNode(pHead);
}
int main() {
NODE *pNode;
const int MAX = 10;
// Add items to linked list
for (int i = 0; i < MAX; i++) {
pNode = new NODE;
pNode->nData = i;
AppendNode(pNode);
}
// Now display each item in list
for (pNode = pHead; pNode != NULL; pNode = pNode->pNext) {
cout << pNode->nData << endl;
}
// Clean up
DeleteAllNodes();
}