十六是只仙 发表于 2016-12-29 16:34:23

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

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


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

struct person
{
        char name;
        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 theperson\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 theperson\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={0};
    candidate=creat();   
    print(candidate);

}


代码农民 发表于 2016-12-29 19:37:10

本帖最后由 代码农民 于 2016-12-29 21:46 编辑

//dblist.h
#ifndef _DB_LIST
#define _DB_LIST

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

#endif

Head Create( void );
Person AddVotes( char* PersonName, Head X );
void Print( Head X );
int PersonsNumbers( Head X);

//dblist.c
#include "dblist.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>




struct Node
{
    char   name;
    int      votes;
    Person   Ahead;
    Person   Next;
};



Head
Create( )
{
    Headp;

    p = malloc( sizeof( struct Node ) );
    if( P == NULL )
      {
            printf( "Head创建时内存不足\n" );
            exit( 1 );
      }
    strcpy( p->name, "a" );
    p->votes = 0;
    p-> Ahead = Next = NULL;
    return p;
}






Head//如果表里有这个人就为这个人的票数加1,没有的话就插入一个结点,插入后整个表是排序好了的
AddVotes( char* PersonName, Head X )
{
    Personp1, p2, p3;
    int a;
   
    p2 = X->Next;
    p3 = X;
    while( p2 != NULL )
    {   
      a = strcmp( PersonName, p2->name );
      if( a == 0 )
      {
             ++p2->votes;
             return X;
      }
      else
      if( a > 0 )
      {
            p3 = p2;
            p2 = p2->Next;
      }
      else
            break;
    }
   
    p1 = malloc( sizeof( struct Node ) );
      if( p1 == NULL )
            {
                printf( "Person创建时内存不足\n" );
                exit( 1 );
            }
      strcpy( p1->name, PersonName );
      p1->votes = 1; //因为这个不存在的人被投了一票
        ++X->votes;    //表中人数加1


    p1->Ahead = p3;
    p1->Next= p2;
    p3->Next = p1;
    if( p2 != NULL )
      p2->Ahead = p1;
   
   
    return X;
}


void
Print( Head X )
{
    Person p;

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


int   //表中的人数
PersonsNumbers( Head X )
{
        return X->votes;
}

cb7960588 发表于 2017-1-3 19:34:02

楼主,你head=p1;的时候,当时p1这个结构体的上下级指针是空的,之后你再没有对这个head重写过,所以你在打印的时候,只能打印出一个,第二个完全找不到。

十六是只仙 发表于 2017-1-4 00:47:00

代码农民 发表于 2016-12-29 19:37


嗯嗯,感恩

十六是只仙 发表于 2017-1-4 00:47:38

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

懂了,谢谢!
页: [1]
查看完整版本: 建双向链表,请问为什么打印不出来