线性表的顺序表示和实现
#include <stdio.h> #include <stdlib.h> #define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define INFEASIBLE -1 #define OVERFLOW -2 #define LIST_INIT_SIZE 100 #define LISTINCREMENT 10 typedef int Status; typedef int ElemType; typedef struct { ElemType *elem; int length; int listsize; }SqList; Status InitList_Sq(SqList &L)//线性表的初始化 { L.elem=(ElemType *)malloc(sizeof(ElemType)*LIST_INIT_SIZE); if(!L.elem) { printf("存储分配失败"); exit(OVERFLOW); } L.length=0; L.listsize=LIST_INIT_SIZE; return OK; } Status ListInsert_Sq(SqList &L,int i,ElemType e)//插入 { ElemType *newbase,*p,*q; if(i<1 || i>L.length+1) { printf("i的值不合法"); return ERROR; } if(L.length>=L.listsize) { newbase=(ElemType *)realloc(L.elem,sizeof(ElemType)*(L.length+LISTINCREMENT)); if(!newbase) { printf("存储分配失败"); exit(OVERFLOW); } L.elem=newbase; L.listsize+=LISTINCREMENT; } q=&(L.elem[i-1]); for(p=&(L.elem[L.length-1]) ; p>=q ; p--) *(p+1)=*p; *q=e; ++L.length; return OK; } Status ListDelete_Sq(SqList &L,int i,ElemType e)//删除 { ElemType *p,*q; if(i<1 || i>L.length+1) { printf("i的值不合法"); return ERROR; } p=&(L.elem[i-1]); e=*p; q=L.elem+L.length-1; for(++p; p<=q ; ++p) *(p-1)=*p; --L.length; return OK; } int compare(ElemType e1, ElemType e2) { if(e1==e2) return 1; else return 0; } int LocateElem_Sq(SqList L,ElemType e)//定位 { int i; ElemType *p; i=1,p=L.elem; while(i<=L.length && !compare(*p++,e)) ++i; if(i<L.length) return i; return 0; } Status PrintList_Sq(SqList &L)//打印 { ElemType i; for(i=0 ; i<L.length; i++ ) { printf("%d ",*(L.elem+i)); } return OK; } Status ClearList_Sq(SqList &L)//清空 { L.length=0; return OK; } Status DestoryList_Sq(SqList &L)//销毁 { int i; for(i=0 ; i<L.length ; i++) { free(L.elem+i); } return OK; } int ListLength_Sq(SqList &L)//长度 { return L.length; } Status ListEmpty_Sq(SqList &L)//是否为空 { if(L.length) return FALSE; return TRUE; } Status GetElem_Sq(SqList L,int i,ElemType &e)//获得元素 { if(i<0 || i>L.length+1) { printf("i的值不合法"); return ERROR; } e=*(L.elem+i); return OK; } Status NextElem(SqList &L,ElemType cur_e,ElemType &next_e)//下一个元素 { int i; i=LocateElem_Sq(L,cur_e)-1; if(i>L.length-1 || i<0) return ERROR; else next_e=*(L.elem+i+1); return OK; } int PriorElem(SqList &L,ElemType cur_e,ElemType &pre_e)//上一个元素 { int i; i=LocateElem_Sq(L,cur_e)-1; { if (i>=1) { pre_e=*(L.elem+i-1); return OK; } else { return ERROR; } } } int main() { int i=1; SqList L; ElemType data,search,prior,next; InitList_Sq(L); printf("请输入要插入的数,以0结束\n"); while(scanf("%d",&data),data) { ListInsert_Sq(L,i,data); i++; } printf("你创建的线性表的长度是:%d\n",ListLength_Sq(L)); printf("\n请输入你要查找的数\n"); scanf("%d",&search); if(!LocateElem_Sq(L,search)) { printf("您输入的不存在\n"); } else { printf("你要查找的数的位子是在第%d位\n",LocateElem_Sq(L,search)); } printf("\n请输入你要查找数的后一位(即找他的前一位)\n"); scanf("%d",&search); if(!PriorElem(L,search,prior)) { printf("你所输入的数的前一位不存在\n"); } else { printf("%d,的前一位是%d\n",search,prior); } printf("\n请输入你要查找数的前一位(即找他的后一位)\n"); scanf("%d",&search); if(!NextElem(L,search,next)) { printf("你所输入的数的后一位不存在\n"); } else { printf("%d,的后一位是%d\n",search,next); } printf("\n你所创建的顺序表是:\n"); PrintList_Sq(L); printf("\n"); return 0; }
数据结构->第二章->线性表的顺序表示和实现
评论

浙公网安备 33010602011771号