标题:链表遍历问题
取消只看楼主
wuwei11263
Rank: 2
等 级:论坛游民
帖 子:8
专家分:55
注 册:2007-4-2
结帖率:100%
已结贴  问题点数:2 回复次数:1 
链表遍历问题
大家好,自己写了个链表创建和打印程序,编译和运行都正常,就是遍历的时候只输出最后一个节点,请大家帮我指正,谢谢大家,下面附上源代码:

/*创建链表,并遍历链表
   时间:2012.02.08
   作者:david
*/
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>

    typedef struct Node{
        int data;
        struct Node *next;
        }Node,*pNode;
        
        //函数声明
          pNode create();//创建链表
          void  travel(pNode phead);//遍历链表
        
        int  main(void){
             pNode phead;
             phead=create();
             travel(phead);
             return 0;
             }
           pNode create(){//输入链表长度和数据构造链表
                    int i,len,val;
                    pNode phead=(pNode)malloc(sizeof(Node));  
                    if(phead==NULL){
                       printf("系统分配失败,程序退出");
                       exit(-1);
                         }   
                    pNode ptail=phead;
                    printf("请输入链表的长度\n");
                    scanf("%d",&len);
                    for(i=0;i<len;i++){
                          printf("请输入链表的第%d的数据\n",i+1);
                          scanf("%d",&val);
                          pNode pnew=(pNode)malloc(sizeof(Node));
                          if(pnew==NULL){
                            printf("系统分配失败,程序退出");
                             exit(-1);
                         }   
                         pnew->data=val;
                         phead->next=pnew;
                         pnew->next=NULL;
                         ptail=pnew;
                          }
                          return phead;
                          }
         void  travel(pNode phead){//输入头指针,如果指针的值不为空就遍历
               
               pNode p=phead->next;
               while(p!=NULL){
                       printf("%d",p->data);
                       p=p->next;
                       }
                     }  
搜索更多相关主题的帖子: include next void 源代码 travel 
2012-02-09 09:03
wuwei11263
Rank: 2
等 级:论坛游民
帖 子:8
专家分:55
注 册:2007-4-2
得分:0 
回复 2楼 tangtangt
谢谢,运行通过了,谢谢!
2012-02-09 13:04



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-360620-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.792328 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved