标题:输入的时候只能输一组,大神求助
取消只看楼主
tian小韵
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2016-5-26
结帖率:100%
已结贴  问题点数:20 回复次数:3 
输入的时候只能输一组,大神求助
#include<stdio.h>
#include<stdlib.h>
#define SIZE sizeof(struct student)
struct student
{
    long int num;
    char name[20];
    char sex;
    int age;
    struct student *next;
};
struct student *create()
{
    struct student *head,*p1,*p2;
    head=NULL;
    p1=(struct student *)malloc(SIZE);
    scanf("%ld%s%c%d",&p1->num,p1->name,&p1->sex,&p1->age);
    while(p1->num!=0)
    {
        if(head=NULL)
            head=p1;
        else
            p2->next=p1;
    p2=p1;
    p1=(struct student *)malloc(SIZE);
    scanf("%ld%s%c%d",&p1->num,p1->name,&p1->sex,&p1->age);
    }
    p2->next=NULL;
    free(p1);
    return head;
}
struct student *delet(struct student *head,int num)
{
    struct student *p1=head,*p2;
    while(p1->num!=num&&p1->next!=NULL)
    {
        p2=p1;
        p1=p1->next;
    }
    if(p1->num==num)
    {
        if(p1==head)
            head=p1->next;
        else
            p2->next=p1->next;
    }
    return head;
}
void print(struct student *head)
{
    struct student *p1;
    p1=head;
    if(head=NULL)
        printf("为空表");
    else
    {
        do
        {
            printf("%ld%s%c%d",p1->num,p1->name,p1->sex,p1->age);
            p1=p1->next;
        }
        while(p1->next!=NULL);
    }
}
void main()
{
    struct student *head;
    long int num;
    head=create();
    printf("原来的链表:");
    print(head);
    printf("Please enter you want deleted:");
    scanf("%ld",&num);
    delet(head,num);
    printf("删除后的链表:");
    print(head);
}

搜索更多相关主题的帖子: include create 
2016-05-26 23:19
tian小韵
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2016-5-26
得分:0 
回复 3楼 wanglianyi1
是的
2016-05-27 23:48
tian小韵
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2016-5-26
得分:0 
回复 2楼 grmmylbs
谢谢你的提醒,不过为什么有的不用给P2开辟空间啊,它应该只是一个结构体指针变量吧

#include<stdio.h>
#include<stdlib.h>
#define SIZE sizeof(struct student)
struct student
{
    long int num;
    char name[20];
    char sex;
    int age;
    struct student *next;
};
struct student *create()
{
    struct student *head,*p1,*p2;
    head=NULL;
    p1=(struct student *)malloc(SIZE);
    scanf("%ld%s%c%d",&p1->num,p1->name,&p1->sex,&p1->age);
    while(p1->num!=0)
    {
        if(head=NULL)
            head=p1;
        else
            p2->next=p1;
    p2=p1;
    p1=(struct student *)malloc(SIZE);
    scanf("%ld%s%c%d",&p1->num,p1->name,&p1->sex,&p1->age);
    }
    p2->next=NULL;
    free(p1);
    return head;
}
struct student *delet(struct student *head,int num)
{
    struct student *p1=head,*p2;
    while(p1->num!=num&&p1->next!=NULL)
    {
        p2=p1;
        p1=p1->next;
    }
    if(p1->num==num)
    {
        if(p1==head)
            head=p1->next;
        else
            p2->next=p1->next;
    }
    return head;
}
void print(struct student *head)
{
    struct student *p1;
    p1=head;
    if(head=NULL)
        printf("为空表");
    else
    {
        do
        {
            printf("%ld%s%c%d",p1->num,p1->name,p1->sex,p1->age);
            p1=p1->next;
        }
        while(p1->next!=NULL);
    }
}
void main()
{
    struct student *head;
    long int num;
    head=create();
    printf("原来的链表:");
    print(head);
    printf("Please enter you want deleted:");
    scanf("%ld",&num);
    delet(head,num);
    printf("删除后的链表:");
    print(head);
}
2016-05-27 23:53
tian小韵
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2016-5-26
得分:0 
回复 7楼 linlulu001
所以它应不应该再开辟空间?
2016-05-28 10:25



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-465290-1-1.html




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

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