赞
踩
顺序表也是线性表,具有线性结构。线性结构分为两种,一是顺序存储,在内存中是连续的;二是链式存储,在内存中是不连续的。
顺序表的特点:
(1) 除第一个元素外,其他所有元素都只有一个直接前驱。
(2) 除最后一个元素外,其他所有元素都只有一个直接后继。
顺序表有容量和长度两个属性。长度指的是,顺序表中有效元素的个数。容量指的是顺序表中最大的可能长度。
若顺序表中的有效元素为0,即长度为0,则称之为空表。
顺序表头文件sequencelist.h
- #ifndef _SEQUENCELIST_H
- #define _SEQUENCELIST_H
-
- #define SUCCESS 10000
- #define FAILURE 10001
- #define TRUE 10002
- #define FAULSE 10003
- #define SIZE 10
-
- typedef int ElemType;
-
- struct SequenceList
- {
- int length;
- ElemType *elem;
- };
-
- typedef struct SequenceList SqList;
-
- int ListInit(SqList *list);
- int ListInsert(SqList *list, int position, ElemType e);
- int ListLength(SqList list);
- int ListEmpty(SqList list);
- int GetElem(SqList L, int position, ElemType *elem);
- int ListTraverse(SqList list, void (*p)(ElemType));
- int LocateElem(SqList list, ElemType elem, int (*p)(ElemType, ElemType));
- int ListDelete(SqList *list, int position, ElemType *elem);
- int ListClear(SqList *list);
- int ListDestory(SqList *list);
- int ListInsertSort(SqList *list, ElemType elem, int (*p)(ElemType, ElemType));
- #endif
顺序表初始化
- int ListInit(SqList *list)
- {
- if(NULL == list)
- {
- return FAILURE;
- }
-
- list->length = 0;
-
- list->elem = (ElemType *)malloc( SIZE*sizeof(ElemType));
- if(NULL == list->elem)
- {
- return FAILURE;
- }
-
- return SUCCESS;
- }
顺序表插入
- int ListInsert(SqList *list, int position, ElemType elem)
- {
- int i;
-
- if(NULL == list || NULL == list->elem)
- {
- return FAILURE;
- }
-
- if(position > list->length + 1 || position < 1 || list->length >= SIZE)
- {
- return FAILURE;
- }
-
- for(i = 0; i < list->length - position +1; i++)
- {
- list->elem[list->length - i] = list->elem[list->length - i -1];
- }
-
- list->elem[position - 1] = elem;
- list->length++;
-
- return SUCCESS;
- }
顺序表求长
- int ListLength(SqList list)
- {
- return list.length;
- }
顺序表判空
- int ListEmpty(SqList list)
- {
- return (list.length == 0) ? TRUE : FAULSE;
- }
顺序表求值
- int GetElem(SqList L, int position, ElemType *elem)
- {
- if(position < 1 || position > L.length)
- {
- return FAILURE;
- }
- *elem = L.elem[position - 1];
-
- return SUCCESS;
- }
顺序表遍历
- int ListTraverse(SqList list, void (*p)(ElemType))
- {
- int i;
-
- if( 0 == list.length)
- {
- return FAILURE;
- }
-
- for( i = 0; i < list.length; i++)
- {
- (*p)(list.elem[i]);
- }
-
- return SUCCESS;
- }
顺序表检索
- int LocateElem(SqList list, ElemType elem, int (*p)(ElemType, ElemType))
- {
- int i;
- if( 0 == list.length)
- {
- return FAILURE;
- }
-
- for(i = 0; i < list.length; i++)
- {
- if(TRUE == (*p)(list.elem[i], elem))
- {
- return i+1;
- }
- }
-
- return FAILURE;
- }
顺序表删除
- int ListDelete(SqList *list, int position, ElemType *elem)
- {
- int i;
-
- if(NULL == list)
- {
- return FAILURE;
- }
-
- if(position > list->length + 1 || position < 1)
- {
- return FAILURE;
- }
-
- *elem = list->elem[position - 1];
- for(i = 0; i < list->length -position; i++)
- {
- list->elem[position - 1 + i] = list->elem[position + i];
- }
- list->length--;
-
- return SUCCESS;
- }
顺序表重置
- int ListClear(SqList *list)
- {
- if(NULL == list)
- {
- return FAILURE;
- }
-
- list->length = 0;
-
- return SUCCESS;
- }
顺序表销毁
- int ListDestory(SqList *list)
- {
- if(NULL == list)
- {
- return FAILURE;
- }
-
- list->length = 0;
- free(list->elem);
- list->elem = NULL;
-
- return SUCCESS;
- }
测试文件
- #include<stdio.h>
- #include<stdlib.h>
- #include "sequencelist.h"
- #include<time.h>
-
- void print(ElemType elem)
- {
- printf("%d ", elem);
- }
- int EQ(ElemType L_elem, ElemType elem)
- {
- if(L_elem == elem)
- {
- return TRUE;
- }
- else
- {
- return FALSE;
- }
- }
- int GT(ElemType L_elem, ElemType elem)
- {
- if(L_elem > elem)
- {
- return TRUE;
- }
- else
- {
- return FALSE;
- }
- }
- int LT(ElemType L_elem, ElemType elem)
- {
- if(L_elem < elem)
- {
- return TRUE;
- }
- else
- {
- return FALSE;
- }
- }
- int main()
- {
- SqList L;
- int ret, i;
- ElemType elem;
- int position = 3;
-
- srand(time(NULL));
-
- ret = ListInit(&L);
- if(SUCCESS == ret)
- {
- printf("Init success.\n");
- }
- else
- {
- printf("Init failure.\n");
- }
-
-
- for(i = 0; i < 12; i++)
- {
- ret = ListInsert(&L, i + 1, rand() % 10);
- if(SUCCESS == ret)
- {
- printf("Insert success.\n");
- }
- else
- {
- printf("Insert failure.\n");
- }
- }
-
-
- printf("Length = %d\n", ListLength(L));
- printf((ListEmpty(L) == TRUE) ? "Empty.\n" : "Not empty.\n");
-
-
- position = 3;
- ret = GetElem(L, position, &elem);
- if( FAILURE == ret)
- {
- printf("GetElem failure\n");
- }
- else
- {
- printf("the %dth is %d\n", position, elem);
- }
-
-
- ret = ListTraverse(L,print);
- if( FAILURE == ret)
- {
- printf("\nTraverse failure.\n");
- }
- else
- {
- printf("\nTraverse success.\n");
- }
-
- elem = 3;
- ret = LocateElem(L, elem, EQ);
- if(FAILURE == ret)
- {
- printf("failure\n");
- }
- else
- {
- printf(" %d is the %dth element\n", elem, ret);
- }
-
-
- position = 3;
- ret = ListDelete(&L, position, &elem);
- if(FAILURE == ret)
- {
- printf("Delete failure.\n");
- }
- else
- {
- printf("Delete the %dth element %d success.\n", position, elem);
- }
- ret = ListTraverse(L,print);
- if( FAILURE == ret)
- {
- printf("\nTraverse failure.\n");
- }
- else
- {
- printf("\nTraverse success.\n");
- }
-
- ret = ListClear(&L);
- if(FAILURE == ret)
- {
- printf("Clear failure.\n");
- }
- else
- {
- printf("Clear success.\n");
- }
-
- ret = ListDestory(&L);
- if(FAILURE == ret)
- {
- printf("Destory failure.\n");
- }
- else
- {
- printf("Destory success.\n");
- }
- for(i = 0; i < 12; i++)
- {
- ret = ListInsert(&L, i + 1, rand() % 10);
- if(SUCCESS == ret)
- {
- printf("Insert success.\n");
- }
- else
- {
- printf("Insert failure.\n");
- }
- }
- return 0;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。