数据结构(C语言)顺序表的合并
#include<stdio.h>#include<malloc.h>#defineok1#defineerror0#defineLIST_INT_SIZE10#defi...
#include<stdio.h>
#include<malloc.h>
#define ok 1
#define error 0
#define LIST_INT_SIZE 10
#define elemtype int
typedef struct{
elemtype *elem;
int length;
int listsize;
}sqlist;
int Init_sqlist(sqlist *la)
{
(*la).elem=(elemtype*)malloc(LIST_INT_SIZE*sizeof(elemtype));
if(!(*la).elem)return error;
(*la).length=0;
(*la).listsize=LIST_INT_SIZE;
return ok;
}
void mergelist_sq(sqlist la,sqlist lb,sqlist &lc)
void main()
{
int i;sqlist la,lb,lc;
Init_sqlist(&la);
Init_sqlist(&lb);
for(i=0;i<5;i++)
scanf("%d",la.elem[i]);
la.length=5;
for(i=0;i<5;i++)
scanf("%d",lb.elem[i]);
lb.length=5;
mergelist_sq(la,lb,lc);
}
void mergelist_sq(sqlist la,sqlist lb,sqlist &lc)
{
int *pa,*pb,*pc,*pa_last,*pb_last,i;
pa=la.elem;pb=lb.elem;
lc.listsize=la.length+lb.length;
pc=lc.elem=(elemtype*)malloc(lc.listsize*sizeof(elemtype));
pa_last=la.elem+la.length-1;
pb_last=lb.elem+lb.length-1;
while(pa<=pa_last&&pb<=pb_last){
if(*pa<=*pb)*pc++=*pa++;
else *pc++=*pb++;}
while(pa<=pa_last) *pc++=*pa++;
while(pb<=pb_last) *pc++=*pb++;
for(i=0;i<lc.listsize-1;i++)
printf("%d,",lc.elem[i]);
}
运行时有错误 帮忙修改一下吧 谢啦 展开
#include<malloc.h>
#define ok 1
#define error 0
#define LIST_INT_SIZE 10
#define elemtype int
typedef struct{
elemtype *elem;
int length;
int listsize;
}sqlist;
int Init_sqlist(sqlist *la)
{
(*la).elem=(elemtype*)malloc(LIST_INT_SIZE*sizeof(elemtype));
if(!(*la).elem)return error;
(*la).length=0;
(*la).listsize=LIST_INT_SIZE;
return ok;
}
void mergelist_sq(sqlist la,sqlist lb,sqlist &lc)
void main()
{
int i;sqlist la,lb,lc;
Init_sqlist(&la);
Init_sqlist(&lb);
for(i=0;i<5;i++)
scanf("%d",la.elem[i]);
la.length=5;
for(i=0;i<5;i++)
scanf("%d",lb.elem[i]);
lb.length=5;
mergelist_sq(la,lb,lc);
}
void mergelist_sq(sqlist la,sqlist lb,sqlist &lc)
{
int *pa,*pb,*pc,*pa_last,*pb_last,i;
pa=la.elem;pb=lb.elem;
lc.listsize=la.length+lb.length;
pc=lc.elem=(elemtype*)malloc(lc.listsize*sizeof(elemtype));
pa_last=la.elem+la.length-1;
pb_last=lb.elem+lb.length-1;
while(pa<=pa_last&&pb<=pb_last){
if(*pa<=*pb)*pc++=*pa++;
else *pc++=*pb++;}
while(pa<=pa_last) *pc++=*pa++;
while(pb<=pb_last) *pc++=*pb++;
for(i=0;i<lc.listsize-1;i++)
printf("%d,",lc.elem[i]);
}
运行时有错误 帮忙修改一下吧 谢啦 展开
5个回答
展开全部
1、c语言是没有引用的,可以使用一个const指针来模拟引用void InitList(SqList *L){L->last=0;}。
2、这个是顺序表的初始化,不用管int LenList(SqList L){return L.last;}。
3、插入操作,这一步是关键,插入顺序表int InsertList(SqList *L,int i,ElemType x){int k;if(L->last>=MAX){printf("表已满,无法插入\n");return 0}else if(i<0 || i>L->last+1){printf("插入位置不合法\n");return 0;}。
4、这是接着上面的第二步,判断的else{for(k=L->last;k>i-1;k--)L->elem[k]=L->elem[k-1];L->elem[i-1]=x;L->last++;return 0;}。
5、canf(%d",&a) 是读取整型变量a在内存中的地址,而scanf("%d",a)是读取整型变量a的值。就完成了。
展开全部
#include<stdio.h>
#define NUM 20 //宏定义
void p1(int x[] , int y[] , int i , int j);
void p2(int x[] , int y[] , int i , int j);
void p3(int x[] , int y[] , int i , int j);
//以上三个是函数声明
void p1(int x[] , int y[] , int i , int j)
{
int m,n,a,b;
int k = 0 ,g = 0;
int c[NUM];
for(m = 0 ; m < i ; m ++ )
{ for(n = 0 ; n < j ; n ++ )
{
if(x[m] == y[n]) {x[m] = 0 ; y[n] = 0;break;} //相同元素赋值0
}
}
for( a = 0 ; a < i ; a++)
{
if( x[a] != 0 ) c[k++] = x[a] ;
else continue;
}
for( b = 0 ; b < j ; b++)
{
if( y[b] != 0 ) c[k++] = y[b] ;
else continue;
}
while(c[g])
{printf("%d ",c[g++]);}
printf("\n");
}
void p2(int x[] , int y[] , int i , int j)
{ int m, n,a,b,q ,temp;
int c[NUM];
for( m = 0 ; m < i ; m++)
{c[m] = x[m];}
for( n = 0 ; n < j ; n ++)
{c[n+m] = y[n];}
for( a = 0 ; a < n ; a++)
for(b = 0 ; b < n ; b ++)
{
if(c[b]>c[b+1]) {temp = c[b] ; c[b] = c[b+1] ; c[b+1] = temp ; }
}
for( q = 0 ; q < n+m ; q ++)
{printf("%d ",c[q]);
}
printf("\n");
}
void p3(int x[] , int y[] , int i , int j)
{int m, n,a,b,q ,temp;
int c[NUM];
for( m = 0 ; m < i ; m++)
{c[m] = x[m];}
for( n = 0 ; n < j ; n ++)
{c[n+m] = y[n];}
for( a = 0 ; a < n ; a++)
for(b = 0 ; b < n ; b ++)
{
if(c[b]<c[b+1]) //判断是否满足,然后两个数调换
{temp = c[b] ; c[b] = c[b+1] ; c[b+1] = temp ; }
}
for( q = 0 ; q < n+m ; q ++)
{printf("%d ",c[q]);
}
}
int main()
{
int a[4]={3,5,8,11};
int b[7]={2,6,8,9,11,15,20};
// p1(a,b,4,7);
p2(a,b,4,7);
// p3(a,b,4,7);
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
程序没什么大的毛病。。只是一些编写代码时的小疏忽
#include<stdio.h>
#include<malloc.h>
#define
ok
1
#define
error
0
#define
LIST_INT_SIZE
10
#define
elemtype
int
typedef
struct{
elemtype
*elem;
int
length;
int
listsize;
}sqlist;
int
Init_sqlist(sqlist
*la)
{
(*la).elem=(elemtype*)malloc(LIST_INT_SIZE*sizeof(elemtype));
if(!(*la).elem)return
error;
(*la).length=0;
(*la).listsize=LIST_INT_SIZE;
return
ok;
}
void
mergelist_sq(sqlist
la,sqlist
lb,sqlist
&lc)
//
忘了加;
void
main()
{
int
i;sqlist
la,lb,lc;
Init_sqlist(&la);
Init_sqlist(&lb);
for(i=0;i<5;i++)
scanf("%d",la.elem[i]);
//
应该改为
scanf(
"%d",&la.elem[i]
);
la.length=5;
for(i=0;i<5;i++)
scanf("%d",lb.elem[i]);
//
同上
lb.length=5;
mergelist_sq(la,lb,lc);
}
void
mergelist_sq(sqlist
la,sqlist
lb,sqlist
&lc)
{
int
*pa,*pb,*pc,*pa_last,*pb_last,i;
pa=la.elem;pb=lb.elem;
lc.listsize=la.length+lb.length;
pc=lc.elem=(elemtype*)malloc(lc.listsize*sizeof(elemtype));
pa_last=la.elem+la.length-1;
pb_last=lb.elem+lb.length-1;
while(pa<=pa_last&&pb<=pb_last){
if(*pa<=*pb)*pc++=*pa++;
else
*pc++=*pb++;}
while(pa<=pa_last)
*pc++=*pa++;
while(pb<=pb_last)
*pc++=*pb++;
for(i=0;i<lc.listsize-1;i++)
//
改为for(
i=0;
i<lc.listsize;
i++
)
printf("%d,",lc.elem[i]);
}
#include<stdio.h>
#include<malloc.h>
#define
ok
1
#define
error
0
#define
LIST_INT_SIZE
10
#define
elemtype
int
typedef
struct{
elemtype
*elem;
int
length;
int
listsize;
}sqlist;
int
Init_sqlist(sqlist
*la)
{
(*la).elem=(elemtype*)malloc(LIST_INT_SIZE*sizeof(elemtype));
if(!(*la).elem)return
error;
(*la).length=0;
(*la).listsize=LIST_INT_SIZE;
return
ok;
}
void
mergelist_sq(sqlist
la,sqlist
lb,sqlist
&lc)
//
忘了加;
void
main()
{
int
i;sqlist
la,lb,lc;
Init_sqlist(&la);
Init_sqlist(&lb);
for(i=0;i<5;i++)
scanf("%d",la.elem[i]);
//
应该改为
scanf(
"%d",&la.elem[i]
);
la.length=5;
for(i=0;i<5;i++)
scanf("%d",lb.elem[i]);
//
同上
lb.length=5;
mergelist_sq(la,lb,lc);
}
void
mergelist_sq(sqlist
la,sqlist
lb,sqlist
&lc)
{
int
*pa,*pb,*pc,*pa_last,*pb_last,i;
pa=la.elem;pb=lb.elem;
lc.listsize=la.length+lb.length;
pc=lc.elem=(elemtype*)malloc(lc.listsize*sizeof(elemtype));
pa_last=la.elem+la.length-1;
pb_last=lb.elem+lb.length-1;
while(pa<=pa_last&&pb<=pb_last){
if(*pa<=*pb)*pc++=*pa++;
else
*pc++=*pb++;}
while(pa<=pa_last)
*pc++=*pa++;
while(pb<=pb_last)
*pc++=*pb++;
for(i=0;i<lc.listsize-1;i++)
//
改为for(
i=0;
i<lc.listsize;
i++
)
printf("%d,",lc.elem[i]);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
程序没什么大的毛病。。只是一些编写代码时的小疏忽
#include<stdio.h>
#include<malloc.h>
#define ok 1
#define error 0
#define LIST_INT_SIZE 10
#define elemtype int
typedef struct{
elemtype *elem;
int length;
int listsize;
}sqlist;
int Init_sqlist(sqlist *la)
{
(*la).elem=(elemtype*)malloc(LIST_INT_SIZE*sizeof(elemtype));
if(!(*la).elem)return error;
(*la).length=0;
(*la).listsize=LIST_INT_SIZE;
return ok;
}
void mergelist_sq(sqlist la,sqlist lb,sqlist &lc) // 忘了加;
void main()
{
int i;sqlist la,lb,lc;
Init_sqlist(&la);
Init_sqlist(&lb);
for(i=0;i<5;i++)
scanf("%d",la.elem[i]); // 应该改为 scanf( "%d",&la.elem[i] );
la.length=5;
for(i=0;i<5;i++)
scanf("%d",lb.elem[i]); // 同上
lb.length=5;
mergelist_sq(la,lb,lc);
}
void mergelist_sq(sqlist la,sqlist lb,sqlist &lc)
{
int *pa,*pb,*pc,*pa_last,*pb_last,i;
pa=la.elem;pb=lb.elem;
lc.listsize=la.length+lb.length;
pc=lc.elem=(elemtype*)malloc(lc.listsize*sizeof(elemtype));
pa_last=la.elem+la.length-1;
pb_last=lb.elem+lb.length-1;
while(pa<=pa_last&&pb<=pb_last){
if(*pa<=*pb)*pc++=*pa++;
else *pc++=*pb++;}
while(pa<=pa_last) *pc++=*pa++;
while(pb<=pb_last) *pc++=*pb++;
for(i=0;i<lc.listsize-1;i++) // 改为for( i=0; i<lc.listsize; i++ )
printf("%d,",lc.elem[i]);
}
#include<stdio.h>
#include<malloc.h>
#define ok 1
#define error 0
#define LIST_INT_SIZE 10
#define elemtype int
typedef struct{
elemtype *elem;
int length;
int listsize;
}sqlist;
int Init_sqlist(sqlist *la)
{
(*la).elem=(elemtype*)malloc(LIST_INT_SIZE*sizeof(elemtype));
if(!(*la).elem)return error;
(*la).length=0;
(*la).listsize=LIST_INT_SIZE;
return ok;
}
void mergelist_sq(sqlist la,sqlist lb,sqlist &lc) // 忘了加;
void main()
{
int i;sqlist la,lb,lc;
Init_sqlist(&la);
Init_sqlist(&lb);
for(i=0;i<5;i++)
scanf("%d",la.elem[i]); // 应该改为 scanf( "%d",&la.elem[i] );
la.length=5;
for(i=0;i<5;i++)
scanf("%d",lb.elem[i]); // 同上
lb.length=5;
mergelist_sq(la,lb,lc);
}
void mergelist_sq(sqlist la,sqlist lb,sqlist &lc)
{
int *pa,*pb,*pc,*pa_last,*pb_last,i;
pa=la.elem;pb=lb.elem;
lc.listsize=la.length+lb.length;
pc=lc.elem=(elemtype*)malloc(lc.listsize*sizeof(elemtype));
pa_last=la.elem+la.length-1;
pb_last=lb.elem+lb.length-1;
while(pa<=pa_last&&pb<=pb_last){
if(*pa<=*pb)*pc++=*pa++;
else *pc++=*pb++;}
while(pa<=pa_last) *pc++=*pa++;
while(pb<=pb_last) *pc++=*pb++;
for(i=0;i<lc.listsize-1;i++) // 改为for( i=0; i<lc.listsize; i++ )
printf("%d,",lc.elem[i]);
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
程序没什么大的毛病。。只是一些编写代码时的小疏忽
#include
#include
#define
ok
1
#define
error
0
#define
LIST_INT_SIZE
10
#define
elemtype
int
typedef
struct{
elemtype
*elem;
int
length;
int
listsize;
}sqlist;
int
Init_sqlist(sqlist
*la)
{
(*la).elem=(elemtype*)malloc(LIST_INT_SIZE*sizeof(elemtype));
if(!(*la).elem)return
error;
(*la).length=0;
(*la).listsize=LIST_INT_SIZE;
return
ok;
}
void
mergelist_sq(sqlist
la,sqlist
lb,sqlist
&lc)
//
忘了加;
void
main()
{
int
i;sqlist
la,lb,lc;
Init_sqlist(&la);
Init_sqlist(&lb);
for(i=0;i<5;i++)
scanf("%d",la.elem[i]);
//
应该改为
scanf(
"%d",&la.elem[i]
);
la.length=5;
for(i=0;i<5;i++)
scanf("%d",lb.elem[i]);
//
同上
lb.length=5;
mergelist_sq(la,lb,lc);
}
void
mergelist_sq(sqlist
la,sqlist
lb,sqlist
&lc)
{
int
*pa,*pb,*pc,*pa_last,*pb_last,i;
pa=la.elem;pb=lb.elem;
lc.listsize=la.length+lb.length;
pc=lc.elem=(elemtype*)malloc(lc.listsize*sizeof(elemtype));
pa_last=la.elem+la.length-1;
pb_last=lb.elem+lb.length-1;
while(pa<=pa_last&&pb<=pb_last){
if(*pa<=*pb)*pc++=*pa++;
else
*pc++=*pb++;}
while(pa<=pa_last)
*pc++=*pa++;
while(pb<=pb_last)
*pc++=*pb++;
for(i=0;i
评论
0
0
加载更多
#include
#include
#define
ok
1
#define
error
0
#define
LIST_INT_SIZE
10
#define
elemtype
int
typedef
struct{
elemtype
*elem;
int
length;
int
listsize;
}sqlist;
int
Init_sqlist(sqlist
*la)
{
(*la).elem=(elemtype*)malloc(LIST_INT_SIZE*sizeof(elemtype));
if(!(*la).elem)return
error;
(*la).length=0;
(*la).listsize=LIST_INT_SIZE;
return
ok;
}
void
mergelist_sq(sqlist
la,sqlist
lb,sqlist
&lc)
//
忘了加;
void
main()
{
int
i;sqlist
la,lb,lc;
Init_sqlist(&la);
Init_sqlist(&lb);
for(i=0;i<5;i++)
scanf("%d",la.elem[i]);
//
应该改为
scanf(
"%d",&la.elem[i]
);
la.length=5;
for(i=0;i<5;i++)
scanf("%d",lb.elem[i]);
//
同上
lb.length=5;
mergelist_sq(la,lb,lc);
}
void
mergelist_sq(sqlist
la,sqlist
lb,sqlist
&lc)
{
int
*pa,*pb,*pc,*pa_last,*pb_last,i;
pa=la.elem;pb=lb.elem;
lc.listsize=la.length+lb.length;
pc=lc.elem=(elemtype*)malloc(lc.listsize*sizeof(elemtype));
pa_last=la.elem+la.length-1;
pb_last=lb.elem+lb.length-1;
while(pa<=pa_last&&pb<=pb_last){
if(*pa<=*pb)*pc++=*pa++;
else
*pc++=*pb++;}
while(pa<=pa_last)
*pc++=*pa++;
while(pb<=pb_last)
*pc++=*pb++;
for(i=0;i
评论
0
0
加载更多
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询