标题:一个链表问题包含了节点的 插入 删除 自定义函数的输出 有点问题 大家帮帮 ...
只看楼主
hnuhsg1226
Rank: 9Rank: 9Rank: 9
来 自:中国
等 级:蜘蛛侠
威 望:2
帖 子:314
专家分:1314
注 册:2011-3-27
得分:7 
楼主代码太乱,思路不清晰,你先别按题目实现的意思来,先把几个功能做出来,比如查询,插入,删除之类的,都用单个函数实现,别都放在一起,也便于测试。
下面是我以前写的有关链表的代码,建议去参考下。
https://bbs.bccn.net/thread-335558-1-1.html

我的地盘
2011-12-05 11:07
吴小杰
Rank: 3Rank: 3
来 自:。。。。。。
等 级:论坛游侠
帖 子:58
专家分:107
注 册:2011-11-11
得分:0 
这是我改好后的   谢谢大家的帮忙啊 thank you
#include <stdio.h>
#include <malloc.h>
#define LEN sizeof(struct student)
struct student {
    char num[15];
    long numb;
    char name[20];
    char sex[6];
    float score1;
    float score2;
    float score3;
    struct student*next;
};
int main() {
    void print(struct student*head);
    int n = 1;
    struct student *head, *p1, *p2, *p0;
    head = p1 = p2 = (struct student*) malloc(LEN);
    p1->next = NULL;
    scanf("%s", p1->num);
    scanf("%s %s %f %f %f", p1->name, p1->sex, &p1->score1, &p1->score2,
            &p1->score3);
    p1->numb = atoi(p1->num);
    for (;;) {
        if (n > 1)
            p2->next = p1;
        p2 = p1;
        n++;
        p1 = (struct student*) malloc(LEN);
        p1->next = NULL;
        scanf("%s", p1->num);
        p1->numb = atoi(p1->num);
        if (p1->numb == 0)
            break;
        scanf("%s %s %f %f %f", p1->name, p1->sex, &p1->score1, &p1->score2,
                &p1->score3);

    }
    printf("\n\n            输入的数据\n");
    print(head);
    for(p1=head;;p1=p1->next)
    {   float a ;
        a=p1->score1+p1->score2+p1->score3;
        if(a>=240)
            printf("\n%s %s %.1f\n",p1->num,p1->name,a/3);
        if(p1->next==NULL)
            break;
    }
    p0 = (struct student*) malloc(LEN);
    p0->next = NULL;
    printf("\n输入插入的学生信息\n");
    scanf("%s %s %s %f %f %f", p0->num, p0->name, p0->sex, &p0->score1,
            &p0->score2, &p0->score3);
    p0->numb = atoi(p0->num);
    for (p1 = head;; p1 = p1->next) {
        if (p1->next != NULL && p1->numb < p0->numb
                && p1->next->numb > p0->numb) {
            p0->next = p1->next;
            p1->next = p0;
            break;
        }
        if (p1->next == NULL && p1->numb < p0->numb) {
            p0 = p1->next;
            p0->next = NULL;
            break;
        }
        if (head->numb > p0->numb) {
            p0->next = head;
            p0 = head;
            break;
        }
    }
    printf("\n\n            插入后\n");
    print(head);
    for (p2 = p1 = head;; p2 = p1, p1 = p1->next) {
        if (head->score2 < 70)
             head = head->next;
        if (p1->score2 < 70 && p1->next != NULL)
             p2->next = p1->next;
        if (p1->score2 < 70 && p1->next == NULL)
             p2->next = NULL;
        if(p1->next==NULL)
            break;
    }
    printf("\n\n             删除后\n");
    print(head);
    return 0;
}
void print(struct student*head) {
    struct student*p1;
    p1 = head;
    if (p1 != NULL)
        do {
            printf("%s %s %s %.1f %.1f %.1f\n", p1->num, p1->name, p1->sex,
                    p1->score1, p1->score2, p1->score3);
            p1 = p1->next;
        } while (p1 != NULL);
}
2011-12-05 14:45
wo841324199
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2011-12-7
得分:0 
回复 4楼 吴小杰
#include <stdio.h>
#include <malloc.h>
#define LEN sizeof(struct student)
struct student {
    char num[15];
    char name[20];
    char sex[6];
    float score1;
    float score2;
    float score3;
    struct student*next;
};
int main() {
    void print(struct student*head);//1   1到2 没问题了
    int n = 1;
    struct student *head, *p1, *p2,*p0;
    head = p1 = p2 = (struct student*) malloc(LEN);
    p1->next = NULL;//但是这里为什么要加这句呢//头指针啊!但头指针不是只存第一个结点的地址吗?怎么存在数据域啊!
    scanf("%s", p1->num);
    scanf("%s %s %f %f %f", p1->name, p1->sex, &p1->score1, &p1->score2,
            &p1->score3);
    for (;;) {
        if (n > 1)
            p2->next = p1;
        p2 = p1;
        n++;
        p1 = (struct student*) malloc(LEN);
        p1->next=NULL;//还有这句   这是我听了他们的建议加的//尾结点啊!
        scanf("%s", p1->num);
        int a;
        a = atoi(p1->num);
        if (a == 0)
            break;
        scanf("%s %s %f %f %f", p1->name, p1->sex, &p1->score1, &p1->score2,
                &p1->score3);

    }
   print(head);//2
   p0=(struct student*) malloc(LEN);//重新开一个空间给要插入的p0
   printf("输入插入的学生信息\n");
    scanf("%s %s %s %f %f %f",p0->num, p0->name, p0->sex, &p0->score1, &p0->score2,
                &p0->score3);
    p1=head;
    for(;;p0=p0->next)//让p0后移//好像不对吧!是让链表中的结点向后移啊!设两个指针p1,p2一个用来计数,一个用来后移;
    {if(p1->next!=NULL&&p1->num<p0->num&&p1->next->num>p0->num)
        {p0->next=p1->next;
         p1->next=p0;
         break;}//插入节点到链表中间
    if(p1->next==NULL&&p1->num<p0->num)
    { p0=p1->next;
       p0->next=NULL;
    break;}//插入节点到链表的尾端
    if(head->num>p0->num)
        {p0->next=head;
        p0=head;
        break;}//头结点
    }
    print(head);

    for(p2=head,p1=head;;p2=p1,p1=p1->next)//3
    {if(head->score2<70)
        head=head->next;//删除头结点
    if(p1->score2<70&&p1->next!=NULL)
        p2->next=p1->next;//删除链表中间的节点
    if(p1->score2<70&&p1->next==NULL)
        p2->next=NULL;//删除最后一个节点
    }
    print(head);

    return 0;
}                                                                 接受头结点
void print(struct student*head)//4    这里没问题      void print(struct student*head)这句帮我解释下啊
{    struct student*p1;
    p1 = head;
    if (p1 != NULL)
        do {
            printf("%s %s %s %.1f %.1f %.1f\n", p1->num, p1->name, p1->sex,
                    p1->score1, p1->score2, p1->score3);
            p1 = p1->next;
        } while (p1 != NULL);
}
2011-12-07 11:25



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




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

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