ngaut

asm/c/c++/......

常用链接

统计

积分与排名

others

something special

经典的c/c++

朋友的网上家园

最新评论

数据结构笔记:递归实现链表的逆序输出

/********************************************************************
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;
}


递归是个好东西啊,搞明白了觉得逻辑很清晰,结构也很好,就是效率不高^_^,用栈来模拟递归看起来就不好懂了。

posted on 2005-12-22 16:13 ngaut 阅读(1596) 评论(7)  编辑 收藏 引用 所属分类: c/c++/ds

评论

# re: 数据结构笔记:递归实现链表的逆序输出 2005-12-22 17:07 小明

用栈的话这样实现:

void ReservePrint(List L)
{
assert( NULL != L );
int count = 0;
int temp;
while (NULL != L->Next)
{
++count;
temp = L->Next;
__asm push temp;
L = L->Next;
}

while(--count>=0)
{
__asm pop temp;
printf(" %d ", temp);
}
}  回复  更多评论   

# re: 数据结构笔记:递归实现链表的逆序输出 2005-12-22 19:14 ngaut

个人觉得存在先进后出的结构时用栈实现更好理解,这里用栈来实现似乎更加好理解一些^_^,另外你的代码有点瑕疵,这里内嵌汇编不合适吧,因为不确定链表的长度,如果大于操作系统给ReservePrint分配的堆栈就会overflow了,不知是否正确^_^  回复  更多评论   

# re: 数据结构笔记:递归实现链表的逆序输出 2005-12-22 19:57 小明

如果会overflow,你的递归方法一样会overflow,因为函数调用,都是使用堆栈来传递参数。


  回复  更多评论   

# re: 数据结构笔记:递归实现链表的逆序输出 2005-12-22 20:28 ngaut

是啊,多谢老兄指点,不过你好像误解我的意思了(其实是我自己没有说清楚)^_^
我的意思是:另外实现一个堆栈,比如链栈,而不是直接用该函数自己的栈,这样就不会overflow了  回复  更多评论   

# re: 数据结构笔记:递归实现链表的逆序输出 2005-12-22 20:49 小明

嗯,默认状态,堆栈大概可以push 250,000 个int,当然这个大小也是可以调整的。

我这样写,主要也是从效率上考虑,使用原始的stack,飞快哦!  回复  更多评论   

# re: 数据结构笔记:递归实现链表的逆序输出 2008-03-26 09:33 song

很好的程序,学习了。3k  回复  更多评论   

# re: 数据结构笔记:递归实现链表的逆序输出 2008-07-31 12:48 haha

printStack(Stack s){
Object o = s.pop();
if(o == null)
return;
printStack(s);
println(o);


}  回复  更多评论   


标题  
姓名  
主页
验证码 *
内容(提交失败后,可以通过“恢复上次提交”恢复刚刚提交的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
[使用Ctrl+Enter键可以直接提交]

相关链接:
网站导航: