注册 登录
编程论坛 C++教室

建立一个线性链表并存入数据

qq1274371820 发布于 2014-12-29 22:28, 538 次点击


            输入旅客姓名、年龄、性别、到达日期和所需房间等级 ,建立一个线性链表并将数据存入其中。请问这个要怎么改-。-

#include "stdio.h"
#include "stdlib.h"
typedef int Elemtype;
typedef struct Lnode
{
    char name;
    int age;
    char sex;
    int data;
    int dengji;
    Elemtype data;
    struct Lnode *next;
}Lnode;
Lnode *creat_L()
{
    Lnode *h,*p,*s,*t;
    Elemtype x;
    h=(Lnode *)malloc(sizeof(Lnode));
    h->next=NULL;
    s=h;
    printf("输入旅客姓名、年龄、性别、到达日期和所需房间等级");
    scanf("%c%d%c%d%d",&t.name,&t.age,&t.sex,&t.data,&t.dengji);
    while(x!=-111)
    {
        p=(Lnode *)malloc(sizeof(Lnode));
        p->data=x;
        p->next=NULL;
        s->next=p;
        s=p;
        printf("%c %d %c %d %d",&t.name,&t.age,&t.sex,&t.data,&t.dengji);
        printf("\n data=?");
        scanf("%c%d%c%d%d",&t.name,&t.age,&t.sex,&t.data,&t.dengji);
    }   
    if(h->next==NULL)
    {
        printf("\n程序出错!");
    }
    return(h);
}

4 回复
#2
yangfrancis2014-12-30 16:35
p=(Lnode *)malloc(sizeof(Lnode));
这句改成p=new Lnode;试一下看能不能通过
#3
tiucdccsgz2015-01-04 21:27
亲,你真是太马虎了。。。。。。。。。。。。
程序代码:
#include "stdio.h"
#include "stdlib.h"
typedef int Elemtype;
typedef struct Lnode
{
    char name;
    int age;
    char sex;
    int data;    //<----------------------
    int dengji;                        //  |            是不是一样的呢亲 ?
    Elemtype data; //<-------------------
   
//struct
    Lnode *next;
} Lnode;
Lnode *creat_L()
{
    Lnode *h,*p,*s,*t;
    Elemtype x;
    h=(Lnode *)malloc(sizeof(Lnode));
    h->next=NULL;
    s=h;
    printf("输入旅客姓名、年龄、性别、到达日期和所需房间等级");
    scanf("%c%d%c%d%d",&t.name,&t.age,&t.sex,&t.data,&t.dengji);
    while(x!=-111)
    {
        p=(Lnode *)malloc(sizeof(Lnode));
        p->data=x;
        p->next=NULL;
        s->next=p;
        s=p;
        printf("%c %d %c %d %d",&t.name,&t.age,&t.sex,&t.data,&t.dengji);
        printf("\n data=?");
        scanf("%c%d%c%d%d",&t.name,&t.age,&t.sex,&t.data,&t.dengji);
    }   
    if(h->next==NULL)
    {
        printf("\n程序出错!");
    }
    return(h);
}
#4
tiucdccsgz2015-01-04 21:27
亲,你真是太马虎了。。。。。。。。。。。。
程序代码:
#include "stdio.h"
#include "stdlib.h"
typedef int Elemtype;
typedef struct Lnode
{
    char name;
    int age;
    char sex;
    int data;    //<----------------------
    int dengji;                        //  |            是不是一样的呢亲 ?
    Elemtype data; //<-------------------
   
//struct
    Lnode *next;
} Lnode;
Lnode *creat_L()
{
    Lnode *h,*p,*s,*t;
    Elemtype x;
    h=(Lnode *)malloc(sizeof(Lnode));
    h->next=NULL;
    s=h;
    printf("输入旅客姓名、年龄、性别、到达日期和所需房间等级");
    scanf("%c%d%c%d%d",&t.name,&t.age,&t.sex,&t.data,&t.dengji);
    while(x!=-111)
    {
        p=(Lnode *)malloc(sizeof(Lnode));
        p->data=x;
        p->next=NULL;
        s->next=p;
        s=p;
        printf("%c %d %c %d %d",&t.name,&t.age,&t.sex,&t.data,&t.dengji);
        printf("\n data=?");
        scanf("%c%d%c%d%d",&t.name,&t.age,&t.sex,&t.data,&t.dengji);
    }   
    if(h->next==NULL)
    {
        printf("\n程序出错!");
    }
    return(h);
}
#5
qq12743718202015-11-15 13:00


         啊是的
1