| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 428 人关注过本帖
标题:链表删除出错,报错原因 好像类型转换出错,但是定义的是同一类型呀,求大神 ...
只看楼主 加入收藏
风车转风车89
Rank: 2
等 级:论坛游民
帖 子:125
专家分:45
注 册:2014-9-15
结帖率:81.82%
收藏
已结贴  问题点数:20 回复次数:3 
链表删除出错,报错原因 好像类型转换出错,但是定义的是同一类型呀,求大神指点
程序代码:
出错在删除链表那一行main函数中的这一句错误head=del(head,del_num);错误原因error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'struct student' (or there is no acceptable conversion)

#include<stdio.h>
#include<malloc.h>
#define NULL 0
struct student
{
    int num;
    char name[10];
    float score[3];
    struct student *next;
};
typedef struct student stu;
void main()
{
    stu *creat(int a);//创建节点数为a的链表;
    stu *insert(stu *head,stu *add);//head为原链表头指针,add为指向待插入已知链表指针;
    void print(stu *head);//输出链表
    stu del(stu *head,int element);//删除链表
    stu *head,*p;int n=2,del_num=1;
    head=creat(n);
    print(head);
    p=(stu*)malloc(sizeof(stu));
    printf("******输入学生信息*********\n");
    scanf("%d%s%f%f%f",&p->num,p->name,&p->score[0],&p->score[1],&p->score[2]);
    head=insert(head,p);
    print(head);
    head=del(head,del_num);
    print(head);
}
stu *creat(int a)//创建节点数为a的链表;
{
    stu *head,*p,*q;int n=0;
    p=q=(stu*)malloc(sizeof(stu));
    printf("******输入学生信息*********\n");
    scanf("%d%s%f%f%f",&p->num,p->name,&p->score[0],&p->score[1],&p->score[2]);
    head=NULL;
    while(n<a)
    {
        n++;
        if(n==1) head=p;
        else q->next=p;
        q=p;
        p=(stu*)malloc(sizeof(stu));
        printf("******输入学生信息*********\n");
        scanf("%d%s%f%f%f",&p->num,p->name,&p->score[0],&p->score[1],&p->score[2]);
    }
    q->next=NULL;
    return head;
}
stu *insert(stu *head,stu *add)//head为原链表头指针,add为指向待插入已知链表指针;
{
    stu *p,*q,*p0;
    p=q=head;p0=add;
    if(p==NULL)
    {
        head=p0;p0->next=NULL;
    }
    else
    {
        while(p->num<p0->num&&p->next!=NULL)
        {
            q=p;p=p->next;
        }
        if(p0->num<=p->num)
        {

            if(p==head)
            {head=p0;p0->next=p;}
            else
            {

                if(p->next==NULL)
                {p->next=p0;p0->next=NULL;}
                else
                {q->next=p0;p0->next=p;}
            }
        }
    }
    return head;
}

void print(stu *head)//输出链表
{
    stu *p;
    p=head;
    printf("******输出链表*********\n");
    while(p!=NULL)
    {

        printf("   num=%d  name=%s  score[0]=%f  score[1]=%f  score[2]=%f  next=%d\n",p->num,p->name,p->score[0],p->score[1],p->score[2],p->next);
        p=p->next;

    }
    printf("******输出结束*********\n");
}
stu *del(stu *head,int element)//删除链表
{
    stu *p,*q;
    if(head==NULL)
        printf("   链表不纯在     \n");
    p=q=head;
    while(p->num!=element&&p!=NULL)
    {q=p;p=p->next;}
    if(p->num==element)
    {
        if(p==head)head=p->next;
        else q->next=p->next;
    }
    else printf(" not be found the num  \n");
    return head;   

}
搜索更多相关主题的帖子: conversion include 
2014-10-07 12:54
embed_xuel
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:58
帖 子:3845
专家分:11385
注 册:2011-9-13
收藏
得分:20 
有什么好说的,楼主仔细看看函数声明那块

总有那身价贱的人给作业贴回复完整的代码
2014-10-07 13:08
风车转风车89
Rank: 2
等 级:论坛游民
帖 子:125
专家分:45
注 册:2014-9-15
收藏
得分:0 
回复 2 楼 embed_xuel
哎呀 大神呀  我一直以为函数定义错了呢!谢谢
2014-10-07 13:16
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:0 
自己写的代码怎么会一直看不出问题何在

授人以渔,不授人以鱼。
2014-10-07 19:05
快速回复:链表删除出错,报错原因 好像类型转换出错,但是定义的是同一类型呀, ...
数据加载中...
 
   



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

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