您的位置:首页 > 编程语言 > C语言/C++

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

2009-06-10 11:24 381 查看
#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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: