鱼C论坛

 找回密码
 立即注册
查看: 3790|回复: 2

单链表 如何 逆序 输出

[复制链接]
发表于 2015-12-25 18:25:22 | 显示全部楼层 |阅读模式
15鱼币
如题,我百度了一下,百度上有说 将链表调转后 输出,但是不会,还有一种用 栈 输出,采用递归。
我采用了后面一种,但是输出 答案完全错误。

  1. # include <stdio.h>
  2. # include <stdlib.h>
  3. struct node
  4. {
  5.         int data;
  6.         struct node *next;
  7. };
  8. void digui(struct node* head)
  9. {
  10.         if(head == NULL)
  11.           return;
  12.     if(head->next != NULL)
  13.        digui(head->next);
  14.     printf("%d ",head->next);
  15. }
  16. int main()
  17. {
  18.         struct node *head,*p,*q,*t;
  19.         int i,n,m,a,b;
  20.         scanf("%d",&n);
  21.         head=NULL;
  22.         for(i=1;i<=n;i++)
  23.         {
  24.                 scanf("%d",&a);
  25.                 p=(struct node*)malloc(sizeof(struct node));
  26.                 p->data=a;
  27.                 p->next=NULL;
  28.                 if(head==NULL)
  29.                    head=p;
  30.         else
  31.            q->next=p;
  32.         q=p;
  33.         }
  34.         digui(head);  //递归逆序
  35. /*        t=head;
  36.         while(t != NULL)
  37.          {
  38.                 printf("%d",t->data);
  39.                 t=t->next;
  40.         }   */
  41. //这部分是正序输出,运行可以正常输出  
  42.         return 0;
  43. }
复制代码



这代码主要是 递归那 部分错了,但是不会改,数字输出完全乱了。

最佳答案

查看完整内容

# include # include struct node { int data; struct node *next; }; void digui(struct node* head) { if(head == NULL) return; if(head->next != NULL) digui(head->next); printf("%d ",head->data); } int main() { struct node *head,*p,*q,*t; int i,n,m,a,b; scanf("%d",&n); head=NULL; for(i=1;idat ...
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2015-12-25 18:25:23 | 显示全部楼层
# include <stdio.h>
# include <stdlib.h>
struct node
{
        int data;
        struct node *next;
};
void digui(struct node* head)
{
        if(head == NULL)
          return;
    if(head->next != NULL)
       digui(head->next);
    printf("%d ",head->data);
}
int main()
{
        struct node *head,*p,*q,*t;
        int i,n,m,a,b;
        scanf("%d",&n);
        head=NULL;
        for(i=1;i<=n;i++)
        {
                scanf("%d",&a);
                p=(struct node*)malloc(sizeof(struct node));
                p->data=a;
                p->next=NULL;
                if(head==NULL)
                   head=p;
        else
           q->next=p;
        q=p;
        }
        digui(head);  //递归逆序
/*        t=head;
        while(t != NULL)
         {
                printf("%d",t->data);
                t=t->next;
        }   */
//这部分是正序输出,运行可以正常输出  
        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2015-12-26 11:32:58 | 显示全部楼层
ryxcaixia 发表于 2015-12-26 08:47
# include
# include
struct node

O(∩_∩)O谢谢,又粗心了。我还检查了好几遍都没有发现错误:cry
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-17 02:56

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表