当前位置: 代码迷 >> C语言 >> 谁能帮下忙??
  详细解决方案

谁能帮下忙??

热度:281   发布时间:2008-04-08 17:37:18.0
谁能帮下忙??
#include<stdio.h>
#include<malloc.h>
#define N 10

typedef struct node
{
    char name[N];
    struct node *next;
}stu;

stu *listcreat(int n)
{
    stu *head,*p1,*p2;
    int i;
if((head=(stu *)malloc(sizeof(stu)))==NULL){    
        printf("内存分配失败!\n");
            exit(0);
            }
        head->next=NULL;
    p1=head;
for(i=0;i<n;i++){
    if((p2=(stu *)malloc(sizeof(stu)))==NULL){
            printf("内存分配失败!\n");
                exit(0);
                    }
        p1->next = p2;
            printf("请输入第%d个人姓名:\n",i+1);
                scanf("%s",&p2->name);
                p2->next=NULL;
                p1=p2;
        }    
    return head;
}

void main()
{
    stu *head;
    int n;
    printf("请输入人数:");
    scanf("%d",&n);
    head=listcreat(n);
    
}

[[it] 本帖最后由 xshj1025 于 2008-4-8 18:08 编辑 [/it]]
搜索更多相关的解决方案: stu  内存  head  next  

----------------解决方案--------------------------------------------------------
好像是循环的问题
----------------解决方案--------------------------------------------------------
什么问题?
----------------解决方案--------------------------------------------------------
#include<stdio.h>
#include<malloc.h>
#define N 10

typedef struct node
{
    char name[N];
    struct node *next;
}stu;

stu *listcreat(int n)
{
    stu *head,*p1,*p2;
    int i;
if((p1=(stu *)malloc(sizeof(stu)))==NULL){
        printf("fail!\n");
            exit(0);
            }
        head=p1; /*这里我该啦~~~*/
for(i=0;i<n;i++){
    if((p2=(stu *)malloc(sizeof(stu)))==NULL){
            printf("fail!\n");
                exit(0);
                    }
        p1->next = p2;
            printf("please enter %d man name:\n",i+1);
                scanf("%s",&p2->name);
                p2->next=NULL;
                p1=p2;
        }
    return head;
}
void display(stu *p)
{  stu *q;
  q=p->next;
   while(q->next!=NULL)
    { printf("%s",q->name); q=q->next;}
   printf("%s",q->name);

}

void main()
{
    stu *head;
    int n;
    printf("please enter number:");
    scanf("%d",&n);
    head=listcreat(n);
    display(head);
}
以上是我将你的程序该了以后可以正常达到效果啦~ 顺便加了一个用来遍历的函数,方便看结果如何~ 你自己试试吧~
----------------解决方案--------------------------------------------------------
感谢winnerwxin
我发的有点问题,后来的问题找到了  还是很感谢大家帮忙,很谢谢winnerwxin
----------------解决方案--------------------------------------------------------
  相关解决方案