#ifndef F_FIND_WORK_TEST5_REVERSE_PRINTF_LIST_20171111_JHASKDFJHASF_H_
#define F_FIND_WORK_TEST5_REVERSE_PRINTF_LIST_20171111_JHASKDFJHASF_H_
#include <stdio.h>
#include "DataStructe_Stack.h"
template<class T>
struct FNode
{
T tValue;
FNode *pFNodeNext;
};
template<class T>
void ClearList(FNode<T> **pHead)
{
FNode<T> *pCurNode = *pHead;
while(*pHead)
{
pCurNode= (*pHead)->pFNodeNext;
delete *pHead;
*pHead = pCurNode;
}
pHead = NULL;
}
template<class T>
void PrintLite(FNode<T> *pHead)
{
const char *tType= typeid(T).name();
FNode<T> *pCurNode = pHead;
while (pCurNode)
{
if (0 == strcmp(tType, "int"))
{
TRACE("%d, ", pCurNode->tValue);
}
else if (0 == strcmp(tType, "float"))
{
TRACE("%f, ", pCurNode->tValue);
}
pCurNode = pCurNode->pFNodeNext;
}
TRACE("\n");
}
template<class T>
BOOL Pushback(FNode<T> **pHead, const T tValue)
{
FNode<T> *pCurNode = new FNode<T>;
pCurNode->tValue = tValue;
pCurNode->pFNodeNext = NULL;
if (*pHead == NULL)
{
*pHead = pCurNode;
}
else
{
FNode<T> *pEndNode = *pHead;
while(pEndNode->pFNodeNext)
{
pEndNode = pEndNode->pFNodeNext;
}
pEndNode->pFNodeNext = pCurNode;
}
return TRUE;
}
template<class T>
BOOL RemoveNode(FNode<T> **pHead, const T tValue)
{
if((*pHead == NULL) || (pHead == NULL))
{
return TRUE;
}
FNode<T> *pDeleteNode = NULL;
if((*pHead)->tValue == tValue)
{
pDeleteNode = *pHead;
*pHead = (*pHead)->pFNodeNext;
}
else
{
FNode<T> *pPreNode = *pHead;
while(pPreNode->pFNodeNext && (pPreNode->pFNodeNext->tValue != tValue))
{
pPreNode = pPreNode->pFNodeNext;
}
if(pPreNode->pFNodeNext && (pPreNode->pFNodeNext->tValue == tValue))
{
pDeleteNode = pPreNode->pFNodeNext;
pPreNode->pFNodeNext = pPreNode->pFNodeNext->pFNodeNext;
}
}
if (pDeleteNode)
{
delete pDeleteNode;
pDeleteNode = NULL;
}
return TRUE;
}
template<class T>
void ReversePrintLiteByStack(FNode<T> *pHead)
{
TRACE("ReversePrintLiteByStack=>");
FNode<T> *pCurNode = pHead;
F_DataStructeStack<T> FStractData(10);
while (pCurNode)
{
FStractData.InsertData(pCurNode->tValue, 0);
pCurNode = pCurNode->pFNodeNext;
}
TraceDataBase(FStractData.GetData(), FStractData.GetCurDataSize());
TRACE("\n");
}
template<class T>
void ReversePrintLiteByFunc(FNode<T> *pHead)
{
if(pHead)
{
if(pHead->pFNodeNext)
{
ReversePrintLiteByFunc(pHead->pFNodeNext);
}
const char *pType = typeid(T).name();
if(0 == strcmp(pType, "int"))
{
TRACE("%d, ", pHead->tValue);
}
else if(0 == strcmp(pType, "float"))
{
TRACE("%f, ", pHead->tValue);
}
}
}
void F_Test4_ReversePrintfList()
{
FNode<int> *pHead = NULL;
Pushback(&pHead, 10);
Pushback(&pHead, 8);
Pushback(&pHead, 20);
Pushback(&pHead, 5);
Pushback(&pHead, 26);
Pushback(&pHead, 9);
Pushback(&pHead, 4);
Pushback(&pHead, 2);
PrintLite(pHead);
ReversePrintLiteByStack(pHead);
TRACE("ReversePrintLiteByFunc=>");
ReversePrintLiteByFunc(pHead);
TRACE("\n");
RemoveNode(&pHead, 10);
RemoveNode(&pHead, 26);
RemoveNode(&pHead, 2);
RemoveNode(&pHead, 22);
PrintLite(pHead);
ClearList(&pHead);
FNode<float> *pHeadF = NULL;
Pushback(&pHeadF, (float)1.5);
Pushback(&pHeadF, (float)8.658);
Pushback(&pHeadF, (float)2.265);
Pushback(&pHeadF, (float)22.456);
PrintLite(pHeadF);
ReversePrintLiteByStack(pHeadF);
TRACE("ReversePrintLiteByFunc=>");
ReversePrintLiteByFunc(pHeadF);
TRACE("\n");
ClearList(&pHeadF);
}
#endif
![这里写图片描述](https://img-blog.csdn.net/20171113104140282?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZmpoX3RpZ2VyTWFu/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)