建立线性表

10 篇文章 0 订阅
2 篇文章 0 订阅



#include <stdlib.h>
#include <stdio.h>
#define TRUE  1
#define  FALSE  0
#define  OK    1
#define  ERROR   0
#define  OVERFLOW  -2
typedef   int Status;
#define   LIST_INIT_SIZE  100

typedef int ElemType;
typedef struct
{
   ElemType *elem;
   int length;
   int listsize;
}SqList;

Status InitList(SqList &L)
{
   L.elem=(ElemType *)malloc(LIST_INIT_SIZE*
    sizeof(ElemType));
   if(!L.elem)
    exit(OVERFLOW);
   L.length=0;
   L.listsize=LIST_INIT_SIZE;
   return OK;
}
int ListLength(SqList L)
{
   return L.length;
}
Status getElem(SqList L,int i,ElemType &e)
{
   if(i<=0 || i>L.length)
      return ERROR;
   e=L.elem[i-1];
   return OK;
}

Status fun(ElemType a,ElemType b)
{
   if(a==b)
     return TRUE;
   else
     return FALSE;
}
int LocateElem(SqList L,ElemType e,
   Status (*compare)(ElemType,ElemType))
{
    int i=1;
    ElemType *p;
    p=L.elem;
    while(i<=L.length && !(*compare)(*p++,e))
      ++i;
   if(i<=L.length)
     return i;
  else
    return 0;
}
int main()
{
   SqList List;
   int i;
   ElemType e;
   InitList(List);

   for(i=0;i<5;i++)
     List.elem[i]=i+10;
   List.length=5;

  printf("%d\n",ListLength(List));

  getElem(List,3,e);
  printf("%d\n",e);

  e=11;
  i=LocateElem(List,e,fun);
  printf("%d\n",i);

}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值