33,027
社区成员




delete pList;
// ReList.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
using namespace std;
//定义结点
typedef struct node
{
int data;
node* next;
}NODE,*PNODE;
//定义链表
typedef struct list
{
int len;
PNODE pHeader;
}LIST,*PLIST;
//初始化链表,建立头结点
void InitList(PLIST pList)
{
pList->pHeader = new NODE;
pList->pHeader->next =NULL;
pList->len = 0;
}
//插入数据
void InsertDataToList(PLIST pList,int data)
{
PNODE np;
PNODE p = pList->pHeader;
if ( p == NULL ) return ; //链表还没有初始化,直接返回
while ( p->next != NULL)
{
p = p->next;
}
np= p->next = new NODE;
np->data = data;
np->next = NULL ;
pList->len ++;
}
//逆序打印
void RevPrint(PNODE pNode)
{
PNODE p = pNode;
if( p->next != NULL )
{
p = p->next;
RevPrint(p); //递归打印下一个节点
}
cout<<p->data<<endl; //输出当前结点数值
}
//删除链表,释放空间
void DestroyList(PLIST pList)
{
PNODE p,q;
p = q = pList->pHeader;
while ( p != NULL )
{
q = p->next;
delete p;
p = q;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
PLIST pList = new LIST;
InitList(pList);
for ( int i = 0 ; i < 100; i ++)
{
InsertDataToList(pList,i);
}
RevPrint(pList->pHeader->next); //越过头结点,直接从数据结点开始打印
DestroyList(pList);
return 0;
}