标题:链表答案错误,编程过程没有出现错误
取消只看楼主
鱼游海底
Rank: 1
来 自:中国
等 级:新手上路
帖 子:132
专家分:2
注 册:2015-4-16
结帖率:57.14%
 问题点数:0 回复次数:1 
链表答案错误,编程过程没有出现错误
/*指针实验2_20题 填空*/
#include<stdio.h>
#include<stdlib.h>  
#define LEN sizeof(struct member)
struct member
{   int num;      
    struct member *next;
};  /*建立结构体*/
typedef struct member Member;
/*插入一个新的结点*/  
void insertup(Member *head, Member *newp)
{     
    Member *pre,*suc;   
    pre=head;   
    suc=head->next;   
    while (suc!=NULL)   
    {     
        if (suc->num>=newp->num)   
        {
            pre->next=newp;
            newp->next=suc;
        }
        pre=suc;   
        suc=suc->next;   
    }      
    newp->next=suc;            
    pre->next=newp;     
}   
/*从小到大建立一个动态链表*/
struct member *creat()   
{     
    Member *head,*p1,*p2;   
    int n=0;     
    p1=p2=(Member *)malloc(LEN);   
    scanf("%d",&p1->num);   
    head=NULL;     
    while(p1->num!=0)   
    {      
        n=n+1;      
        if(n==1)
            head=p1;     
        else
            p2->next=p1;     
        p2=p1;      
        p1=(Member *)malloc(LEN);     
        scanf("%d",&p1->num);   
    }     
    p2->next=NULL;   
    return (head);  
}   
/*输出链表*/
void print(Member *head)   
{     
    Member *p;     
    printf("排序后输出为:\n");   
    p=head;     
    if(head!=NULL)     
        do{      
            printf("%5d",p->num);      
            p=p->next;     
        }while(p!=NULL);     
        printf("\n");   
}  
void main()   
{     
   struct member a;
   struct member *head,*newp;
   newp=&a;
   printf("请输入一组按升序排列的数,以0作为结束的标志:\n");
   head=creat();
   printf("输入一个要插入的结点:\n");
   scanf("%d",&newp->num);
   insertup(head,newp);
   print(head);
}
搜索更多相关主题的帖子: 结构体 member include 
2015-06-04 08:58
鱼游海底
Rank: 1
来 自:中国
等 级:新手上路
帖 子:132
专家分:2
注 册:2015-4-16
得分:0 
我找到错误了,哈哈!差个break;
2015-06-04 09:01



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




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

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