64,648
社区成员
发帖
与我相关
我的任务
分享
struct ListNode
{
int m_nValue;
ListNode* m_pNext;
};
_declspec(dllexport) ListNode* CreateListNode(int value);
_declspec(dllexport) void ConnectListNodes(ListNode* pCurrent, ListNode* pNext);
_declspec(dllexport) void PrintListNode(ListNode* pNode);
_declspec(dllexport) void PrintList(ListNode* pHead);
_declspec(dllexport) void DestroyList(ListNode* pHead);
_declspec(dllexport) void AddToTail(ListNode** pHead, int value);
_declspec(dllexport) void RemoveNode(ListNode** pHead, int value);
_declspec(dllexport) void PrintListReverse(ListNode* pHead); //无法调用
_declspec(dllexport) void PrintListReverse_Recursion(ListNode* pHead); //无法调用
#include "stdafx.h"
#include "list.h"
#include "stdio.h"
#include "stdlib.h"
#include<stack>
#include<iostream.h>
ListNode* CreateListNode(int value)
{
ListNode* pNode = new ListNode();
pNode->m_nValue = value;
pNode->m_pNext = NULL;
return pNode;
}
void ConnectListNodes(ListNode* pCurrent, ListNode* pNext)
{
if(pCurrent == NULL)
{
printf("ERROR to connect nodes.\n");
exit(1);
}
pCurrent->m_pNext = pNext;
}
void PrintListNode(ListNode* pNode)
{
if(pNode == NULL)
{
printf("pNode is NULL.\n");
}
else
{
printf("pNode is %d.\n", pNode->m_nValue);
}
}
void PrintList(ListNode* pHead)
{
ListNode* pNode = pHead;
while(pNode != NULL)
{
printf(" %d.\t", pNode->m_nValue);
pNode = pNode->m_pNext ;
}
printf("\n");
}
void DestroyList(ListNode* pHead)
{
ListNode* pNode = pHead;
while(pNode != NULL)
{
delete pNode;
pHead = pHead->m_pNext;
pNode = pHead ;
}
}
void AddToTail(ListNode** pHead, int value)
{
ListNode* pNew = new ListNode();
pNew->m_nValue = value;
pNew->m_pNext = NULL;
if(*pHead == NULL)
{
*pHead = pNew;
}
else
{
ListNode* pNode = *pHead;
while(pNode->m_pNext != NULL)
{
pNode = pNode->m_pNext;
}
pNode->m_pNext = pNew;
}
}
void RemoveNode(ListNode** pHead, int value)
{
if(pHead==NULL || *pHead==NULL)
{
return;
}
ListNode* pToBeDelete = NULL;
if((*pHead)->m_nValue == value)
{
pToBeDelete = *pHead;
*pHead = (*pHead)->m_pNext ;
}
else
{
ListNode* pNode = *pHead;
while(pNode->m_pNext != NULL && (pNode)->m_pNext->m_nValue != value)
{
pNode = pNode->m_pNext;
}
if(pNode->m_pNext != NULL && (pNode)->m_pNext->m_nValue == value)
{
pToBeDelete = pNode->m_pNext;
pNode->m_pNext = pNode->m_pNext->m_pNext ;
}
}
if(pToBeDelete != NULL)
{
delete pToBeDelete;
pToBeDelete = NULL;
}
}
////////////////////////////无法调用///////////////////////////////////////////////
void PrintListReverse(ListNode* pHead)
{
std::stack<ListNode*> nodes;
ListNode* pNode = pHead;
while(pNode != NULL)
{
nodes.push(pNode);
pNode = pNode->m_pNext ;
}
while(!nodes.empty())
{
pNode = nodes.top();
printf("%d \t", pNode->m_nValue);
nodes.pop();
}
printf(" \n");
}
void PrintListReverse_Recursion(ListNode* pHead)
{
if(pHead != NULL)
{
if(pHead->m_pNext != NULL)
PrintListReverse_Recursion(pHead->m_pNext);
printf("%d \t", pHead->m_nValue);
}
}
//////////////////////////////无法调用/////////////////////////////////////////////////
#include "stdafx.h"
#include "stdlib.h"
#include "list.h"
#include<iostream.h>
#include<stack>
using namespace std;
//void PrintListReverse(ListNode* pHead);
//void PrintListReverse_Recursion(ListNode* pHead); //需在此申明 否则无法识别
void printlist_re()
{
ListNode* node1 = CreateListNode(1);
ListNode** p_node1 = &node1;
AddToTail(p_node1, 2);
AddToTail(p_node1, 3);
AddToTail(p_node1, 4);
AddToTail(p_node1, 5);
PrintList(*p_node1);
PrintListReverse(node1); //错误 1 error C3861: “PrintListReverse”: 找不到标识符
PrintListReverse_Recursion(node1); //错误 2 error C3861: “PrintListReverse_Recursion”: 找不到标识符
}
int _tmain(int argc, _TCHAR* argv[])
{
printlist_re();
system("pause");
return 0;
}