鱼C论坛

 找回密码
 立即注册
查看: 1596|回复: 4

建双向链表,请问为什么打印不出来

[复制链接]
发表于 2016-12-29 16:34:23 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x

运行结果 一直是什么不能以read方式打开
源代码在附件里
我能怎么办
我也很绝望啊。。。
求大佬指点


#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct person
{
        char name[10];
        int count;
        struct person *right;
        struct person *left;
       
};
//创建链表
struct person *creat()
{
    int n=0;
        struct person *p1,*p2,*head;
    p1=p2=(struct person*)malloc(sizeof(struct person));
    printf("Please input the  person\n");
    scanf("%s",&p1->name);
    p1->count=0;
    head=NULL;
   for(n=0;n<2;n++)
  {
   
        if(n==0)
        {
                head=p1;
         }
         else
         {
                 p2->right=p1;
                 p1->left=p2;
                
         }
         p2=p1;
         n++;
         p1=(struct person*)malloc(sizeof(struct person));
     printf("Please input the  person\n");
     scanf("%s",&p1->name);
     p1->count=0;
   
   }
        return head;
}
//打印链表
void print(struct person *head)
{
    struct person *p;
    p=head;
     if(head!=NULL)
          for (int i = 0; i<2; i++)

        {
                printf("%s %d\n", p->name, p->count);
                p = p->right;
        }
         
}

//

int main (void)
{
    struct person *candidate;

        char name[200]={0};
    candidate=creat();   
    print(candidate);
  
}


文档.txt

4.76 KB, 下载次数: 7

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-12-29 19:37:10 | 显示全部楼层
本帖最后由 代码农民 于 2016-12-29 21:46 编辑
  1. //dblist.h
  2. #ifndef _DB_LIST
  3. #define _DB_LIST

  4. struct Node; //不完整的声明,结构的定义在.c文件中
  5. typedef struct Node* Head; //头结点的votes用来记人数
  6. typedef struct Node* Person; //结点的votes用来记投票

  7. #endif

  8. Head Create( void );
  9. Person AddVotes( char* PersonName, Head X );
  10. void Print( Head X );
  11. int PersonsNumbers( Head X);
复制代码

  1. //dblist.c
  2. #include "dblist.h"
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <string.h>




  6. struct Node
  7. {
  8.     char     name[16];
  9.     int      votes;
  10.     Person   Ahead;
  11.     Person   Next;
  12. };



  13. Head
  14. Create( )
  15. {
  16.     Head  p;

  17.     p = malloc( sizeof( struct Node ) );
  18.     if( P == NULL )
  19.         {
  20.             printf( "Head创建时内存不足\n" );
  21.             exit( 1 );
  22.         }
  23.     strcpy( p->name, "a" );
  24.     p->votes = 0;
  25.     p-> Ahead = Next = NULL;
  26.     return p;
  27. }






  28. Head  //如果表里有这个人就为这个人的票数加1,没有的话就插入一个结点,插入后整个表是排序好了的
  29. AddVotes( char* PersonName, Head X )
  30. {
  31.     Person  p1, p2, p3;
  32.     int a;
  33.    
  34.     p2 = X->Next;
  35.     p3 = X;
  36.     while( p2 != NULL )
  37.     {   
  38.         a = strcmp( PersonName, p2->name );
  39.         if( a == 0 )
  40.         {  
  41.              ++p2->votes;
  42.              return X;
  43.         }
  44.         else
  45.         if( a > 0 )
  46.         {
  47.             p3 = p2;
  48.             p2 = p2->Next;
  49.         }
  50.         else
  51.             break;
  52.     }
  53.    
  54.     p1 = malloc( sizeof( struct Node ) );
  55.         if( p1 == NULL )
  56.             {
  57.                 printf( "Person创建时内存不足\n" );
  58.                 exit( 1 );
  59.             }
  60.         strcpy( p1->name, PersonName );
  61.         p1->votes = 1; //因为这个不存在的人被投了一票
  62.         ++X->votes;    //表中人数加1


  63.     p1->Ahead = p3;
  64.     p1->Next  = p2;
  65.     p3->Next = p1;
  66.     if( p2 != NULL )
  67.         p2->Ahead = p1;
  68.    
  69.    
  70.     return X;
  71. }


  72. void
  73. Print( Head X )
  74. {
  75.     Person p;

  76.     for( p = X->Next; p != NULL; p = p->Next )
  77.         printf( "%16s:%d\n", p->name, p->votes );
  78. }


  79. int   //表中的人数
  80. PersonsNumbers( Head X )
  81. {
  82.         return X->votes;
  83. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-1-3 19:34:02 | 显示全部楼层
楼主,你head=p1;的时候,当时p1这个结构体的上下级指针是空的,之后你再没有对这个head重写过,所以你在打印的时候,只能打印出一个,第二个完全找不到。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-1-4 00:47:00 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-1-4 00:47:38 | 显示全部楼层
cb7960588 发表于 2017-1-3 19:34
楼主,你head=p1;的时候,当时p1这个结构体的上下级指针是空的,之后你再没有对这个head重写过,所以你在打 ...

懂了,谢谢!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-10 02:57

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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