线性表(静态顺序存储结构)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;
}
}
#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;
}
}
相关文章推荐
- 老郭带你学数据结构(C语言系列)1-线性表之静态顺序表
- 顺序存储结构线性表基本操作 纯C语言实现
- 学习笔记——线性表的静态分配顺序存储结构基本操作(C语言实现)
- C语言 线性表描述 (实战片)
- C语言 数据结构 线性表 顺序表 线性表的顺序存储结构
- 自定义顺序存储结构线性表--java语言描述
- c语言描述的静态查找表
- C语言线性表顺序存储结构实例详解
- c语言线性表顺序存储结构详解
- 线性表-顺序存储结构(C语言实现)
- 读书笔记【c语言描述】--循环链表
- 静态二维数组和动态二维数组(C语言)
- ※数据结构※→☆线性表结构(stack)☆============栈 顺序存储结构(stack sequence)(六)
- 数据结构基础(5)--C语言实现循环队列--静态
- 用C语言联合体和枚举描述数据库
- c语言之静态局部变量,静态全局变量和静态函数
- c语言:【顺序表】静态顺序表的头插、头删
- 用简单的描述C语言 sprintf函数是什么意思。
- 栈-顺序存储结构-c语言
- 线性表基本操作和简单程序——c语言