| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 412 人关注过本帖
标题:链表输入的时候出现问题了。怎么修改,是为了帮一个链表排序的,输入系数和 ...
只看楼主 加入收藏
NeQhk
Rank: 2
等 级:论坛游民
威 望:1
帖 子:164
专家分:85
注 册:2014-7-19
结帖率:69.23%
收藏
已结贴  问题点数:2 回复次数:5 
链表输入的时候出现问题了。怎么修改,是为了帮一个链表排序的,输入系数和指数
#include<stdio.h>
#include<malloc.h>
typedef struct pnode{
    double coef;
    int exp;
    struct pnode *next;
}linklist;

void print(linklist *L)
{
    linklist *p=L->next;
    while(p!=NULL)
    {
        if(p->coef==0)
            ;
        else if(p->exp==0)
            printf("%lf 0",p->coef);
        else
            printf("%lf %d",p->coef,p->exp);
        p=p->next;
    }
}

void destroylist(linklist *L)
{
    linklist *p=L,*q=L->next;
      while(q!=NULL)
      {
            free(p);
            p=q;
            q=p->next;
      }
}

void creat(linklist *L)
{
     L=(linklist *)malloc(sizeof(linklist));
     L->next=NULL;
}


int  Insert (linklist * L,double coef,int exp)
 {
     linklist *p,*s;
     p=L;
     s=(linklist *)malloc( sizeof (linklist));
     s->next = NULL;

     if (!s)
         return 0;
     s->coef=coef;
     s->exp=exp;
     s->next=p->next;
     p->next=s;
     return 1;
 }
void sort(linklist *&head)
{
    linklist *p=head->next,*q,*r;
    if(p!=NULL)
    {
        r=p->next;
        p->next=NULL;//构造只含有一个结点的有序表
        p=r;
        while(p!=NULL)
        {
             r=p->next;//r保存*P结点后继结点的指针
             q=head;
             while(q->next!=NULL && q->next->exp>p->exp)
                 q=q->next;  //在有序表中找插入*P的前驱结点*q
             //将*p插入到*q之后
             p->next=q->next;
             q->next=p;
             p=r;
        }
    }
}

int main()
{
     linklist *ha=NULL;
     int ch;
     creat(ha);
     
     while((ch=scanf_s("%lf %d",&ha->coef,&ha->exp))==2 && ha->exp > 0 )
     {
         Insert(ha,ha->coef,ha->exp);
     }
     print(ha);
     return 0;
}
搜索更多相关主题的帖子: include double 
2015-03-25 11:02
n0noper
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:39
专家分:159
注 册:2015-3-21
收藏
得分:2 
https://bbs.bccn.net/thread-443011-1-1.html###
仔细看!
别针对一类程序错误重复犯,理解了原理,再写就不会错了。

不骄不躁,不卑不亢;虚怀若谷,宠辱不惊。
2015-03-25 12:05
NeQhk
Rank: 2
等 级:论坛游民
威 望:1
帖 子:164
专家分:85
注 册:2014-7-19
收藏
得分:0 
按照你说的修改了,
main函数里定义了
linklist L
后面函数引用都用&L
因为是结构体,引用结构体成员用了L.coef  L.exp
不过程序还是不信
2015-03-25 13:34
NeQhk
Rank: 2
等 级:论坛游民
威 望:1
帖 子:164
专家分:85
注 册:2014-7-19
收藏
得分:0 
#include<stdio.h>
#include<malloc.h>
#define maxsize 30
typedef struct{
    double coef;
    int exp;
}PolyArray[maxsize];
typedef struct node{
    double coef;
    int exp;
    struct node *next;
}linklist;

void print(linklist *L)
{
    linklist *p=L->next;
    while(p!=NULL)
    {
        if(p->coef==0)
            ;
        else if(p->exp==0)
            printf("%lf 0",p->coef);
        else
            printf("%lf %d",p->coef,p->exp);
        p=p->next;
    }
}

void destroylist(linklist *L)
{
    linklist *p=L,*q=L->next;
      while(q!=NULL)
      {
            free(p);
            p=q;
            q=p->next;
      }
}

void creat(linklist *L)
{
     L=(linklist *)malloc(sizeof(linklist));
     L->next=NULL;
}


int  Insert (linklist * L,double coef,int exp)
 {
     linklist *p,*s;
     p=L;
     s=(linklist *)malloc( sizeof (linklist));
     s->next = NULL;

     if (!s)
         return 0;
     s->coef=coef;
     s->exp=exp;
     s->next=p->next;
     p->next=s;
     return 1;
 }
void sort(linklist *head)
{
    linklist *p=head->next,*q,*r;
    if(p!=NULL)
    {
        r=p->next;
        p->next=NULL;//构造只含有一个结点的有序表
        p=r;
        while(p!=NULL)
        {
             r=p->next;//r保存*P结点后继结点的指针
             q=head;
             while(q->next!=NULL && q->next->exp>p->exp)
                 q=q->next;  //在有序表中找插入*P的前驱结点*q
             //将*p插入到*q之后
             p->next=q->next;
             q->next=p;
             p=r;
        }
    }
}

int main()
{
     linklist ha;
     int ch;
     creat(&ha);
     
     while((ch=scanf("%lf %d",&ha.coef,&ha.exp))==2 && ha.exp > 0 )
     {
         Insert(&ha,ha.coef,ha.exp);
     }
     sort(&ha);
     return 0;
}
2015-03-25 13:34
n0noper
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:39
专家分:159
注 册:2015-3-21
收藏
得分:0 
程序代码:
void sort(linklist *head)
{
    linklist *p=head->next,*q,*r;    //p现在指向head的第一个节点(头结点不算)
    if(p!=NULL)
    {
        r=p->next;
        p->next=NULL;//构造只含有一个结点的有序表
        p=r;        //经过上边三部,p竟然指向了head的第二个节点?···我不懂您的意思,你写程序前,先构思(建议用纸笔画一下,吾等菜鸟就是这么学的)
        while(p!=NULL)
        {
            r=p->next;//r保存*P结点后继结点的指针
            q=head;
            while(q->next!=NULL && q->next->exp>p->exp)
                q=q->next;  //在有序表中找插入*P的前驱结点*q
            //将*p插入到*q之后
            p->next=q->next;
            q->next=p;
            p=r;
        }
    }
}

还有,你输入的时候,scanf用法,希望你了解他的返回值是你想要的。

不骄不躁,不卑不亢;虚怀若谷,宠辱不惊。
2015-03-25 21:54
n0noper
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:39
专家分:159
注 册:2015-3-21
收藏
得分:0 
对不起,楼主,是我见少识窄。原谅我啊,因为scanf返回值没深入讨论过不太了解,错怪你了,非常抱歉哈。

不骄不躁,不卑不亢;虚怀若谷,宠辱不惊。
2015-03-25 22:02
快速回复:链表输入的时候出现问题了。怎么修改,是为了帮一个链表排序的,输入系 ...
数据加载中...
 
   



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

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