/********************************************************************
created:    2005/12/22
created:    22:12:2005   14:06
filename:     ReservePrint.c
author:        Liu Qi

  purpose:    鏈表的逆序輸出
********************************************************************
*/


#include 
<stdio.h>
#include 
<assert.h>
#include 
<time.h>
#include 
"../sllist.h"

#define MAX_NUM 10



/*===========================================================================
**    Function name:        GetRandList
**    Parameter:            void
**    Precondition:        void
**    Description:        構造一個由隨機數組成的鏈表
**    Return value:        鏈表的頭節點指針
**    Author:                Liu Qi, 2005/12/18
===========================================================================
*/


List GetRandList(
void)
{
    
int i;
    
    List L 
= SLL_Create();
    
    srand( (unsigned)time( NULL ) ); 
    
for ( i = 0; i < MAX_NUM; i++)
    
{
        SLL_PushFront(rand() 
% 20, L);
    }

    
    
return L;
}




/*===========================================================================
* Function name:    ReservePrint
* Parameter:        L: 鏈表頭指針
* Precondition:        NULL != L
* Description:        遞歸實現鏈表的逆序輸出    
* Return value:        void
* Author:            Liu Qi,  [12/22/2005]
===========================================================================
*/

void ReservePrint(List L)
{
    assert( NULL 
!= L );
    
    
if (NULL != L->Next)
    
{
        ReservePrint(L
->Next);
    }

    
else
    
{
        
//遞歸出口,一般把遞歸出口寫作前面^_^
    }

    
    printf(
" %d ", L->Element);
}





int main(int argc, char *argv[])
{
    List L 
= GetRandList();
    SLL_PrintList(L);
    
    printf(
"\n");
    
    ReservePrint(L
->Next);
    
    printf(
"\n");
    
    SLL_DeleteList(L);
    
    
return 0;
}


遞歸是個好東西啊,搞明白了覺得邏輯很清晰,結構也很好,就是效率不高^_^,用棧來模擬遞歸看起來就不好懂了。