| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 353 人关注过本帖
标题:关于链表插入的困惑
只看楼主 加入收藏
a861268448
Rank: 1
等 级:新手上路
帖 子:47
专家分:0
注 册:2014-9-15
结帖率:66.67%
收藏
 问题点数:0 回复次数:0 
关于链表插入的困惑
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct student
{
    int num;
    int score;
    struct student * next;
};
typedef struct student STU;
STU *create(void)
{
    STU*head=NULL;
    STU*tail=NULL;
    head=(STU*)malloc(sizeof(STU));
    if(head==NULL)
    {
        printf("头结点内存申请失败");//什么情况会失败
        return NULL;//不理解
    }
    head->next=NULL;
    tail=head;
    STU*pnewelement=NULL;
    int n,s;
    while(1)
    {
        printf("请输入学号和成绩\n");
        scanf("%d%d",&n,&s);
        if(n>0 &&s>0)
        {
            pnewelement=(STU*)malloc(sizeof(STU));
            if(pnewelement==NULL){
                printf("头结点内存申请失败");
                return NULL;
            }
            pnewelement->num=n;
            pnewelement->score=s;
            pnewelement->next=NULL;
            tail->next=pnewelement;
            tail=pnewelement;
        }
        else
            break;
    }
    pnewelement=head;
    head=head->next;
    free(pnewelement);
    return head;
}
void disp(STU *head)
{
    STU*p=head;
    while(1)
    {
        if(p==NULL)
        return;
        printf("(学号:%d,成绩:%d)\n",p->num,p->score);
        p=p->next;
    }
}
STU*insert(STU*head)
{
    int n,s;
    STU*einsert=NULL,*E1=NULL,*E2=NULL;
    E1=head;
    while(1)
    {
        printf("请输入待插入同学的学号和成绩\n");
        scanf("%d%d",&n,&s);
        if(n>0 &&s>0)
        {
            einsert=(STU*)malloc(sizeof(STU));
            if(einsert==NULL)
            {
                printf("结点内存申请失败");
                return NULL;
            }
            einsert->num=n;
            einsert->score=s;
            while(n>E1->num &&E1->next!=NULL)
            {
                E2=E1;//这个是什么意思!!??
                E1=E1->next;//不理解这里
            }

            if(n<=E1->num)
            {
                if(head==E1)
                {
                    einsert->next=E1;
                    head=einsert;
                }
                else
                {
                    E2->next=einsert;
                    einsert->next=E1;
                }   
            }
            else
            {
                E1->next=einsert;
                einsert->next=NULL;
            }
        }
        else
             break;
    }
    return head;
}
void main(void)
{
    STU* head=NULL;
    head=create();
    printf("--------------------\n");
    disp(head);
    printf("--------------------\n");
    head=insert(head);
    printf("--------------------\n");
    disp(head);
    getch();
}

红色中的那个怎么理解
搜索更多相关主题的帖子: include return create 
2015-03-16 18:55
快速回复:关于链表插入的困惑
数据加载中...
 
   



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

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