// printlistreverse.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <stack>
using namespace std;
typedef struct node
{
int val;
node* pNext;
}NODE;
NODE* gpstHead = NULL;
void init_list(int cnt)
{
gpstHead = new NODE;
NODE* ptr = gpstHead;
ptr->val = 0;
for (int i=1; i<cnt; i++)
{
ptr->pNext = new NODE;
ptr = ptr->pNext;
ptr->val = i;
}
ptr->pNext = NULL;
}
void destroy_list()
{
NODE* ptr = gpstHead;
gpstHead = NULL;
NODE* ptmp = NULL;
while(ptr != NULL)
{
ptmp = ptr;
ptr = ptr->pNext;
}
}
void print_list_reverse0(NODE* ptr)
{
if (ptr->pNext == NULL)
{
printf("%d ", ptr->val);
}
else
{
print_list_reverse0(ptr->pNext);
printf("%d ", ptr->val);
}
}
void print_list_reverse1(NODE* ptr)
{
stack<int> s;
while(ptr != NULL)
{
s.push(ptr->val);
ptr = ptr->pNext;
}
while(!s.empty())
{
printf("%d ",s.top());
s.pop();
}
}
int _tmain(int argc, _TCHAR* argv[])
{
init_list(10);
print_list_reverse0(gpstHead);
printf("\n");
destroy_list();
init_list(20);
print_list_reverse0(gpstHead);
printf("\n");
destroy_list();
return 0;
}
//
#include "stdafx.h"
#include <stack>
using namespace std;
typedef struct node
{
int val;
node* pNext;
}NODE;
NODE* gpstHead = NULL;
void init_list(int cnt)
{
gpstHead = new NODE;
NODE* ptr = gpstHead;
ptr->val = 0;
for (int i=1; i<cnt; i++)
{
ptr->pNext = new NODE;
ptr = ptr->pNext;
ptr->val = i;
}
ptr->pNext = NULL;
}
void destroy_list()
{
NODE* ptr = gpstHead;
gpstHead = NULL;
NODE* ptmp = NULL;
while(ptr != NULL)
{
ptmp = ptr;
ptr = ptr->pNext;
}
}
void print_list_reverse0(NODE* ptr)
{
if (ptr->pNext == NULL)
{
printf("%d ", ptr->val);
}
else
{
print_list_reverse0(ptr->pNext);
printf("%d ", ptr->val);
}
}
void print_list_reverse1(NODE* ptr)
{
stack<int> s;
while(ptr != NULL)
{
s.push(ptr->val);
ptr = ptr->pNext;
}
while(!s.empty())
{
printf("%d ",s.top());
s.pop();
}
}
int _tmain(int argc, _TCHAR* argv[])
{
init_list(10);
print_list_reverse0(gpstHead);
printf("\n");
destroy_list();
init_list(20);
print_list_reverse0(gpstHead);
printf("\n");
destroy_list();
return 0;
}