| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 563 人关注过本帖
标题:编写函数删除链表中的结点,函数功能实现不了
取消只看楼主 加入收藏
mosquiyan
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2014-12-12
结帖率:85.71%
收藏
已结贴  问题点数:15 回复次数:1 
编写函数删除链表中的结点,函数功能实现不了
编写insert函数,实现对上述链表结点的插入操作,
编写delete函数,实现通过学号删除结点的操作,编写clear函数,实现对链表的清空操作。
#include "stdio.h"
#include "stdlib.h"
#define STUDENT struct student
#define LEN sizeof(STUDENT)
#define N 3
STUDENT
{
    int num;
    char name[20];
    float score[3];
    STUDENT *next;
};
float avg[N]={0.0};
float *average(STUDENT *head)
{
    STUDENT *p;
    int i=0;
    p=head;
    while(p!=NULL)
    {
        avg[i]=(p->score[0]+p->score[1]+p->score[2])/3;
        p=p->next;
        i++;
    }
    return avg;
}

STUDENT *create(int n)
{
    STUDENT *head,*last,*p;
    int i,j;
    head=last=NULL;
    for(i=0;i<n;i++)
    {
        p=(STUDENT *)malloc(LEN);
        printf("\nplease input%d student's number,name:",i+1);
        scanf("%d,%s",&p->num,&p->name);
        printf("\nplease input score1 score2 score3:");
        for(j=0;j<3;j++)
        scanf("%f",&p->score[j]);
        
        p->next=NULL;
        if(i==0)
          head=last=p;
        else
        {
            last->next=p;
            last=p;
        }
    }
    return head;     
}


void output(STUDENT *head)
{
    STUDENT *p;
    int i=0;
    average(head);
    p=head;
    printf("\nnumber\tname\tscore1\tscore2\tscore3\taverage");
    while(p!=NULL)
    {   
        printf("\n%d\t%-s\t%.1f\t%.1f\t%.1f\t%.2f\n",p->num,p->name,p->score[0],p->score[1],p->score[2],avg[i]);
        p=p->next;
        i++;
    }
}

STUDENT *insert(STUDENT *head,STUDENT *p)
{
    STUDENT *p1,*p2;
    p2=head;
    if(head==NULL)//没有插入点
    {
        p->next=NULL;
        head=p;
    }
    else//有插入点
    {
        while( p2!=NULL&&(p->num > p2->num))//查找插入点
        {
            p1=p2;
            p2=p2->next;
        }
        if(p2==head)//插入点为第一个结点的位置
        {
            p->next=head;
            head=p;
        }
        else//位于中间或末尾
        {
            p->next=p2;
            p1->next=p;
        }     
    }
    return head;
}

STUDENT *delete(STUDENT *head,int num)
{
    STUDENT *p1,*p2;
    p2=head;
    if(head==NULL)//如果链表为空
    {
        printf("\nList is null\n");
        return head;
    }
    p2=head;
    while(num!=p2->num && p2->next!=NULL)//查找删除结点
    {
        p1=p2;
        p2=p2->next;
    }
    if(p2->next!=NULL)//如果不是链表的末尾,即找到了删除结点p2
    {
        if(p2==head)//删除的是第一个结点
           head=p2->next;
        else
           p1->next=p2->next;
        free(p2);
        printf("\ndelete %d successfully",num);
    }
    else
        printf("%d can't be found!",num);
    return head;
}

void clear(STUDENT *head)
{
    STUDENT *p,*p1;
    p=head;
    while(p!=NULL)
    {
        p1=p;
        p=p->next;
        free(p1);
    }
}
main()
{
    STUDENT *head;
    STUDENT *p;
    p=(STUDENT *)malloc(LEN);
    int n,i;
    char c;
    do
    {
        printf("1.input student information\n");
        printf("2.insert student information\n");
        printf("3.delete student information\n");
        printf("4.clear the information\n");
        printf("5.quit\n");
        printf("please choose");
        c=getchar();
        getchar();
        if(c=='1')
        {
            printf("please input the amount of the students:");
            scanf("%d",&n);
            getchar();
            head=create(n);
            output(head);
        }
        else if(c=='2')
        {
            printf("please input the student's number,name:");
            scanf("%d,%s",&p->num,&p->name);
            printf("please input score1 score2 score3:");
            for(i=0;i<3;i++)
            scanf("%f",&p->score[i]);
            insert(head,p);
            printf("the new information is:");
            output(head);   
        }
        else if(c=='3')
        {
            printf("please input the number of the student you want to delete:");
            scanf("%d",n);
            delete(head,n);
            printf("the new information is:");
            output(head);   
        }
        else if(c=='4')
        {
            clear(head);
            //output(head);
        }
        else if(c=='5')
        break;
        getchar();   
    }while(c!='5');
    printf("thank you for using this system!\n");   
}

不知道为什么delete函数老是实现不了,而且运行时无法重复实现insert的功能,不知道哪里出问题了,请大家指点一下O(∩_∩)O
搜索更多相关主题的帖子: average include insert 
2014-12-13 17:18
mosquiyan
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2014-12-12
收藏
得分:0 
我都是按照课本上的做法来写的里面具体的步骤也不是很理解
2014-12-13 22:58
快速回复:编写函数删除链表中的结点,函数功能实现不了
数据加载中...
 
   



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

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