线性表(静态顺序存储结构)c语言描述

#include <iostream.h>
#define MAXSIZE 50
typedef int ElemType;
typedef struct
{
 ElemType list[MAXSIZE];
 int size;
}List;
void initList(List *L);
int listSize(List L);
int listEmpty(List *L);
ElemType getElement(List L,int pos);
void traverseList(L);
int find(List L,ElemType *item);
int update(List *L,ElemType *item);
void insertRear(List *L,ElemType item);
void insertFront(List *L,ElemType item);
void insert(List *L,ElemType item);
ElemType deleteFront(List *L);
int deleteL(List *L,ElemType *item);
void sort(List *L);
void main()
{
 List L1;
 int i;
 ElemType d;
 initList(&L1);
 for(i=0;i<10;i++)
 {
  insertRear(&L1,i);
 } 
 traverseList(L1);
 printf("/n");
 printf("the List's size is %d ./n",listSize(L1));
 printf("the fourth element in the list is %d./n",getElement(L1,4));
 if(1==find(L1,11))
 {
  printf("this element 11 is included the list./n");
 }
 else
 {
  printf("no such element in the list./n");
 }
 insertFront(&L1,11);
 traverseList(L1);
 printf("/n");
 sort(&L1);
 //printf("to delete the first element %d./n",deleteFront(&L1));
 traverseList(L1);
 printf("/n");
 insert(&L1,5);
 traverseList(L1);
 printf("/n");
 printf("to delete element 5 from the list./n");
 d=5;
 deleteL(&L1,&d);
 traverseList(L1);
 printf("/n");


}
void initList(List *L)
{
 L->size=0;
}
int listSize(List L)
{
 return L.size;
}
int listEmpty(List *L)
{
 if(0==L->size)
 {
  return 1;
 }
 return 0;
}
ElemType getElement(List L,int pos)
{
 if(pos<1 || pos>L.size)
 {
  printf("pos is out range!/n");
  exit(1);
 }
 return L.list[pos-1];
}
void traverseList(List L)
{
 int i;
 for(i=0;i<L.size;i++)
 {
  printf("%d ",L.list[i]);
 }
}

int find(List L,ElemType *item)
{
 int i;
 for(i=0;i<L.size;i++)
 {
  if(item==L.list[i])
  {
   *item=L.list[i];
   return 1;
  }
 }
 return 0;
}

int update(List *L,ElemType *item)
{
 int i;
 for(i=0;i<L->size;i++)
 {
  if(item==L->list[i])
  {
   L->list[i]=item;
   return 1;
  }
 }
 return 0;
}

void insertRear(List *L,ElemType item)
{
 if(L->size==MAXSIZE)
 {
  printf("L->list overflow!!/n");
  exit(1);
 }
 L->list[L->size]=item;
 L->size++;
}

void insertFront(List *L,ElemType item)
{
 int i;
 if(L->size==MAXSIZE)
 {
  printf("L->list overflow!!/n");
  exit(1);
 }
 for(i=L->size-1;i>=0;i--)
 {
  L->list[i+1]=L->list[i];
 }
 L->list[0]=item;
 L->size++;
}

void insert(List *L,ElemType item)
{
 int i,j;
 if(L->size==MAXSIZE)
 {
  printf("L->list overflow!!/n");
  exit(1);
 }
 for(i=0;i<L->size;i++)
 {
  if(item<L->list[i])
  {
   break;
  }
 }
 for(j=L->size-1;j>=i;j--)
 {
  L->list[j+1]=L->list[j];
 }
 L->list[i]=item;
 L->size++;
}

ElemType deleteFront(List *L)
{
 ElemType temp;
 int i;
 if(1==listEmpty(L))
 {
  printf("L->list overflow!!/n");
  exit(1);
 }
 
 temp=L->list[0];
 for(i=1;i<L->size;i++)
 {
  L->list[i-1]=L->list[i];
 }
 L->size--;
 return temp;
}

int deleteL(List *L,ElemType *item)
{
 int i,j;
 if(1==listEmpty(L))
 {
  printf("L->list no any element!!/n");
  exit(1);
 }
 for(i=0;i<L->size;i++)
 {
  if(*item==L->list[i])
   break;
 }
 if(i==L->size)
 {
  printf("L->list no such element!!/n");
  return 0;
 }
 *item=L->list[i];
 for(j=i+1;j<L->size;j++)
 {
  L->list[j-1]=L->list[j];
 }
 L->size--;
 return 1;

}

void sort(List *L)
{
 int i,j;
 ElemType x;
 for(i=1;i<L->size;i++)
 {
  x=L->list[i];
  for(j=i-1;j>=0;j--)
  {
   if(x<L->list[j])
   {
    L->list[j+1]=L->list[j];
   }
   else
   {
    break;
   }
  }
  L->list[j+1]=x;
 }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值