比较线性表的大小

 
  
#include < stdio.h >
#include
< stdlib.h >
#include
< string .h >
#define MAXSIZE 100
typedef
int DataType;
typedef
struct
{
DataType data[MAXSIZE];
int last;
}seqlist;
seqlist
* Creat_seqlist()
{
int n;
seqlist
* L;
L
= (seqlist * )malloc( sizeof (seqlist));
L
-> last =- 1 ;
printf(
" 请输入创建的线性表的数据元素的个数:\n " );
scanf(
" %d " , & n);
for ( int i = 0 ;i < n;i ++ )
{
printf(
" 请输入第%d个数据元素的值:\n " ,i + 1 );
scanf(
" %d " , & L -> data[i]);
}
L
-> last = n - 1 ;
return L;
}
int cmp(seqlist * a,seqlist * b)
{
seqlist
* na, * nb;
na
= (seqlist * )malloc( sizeof (seqlist));
na
-> last =- 1 ;
nb
= (seqlist * )malloc( sizeof (seqlist));
nb
-> last =- 1 ;
int i = 0 ;
while (i <= a -> last && i <= b -> last && a -> data[i] == b -> data[i])
i
++ ;
for ( int j = i;j <= a -> last;j ++ )
{
na
-> data[j - i] = a -> data[j];
na
-> last ++ ;
}
for ( int j = i;j <= b -> last;j ++ )
{
nb
-> data[j - i] = b -> data[j];
nb
-> last ++ ;
}
if (na -> last == nb -> last && na -> last ==- 1 )
return 0 ;
else
{
if (na -> last >- 1 && nb -> last ==- 1 || na -> last >- 1 && nb -> last >- 1 && na -> data[ 0 ] > nb -> data[ 0 ])
return 1 ;
else
return - 1 ;
}
}
int main()
{
int n;
seqlist
* A, * B;
A
= Creat_seqlist();
B
= Creat_seqlist();
n
= cmp(A,B);
if (n == 0 )
printf(
" 线性表A==B\n " );
else
{
if (n == 1 )
printf(
" 线性表A>B\n " );
else
printf(
" 线性表A<B\n " );
}
system(
" pause " );
return 0 ;
}

转载于:https://www.cnblogs.com/zhangshu/archive/2011/04/24/2026427.html

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值