| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1294 人关注过本帖
标题:怎样创建一个链表
只看楼主 加入收藏
浅暗花璃
Rank: 1
等 级:新手上路
帖 子:53
专家分:0
注 册:2016-3-31
结帖率:77.78%
收藏
已结贴  问题点数:6 回复次数:2 
怎样创建一个链表
怎样创建一个链表
struct student *create()
{
    struct student *head,*p1,*p2;
    int i=0,n;
    head=NULL;
    printf("plaese input n:");
    scanf("%d",&n);
    p1=((struct student *)malloc(sizeof(struct student)));
    p2=((struct student *)malloc(sizeof(struct student)));
    scanf("%d",&p1->num);
     //scanf("%s",p1->name);
     //fflush(stdin);
    // printf("plase input sex,f-女,m-男:");
    // scanf("%c",&p1->sex);
     //fflush(stdin);
     //scanf("%d",&p1->old);
    for(i=0;i<n;i++)
    {   
        if(i==0)
            head=p1;
        else{
             p1->next=NULL;
            p2->next=p1;
            p2=p1;
        }
   p1=(struct student *)malloc(sizeof(struct student));

    scanf("%d",&p1->num);
   }
  
   
    free(p1);
    return(head);

    }
搜索更多相关主题的帖子: create 
2016-06-02 19:45
linlulu001
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:20
帖 子:944
专家分:4047
注 册:2016-4-13
收藏
得分:3 
以下是引用浅暗花璃在2016-6-2 19:45:50的发言:

怎样创建一个链表
#define LEN sizeof(struct student)//一开始就定义好,方便后面书写。
struct student *create()
{
    struct student *head,*p1,*p2;
    int i=0,n=0;
    head=NULL;
    //printf("plaese input n:");
    //scanf("%d",&n);
    p2=p1=((struct student *)malloc(LEN);
    //p2=((struct student *)malloc(sizeof(struct student)));
    scanf("%d",&p1->num);
     //scanf("%s",p1->name);
     //fflush(stdin);
    // printf("plase input sex,f-女,m-男:");
    // scanf("%c",&p1->sex);
     //fflush(stdin);
     //scanf("%d",&p1->old);
   /* for(i=0;i<n;i++)
    {   
        if(i==0)
            head=p1;
        else{
             p1->next=NULL;
            p2->next=p1;
            p2=p1;
        }
   p1=(struct student *)malloc(sizeof(struct student));

    scanf("%d",&p1->num);
   }*/

  while(p1->num!=0)
    {n++;
        if(n==1) head=p1;
        else
           p2->next=p1;
           p2=p1;
   
        p1=(struct student *)malloc(LEN);
        scanf("%d",&p1->num);
    }
    p2->next=NULL;

    free(p1);
    return(head);

    }

红色我认为你不对的地方,蓝色是我修改后的。不理解再问吧。
2016-06-02 20:35
吹水佬
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:451
帖 子:10539
专家分:42927
注 册:2014-5-20
收藏
得分:3 
程序代码:
#include <stdio.h>
#include <stdlib.h>

typedef struct student
{
    int  num;
    char name[20];
    char sex;
    int  old;
    struct student *next;
} STUDENT, *PSTUDENT;


PSTUDENT _Create()
{
    PSTUDENT head=NULL, p1=NULL, p2=NULL;
    int i=0,n;
    head=NULL;
    printf("plaese input n: ");
    scanf("%d",&n);
    for(i=0; i<n; i++)
    {
        p1=(PSTUDENT)malloc(sizeof(STUDENT));
        printf("\nplase input ID:%d\n", i+1); 
        fflush(stdin);
        printf("num     : "); scanf("%d",&p1->num);
        fflush(stdin);
        printf("name    : "); scanf("%s",p1->name);
        fflush(stdin);
        printf("sex(m/f): "); scanf("%c",&p1->sex);
        fflush(stdin);
        printf("old     : "); scanf("%d",&p1->old);
        p1->next = NULL;
        if (i==0)
        {
            head = p1;
            p2 = p1;
        }
        else
        {
            p2->next = p1;
            p2 = p1;
        }
    }
    return(head);
}

void _Free(PSTUDENT head)
{
    PSTUDENT p;
    while(head != NULL)
    {
        p = head->next;
        free(head);
        head = p;
    }
}

void _List(PSTUDENT p)
{
    for (int i=1; p!=NULL; i++)
    {
        printf("\nList ID:%d\n", i); 
        printf("num     : %d\n", p->num);
        printf("name    : %s\n", p->name);
        printf("sex(m/f): %c\n", p->sex);
        printf("old     : %d\n", p->old);
        p = p->next;
    }
}

main()
{
    PSTUDENT head = _Create();
    if (head != NULL)
    {
        _List(head);
        _Free(head);
    }
}
2016-06-02 21:14
快速回复:怎样创建一个链表
数据加载中...
 
   



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

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