赞
踩
首先进行头文件的使用、宏定义以及使用typedef创建别名:
- #include<stdio.h>
- #include<stdlib.h>
- #define OK 1
- #define ERROR 0
- #define LIST_INIT_SIZE 100
- #define LISTINCREAMENT 10
- typedef float ElemType;
- typedef int Status;
- typedef struct
- {
- ElemType *elem; /* 指向线性表占用的数组空间。*/
- int length; /*线性表的长度*/
- int listsize;
- } SqList;
五个包文件:
1.对空表的创建:
- //创建空表
- Status initlist_sq(SqList &L)
- {
- L.elem=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));
- L.length=0;
- L.listsize=LIST_INIT_SIZE;
- return OK;
- }
2.插入:
- Status listinsert(SqList &L,int i,ElemType e)
- {
- int j;
- if(i<1||i>L.length+1)
- return ERROR;
- if(L.length==L.listsize)
- return ERROR;
- for(j=L.length-1;j>=i-1;j--)
- {
- L.elem[j+1]=L.elem[j];
- }
- L.elem[i-1]=e;
- ++L.length;
- return OK;
- }
3.删除:
- //删除
- Status listdelete(SqList &L,int i)
- //i代表的不是数组下标
- { int j;
- if((i<1)||(i>L.length))
- return ERROR;
- for(j=i;j<=L.length-1;j++)
- L.elem[j-1]=L.elem[j];
- --L.length;
- return OK;
- }
4.取值:
- //取值
- Status GetElem(SqList L,int i,ElemType &e){
- if(i<1||i>L.length)return ERROR;
- e=L.elem[i-1];
- return OK;
- }
5.查找:
- //查找
- Status LocateElem(SqList L,ElemType e)
- {
- //在顺序表中查询第一个满足判定条件的数据元素,若存在,则返回它的位序,若不存在,则返回
- //ERROR
- int i=1;
- while(i<=L.length&&L.elem[i-1]!=e)
- i++;
- if(i<=L.length) return i;
- else return ERROR;
- }
对包的使用:
- int main(){
- SqList L;
- int i,z;
- ElemType x,y;
- initlist_sq(L);
- for(i=1;i<=10;i++){
- scanf("%f",&x);
- listinsert(L,i,x);
- }
- GetElem(L,4,y);
- printf("%f\n",y);
-
- listdelete(L,3);
- for(i=1;i<10;i++){
- printf("%5.1f",L.elem[i-1]);
- }
- printf("\n");
- GetElem(L,4,y);
- printf("%f\n",y);
- z=LocateElem(L,5);
- printf("%d\n",z);
- return 0;
- }
基本代码如下:
- //线性表
- #include<stdio.h>
- #include<stdlib.h>
- #define OK 1
- #define ERROR 0
- #define LIST_INIT_SIZE 100
- #define LISTINCREAMENT 10
- typedef float ElemType;
- typedef int Status;
- typedef struct
- {
- ElemType *elem; /* 指向线性表占用的数组空间。*/
- int length; /*线性表的长度*/
- int listsize;
- } SqList;
- //创建空表
- Status initlist_sq(SqList &L)
- {
- L.elem=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));
- L.length=0;
- L.listsize=LIST_INIT_SIZE;
- return OK;
- }
- //插入
- Status listinsert(SqList &L,int i,ElemType e)
- {
- int j;
- if(i<1||i>L.length+1)
- return ERROR;
- if(L.length==L.listsize)
- return ERROR;
- for(j=L.length-1;j>=i-1;j--)
- {
- L.elem[j+1]=L.elem[j];
- }
- L.elem[i-1]=e;
- ++L.length;
- return OK;
- }
- //删除
- Status listdelete(SqList &L,int i)
- //i代表的不是数组下标
- { int j;
- if((i<1)||(i>L.length))
- return ERROR;
- for(j=i;j<=L.length-1;j++)
- L.elem[j-1]=L.elem[j];
- --L.length;
- return OK;
- }
- //取值
- Status GetElem(SqList L,int i,ElemType &e){
- if(i<1||i>L.length)return ERROR;
- e=L.elem[i-1];
- return OK;
- }
- //查找
- Status LocateElem(SqList L,ElemType e)
- {
- //在顺序表中查询第一个满足判定条件的数据元素,若存在,则返回它的位序,若不存在,则返回
- //ERROR
- int i=1;
- while(i<=L.length&&L.elem[i-1]!=e)
- i++;
- if(i<=L.length) return i;
- else return ERROR;
- }
-
-
- int main(){
- SqList L;
- int i,z;
- ElemType x,y;
- initlist_sq(L);
- for(i=1;i<=10;i++){
- scanf("%f",&x);
- listinsert(L,i,x);
- }
- GetElem(L,4,y);
- printf("%f\n",y);
-
- listdelete(L,3);
- for(i=1;i<10;i++){
- printf("%5.1f",L.elem[i-1]);
- }
- printf("\n");
- GetElem(L,4,y);
- printf("%f\n",y);
- z=LocateElem(L,5);
- printf("%d\n",z);
- return 0;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。