转载自:http://blog.csdn.net/greenhandcgl/article/details/44859637
顺序表结构的实现:
[cpp] view plain copy
- #include
- #include
- #include
- #define LIST_INT_SIZE 100
- #define LISTINCREMENT 10
- #define OK 1
- #define ERROR 0
- #define TRUE 1
- #define FALSE 0
- #define OVERFLOW -1
- typedef int ElemType;
- typedef int Status;
- typedef struct{
- ElemType *elem;
- int length;
- int listsize;
- }SqList;
- Status//初始化线性表
- {
- elem = (ElemType *)malloc(LIST_INT_SIZE * sizeof(ElemType));
- if(!L->elem) exit(OVERFLOW);
- length = 0;
- listsize = LIST_INT_SIZE;
- return OK;
- }
- Status DestroyList(SqList *L)//销毁线性表
- {
- if(!L->elem) exit(ERROR);
- free(L->elem);
- elem = NULL;
- return OK;
- }
- Status CreatList_Sq(SqList *L,int n)
- {
- int
- printf("输入%d个整数:\n",n);
- for(i=0;i
- scanf("\n%d",&L->elem[i]);
- return OK;
- }
- Status ClearList(SqList *L)//将线性表置为空表
- {
- if(!L->elem) return ERROR;
- length = 0;
- return OK;
- }
- Status ListEmpty(SqList *L)//判断线性表是否为空
- {
- if(!L->elem ||length ||length >listsize) return ERROR;
- if(L->length == 0)
- return TRUE;
- else
- return FALSE;
- }
- Status ListLength(SqList *L)//返回线性表长度
- {
- if(!L->elem ||length ||length >listsize)
- return ERROR;
- returnlength;
- }
- Status GetElem(SqList *L, int//用e返回第i个元素的值
- {
- if(!L->elem ||return ERROR;
- *e =elem[i-1];
- return OK;
- }
- Status LocatElem(SqList *L, ElemType e, int compare(void *, void *))
- {
- if(!L->elem) return ERROR;
- int
- for(i = 0;
- if(compare((ElemType *)e, (ElemType *)L->elem[i]) == TRUE)
- return
- }
- return FALSE;
- }
- Status PriorElem(SqList *L, ElemType cur_e, ElemType *pre_e)
- {
- int
- for (i = 1;
- if (L->elem[i] == cur_e) {
- *pre_e =elem[i-1]; /* 与Elemtype 相关*/
- return OK;
- }
- }
- return FALSE;
- }
- Status NextElem(SqList *L, ElemType cur_e, ElemType *next_e)
- {
- int
- for (i=0;
- if (L->elem[i] == cur_e) {
- *next_e = (int )L->elem[i+1]; /* 与Elemtype 相关*/
- return OK;
- }
- }
- return FALSE;
- }
- Status ListInsert(SqList *L, int//向线性表中插入元素
- {
- ElemType *newbase;
- if(i ||length+1) return ERROR;
- if(L->length >=listsize)
- {
- newbase = (ElemType *)realloc(L->elem, (L->listsize + LISTINCREMENT)*sizeof(ElemType));
- if(!newbase) exit(OVERFLOW);
- elem = newbase;
- listsize += LISTINCREMENT;
- }
- ElemType *q = &(L->elem[i-1]);
- ElemType *p;
- for(p = &(L->elem[L->length-1]); p>=q; --p)
- *(p+1) = *p;
- *q = e;
- ++L->length;
- return OK;
- }
- Status ListDelete(SqList *L, int//从线性表中删除元素
- {
- if(i||L->length) return ERROR;
- ElemType *p = &(L->elem[i-1]);
- e = *p;
- ElemType *t = &(L->elem[L->length-1]);
- for(++p; p <= t; ++p) *(p-1) = *p;
- --L->length;
- return OK;
- }
- Status ListTraverse(SqList *L, int (* visit)(void *))
- {
- int
- for (i = 0;
- //如果失败
- if (!visit((int *)L->elem[i]))/* 与Elemtype 相关*/
- return ERROR;
- return TRUE;
- }
- Status ListPrint(SqList *L)
- {
- int
- if (!L->elem ||length ||length >listsize){
- printf("empty!\n");
- return ERROR;
- }
- for (i = 0;
- printf("%5d ",elem[i]);
- }
- printf("\n");
- return TRUE;
- }
[cpp] view plain copy
- int main()
- {
- int
- SqList a;
- SqList *l = &a;
- if(InitList(l)==-2) printf("分配失败");
- printf("\n输入要建立的线性表l的长度n:");//输入线性表得长度
- scanf("%d",&n);
- length=n;
- printf("线性表的长度是:%d\n",l->length);
- CreatList_Sq(l,n);//生成线性表
- puts("原序:");//输出线性表中的元素
- ListPrint(l);
- puts("");
- int ce, pe;
- printf("请输入要查找前驱的元素:");
- scanf("%d", &ce);
- PriorElem(l, ce, &pe);
- printf("%d的前驱是%d\n", ce, pe);
- NextElem(l, ce, &pe);
- printf("%d的后继是%d\n", ce, pe);
- printf("请输入要插入的元素和插入位置:");
- scanf("%d%d", &ce,&pe);
- ListInsert(l, pe,ce);
- printf("插入元素%d后的线性表为\n", ce);
- ListPrint(l);
- printf("请输入要删除元素位置:");
- scanf("%d", &ce);
- ListDelete(l, ce, pe);
- printf("删除%d位置的font-family: Arial, Helvetica, sans-serif;">元素font-family: Arial, Helvetica, sans-serif;">后的线性表为\n", ce);
[cpp] view plain copy
- ListPrint(l);
- DestroyList(l);//销毁顺序表
- return 0;
运行结果: