| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 818 人关注过本帖
标题:这段程序错在哪里?我想把一个结构体插入链表
只看楼主 加入收藏
进阶兽
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2016-11-20
结帖率:100%
收藏
已结贴  问题点数:10 回复次数:2 
这段程序错在哪里?我想把一个结构体插入链表
#include<stdio.h>
#include<stdlib.h>
struct student
{
    int num;
    char name[20];
    struct student* next;
};
int main()
{
    struct student* head,*p,*ptr;
    struct student* creat();
    struct student m={4,"chenqi"};
    struct student* insert(struct student m,struct student* p,int n);
    int n=0;
    ptr=creat();
    insert(m,ptr,n);
    ptr=insert(m,ptr,n);
    while(ptr!=NULL)
    {
        printf("%d %s %p\n",ptr->num ,ptr->name ,ptr->next );
        ptr=ptr->next;
    }
    return 0;
}
struct student* creat()
{
    struct student *p1,*p2,*head;
    head=NULL;
    p1=p2=(struct student*)malloc(sizeof(struct student));
    int n=0;
    scanf("%d %s",&p1->num,&p1->name);
    while(p1->num!=0)
    {
        n=n+1;
        if(n==1)head=p1;
        else p2->next=p1;
        p2=p1;
        p1=(struct student*)malloc(sizeof(struct student));
        scanf("%d %s",&p1->num,&p1->name);
    }
    p2->next=NULL;
    return head;
}
struct student *insert(struct student m,struct student *p,int n)
{
    int i;
    struct student *p1,*p2,*p3;
    p3=p;
    p1=&m;
    for(i=0;i<n;i++)
    p3=p3->next ;
    p2=p3->next ;
    p3->next =p1;
    p1->next=p2;
    return p;
}
搜索更多相关主题的帖子: include insert 结构体 
2016-11-20 21:23
九转星河
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:长长久久
等 级:贵宾
威 望:52
帖 子:5023
专家分:14003
注 册:2016-10-22
收藏
得分:10 
虽然写得不是很规范,而且有几个没有用的变量没删,但主要问题是函数传入地址问题。
就是函数传入参数能不能改变主函数里参数的值的问题~帮你修改了很多了,至于插入,留在了尾部,这个可以改。
程序代码:
#include<stdio.h>
#include<stdlib.h>
struct student
{
    int num;
    char name[20];
    struct student* next;
};
int n=0;//n最好用全局变量
int main()
{
    struct student *ptr;//多余的变量去掉
    struct student* creat();
    struct student m={4,"chenqi",NULL};
    void insert(struct student *m,struct student* p);//不用加返回值,用void就行了
    ptr=creat();
    insert(&m,ptr);//注意,传入的是m的地址,不是m~
    while(ptr!=NULL)
    {
        printf("%d %s %p\n",ptr->num ,ptr->name ,ptr->next );
        ptr=ptr->next;
    }
    return 0;
}
struct student* creat()
{
    struct student *p1,*p2,*head;
    head=NULL;
    p1=p2=(struct student*)malloc(sizeof(struct student));
    scanf("%d%s",&p1->num,&p1->name);
    while(p1->num!=0)
    {
        n=n+1;
        if(n==1)head=p1;
        else p2->next=p1;
        p2=p1;
        p1=(struct student*)malloc(sizeof(struct student));
        scanf("%d%s",&p1->num,&p1->name);
    }
    p2->next=NULL;
    return head;
}
void insert(struct student *m,struct student *p)
{
    int i;
    struct student *p3;
    p3=p;
    for(i=0;i<n-1;i++)
        p3=p3->next;
    p3->next =m;
    m->next=NULL;
}

[code]/*~个性签名:bug是什么意思?bug是看上去没有可能的东西实际上是有可能做到的 就是这样~2018-08-08更~*/[/code]
2016-11-20 23:16
进阶兽
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2016-11-20
收藏
得分:0 
回复 2楼 九转星河
插入别的位置改哪几个地方呢?我自己试了一下该不出来...先谢谢大佬的耐心解答!


[此贴子已经被作者于2016-11-21 07:49编辑过]

2016-11-21 07:07
快速回复:这段程序错在哪里?我想把一个结构体插入链表
数据加载中...
 
   



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

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