Skip to content

Instantly share code, notes, and snippets.

@kaka19ace
Created May 11, 2014 12:27
Show Gist options
  • Save kaka19ace/0028fd6aad8335dab91a to your computer and use it in GitHub Desktop.
Save kaka19ace/0028fd6aad8335dab91a to your computer and use it in GitHub Desktop.
/*
copy from rtmpd 781
*/
#ifndef _LINKEDLIST_H
#define _LINKEDLIST_H
template<typename T>
struct LinkedListNode {
LinkedListNode<T> *pPrev;
LinkedListNode<T> *pNext;
T info;
};
template<typename T>
LinkedListNode<T> *AddLinkedList(LinkedListNode<T> *pTo, T info, bool after) {
LinkedListNode<T> *pResult = new LinkedListNode<T>;
pResult->pNext = NULL;
pResult->pPrev = NULL;
pResult->info = info;
if (pTo != NULL) {
if (after) {
pTo->pNext = pResult;
pResult->pPrev = pTo;
} else {
pResult->pNext = pTo;
pTo->pPrev = pResult;
}
}
return pResult;
}
template<typename T>
LinkedListNode<T> *LastLinkedList(LinkedListNode<T> *pNode) {
while (pNode != NULL) {
if (pNode->pNext == NULL)
return pNode;
pNode = pNode->pNext;
}
return NULL;
}
template<typename T>
LinkedListNode<T> *RemoveLinkedList(LinkedListNode<T> *pNode) {
LinkedListNode<T> *pPrev = pNode->pPrev;
LinkedListNode<T> *pNext = pNode->pNext;
if (pPrev != NULL)
pPrev->pNext = pNext;
if (pNext != NULL)
pNext->pPrev = pPrev;
delete pNode;
if (pPrev != NULL)
return LastLinkedList<T > (pPrev);
return LastLinkedList<T > (pNext);
}
#endif /* _LINKEDLIST_H */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment