| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1602 人关注过本帖
标题:新手 顺序表合并 可能是 GetElem函数出错了 最后Lc乱码 谢谢
只看楼主 加入收藏
凉粉呵呵
Rank: 1
等 级:新手上路
帖 子:73
专家分:0
注 册:2013-2-10
收藏
得分:0 
回复 9楼 azzbcc
/* c1.h (程序名) */
 #include<string.h>
 #include<ctype.h>
 #include<malloc.h> /* malloc()等 */
 #include<limits.h> /* INT_MAX等 */
 #include<stdio.h> /* EOF(=^Z或F6),NULL */
 #include<stdlib.h> /* atoi() */
 #include<io.h> /* eof() */
 #include<math.h> /* floor(),ceil(),abs() */
 #include<process.h> /* exit() */
 /* 函数结果状态代码 */
 #define TRUE 1
 #define FALSE 0
 #define OK 1
 #define ERROR 0
 #define INFEASIBLE -1
 /* #define OVERFLOW -2 因为在math.h中已定义OVERFLOW的值为3,故去掉此行 */
typedef int ElemType;
 typedef int Status;/* Status是函数的类型,其值是函数结果状态代码,如OK等 */
 typedef int Boolean ;/* Boolean是布尔类型,其值是TRUE或FALSE */



/* c2-1.h 线性表的动态分配顺序存储结构 */
 #define LIST_INIT_SIZE 10 /* 线性表存储空间的初始分配量 */
 #define LISTINCREMENT 2 /* 线性表存储空间的分配增量 */
 typedef struct
 {
   ElemType *elem; /* 存储空间基址 */
   int length; /* 当前长度 */
   int listsize; /* 当前分配的存储容量(以sizeof(ElemType)为单位) */
 }SqList;
 


 Status InitList(SqList &L) /* 算法2.3 */
 { /* 操作结果:构造一个空的顺序线性表 */
   L.elem=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));
   if(!L.elem)
     exit(OVERFLOW); /* 存储分配失败 */
   L.length=0; /* 空表长度为0 */
   L.listsize=LIST_INIT_SIZE; /* 初始存储容量 */
   return OK;
 }





 int ListLength(SqList L)
 { /* 初始条件:顺序线性表L已存在。操作结果:返回L中数据元素个数 */
   return L.length;
 }

 Status GetElem(SqList L,int i,ElemType *e)
 { /* 初始条件:顺序线性表L已存在,1≤i≤ListLength(L) */
   /* 操作结果:用e返回L中第i个数据元素的值 */
   if(i<1||i>L.length)
     exit(ERROR);
   *e=*(L.elem+i-1);
   return OK;
 }



 Status ListInsert(SqList &L,int i,ElemType e) /* 算法2.4 */
 {
   ElemType *newbase,*q;
   if(L.length>=L.listsize) /* 当前存储空间已满,增加分配 */
   {
     newbase=(ElemType *)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(ElemType));
     if(!newbase)
       exit(OVERFLOW); /* 存储分配失败 */
     L.elem=newbase; /* 新基址 */
     L.listsize+=LISTINCREMENT; /* 增加存储容量 */
   }
   q=L.elem+i-1; /* q为插入位置 */
  
   *q=e; /* 插入e */
   ++L.length; /* 表长增1 */
   return OK;
 }
 void paixu(SqList La,SqList Lb,SqList &Lc)
 {
    int i,j,k,La_len,Lb_len,a,b;
    InitList(Lc);
    i=j=1;k=0;
    La_len=ListLength(La);
    Lb_len=ListLength(Lb);
    while((i<=La_len)&&(j<=Lb_len))
    {   
        GetElem(La,i,&a);
        GetElem(Lb,j,&b);
        if(a<=b){ListInsert(Lc,++k,a);++i;}
        else {ListInsert(Lc,++k,b);++j;}

    }
    while(i<=La_len)
    {
        GetElem(La,i++,&a);
        ListInsert(Lc,++k,a);
    }
    while(j<=Lb_len)
    {
        GetElem(Lb,j++,&b);
        ListInsert(Lc,++k,b);
    }
}







Status Load(SqList &L)
{  int n,i;
     n=ListLength(L);
     for(i=0;i<n;i++)
         printf("%d ",*(L.elem+i-1));
     printf("\n");
     return OK;
 }


 
int main()
{    int n,i,m,e,a,Lb_len,La_len,Lc_len;
    SqList La,Lb,Lc;
    InitList(La);
    InitList(Lb);
    InitList(Lc);
    scanf("%d",&n);                    //n表示La输入的数组元素
    i=0;
    while(n--){
   
    scanf("%d",&e);
    ListInsert(La,i,e);
    i++;
    }
    Load(La);


    scanf("%d",&m);                    //m表示Lb输入的数组元素
    i=0;
    while(m--){
    scanf("%d",&a);
   
    ListInsert(Lb,i,a);
    i++;
    }
    Load(Lb);

    //printf("%d\n",La_len=ListLength(La));
    //printf("%d\n",Lb_len=ListLength(Lb));
   
    paixu(La,Lb,Lc);
    //printf("%d\n",Lc_len=ListLength(Lc));
   
    Load(Lc);

     

    return 0;
}
改了又改,还是有乱码的地方,求指正,谢谢~
2013-03-20 13:42
azzbcc
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:江西财经大学
等 级:贵宾
威 望:81
帖 子:3293
专家分:12919
注 册:2012-11-4
收藏
得分:0 
回复 11楼 凉粉呵呵
你的ListInsert函数中 i的条件没写清楚,i能否等于 0?

初始化 La 和 Lb的时候,第一个 i都是 0

还有就是你的 Lc在 paixu函数中又 Init一次,这样会有问题的


[fly]存在即是合理[/fly]
2013-03-20 14:00
凉粉呵呵
Rank: 1
等 级:新手上路
帖 子:73
专家分:0
注 册:2013-2-10
收藏
得分:0 
回复 12楼 azzbcc
受教了  谢谢
2013-03-22 23:31
快速回复:新手 顺序表合并 可能是 GetElem函数出错了 最后Lc乱码 谢谢
数据加载中...
 
   



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

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