1. 顺序存储结构:数据元素放在地址连续的存储单元里。
2. 链式存储结构:数据元素存放在任意的存储单元里,存储单元可以不连续。
一个线性表是n个数据元素的有限序列。
以下为源代码,主要功能有:初始化线性表;插入(插入随机数,并在插入时由小到大排序);求出线性表长度;判断线性表是否为空;得到线性表某个位置的元素;遍历线性表;查找元素e在线性表中的位置;删除线性表位置p的元素;清空线性表;销毁线性表(清空并释放分配的空间)
#ifndef _SEQUENCELIST_H
#define _SEQUENCELIST_H
#define SIZE 10
#define SUCCESS 10000
#define FAILURE 10001
#define TRUE 10002
#define FALSE 10003
typedef int ElemType;
struct SequenceList
{
int length;
ElemType *data;
};
typedef struct SequenceList SeList;
int SequenceInit(SeList *l);
int SequenceInsert(SeList *l, ElemType e);
int SequenceLength(SeList l);
int SequenceEmpty(SeList l);
int GetElem(SeList l, int p, ElemType *e);
int SequenceTraverse(SeList l, void (*p)(ElemType));
int LocateElem(SeList l, ElemType e);
int SequenceDelete(SeList *l, int p, ElemType *e);
int SequenceClear(SeList *l);
int SequenceDestroy(SeList *l);
#endif
#include "SequenceList.h"
#include
#include
/*初始化,建立一个空线性表l*/
int SequenceInit(SeList *l) /*l为结构体指针*/
{
if (NULL == l) /*入参判断*/
{
return FAILURE;
}
l->length = 0; /*设顺序表长度为0(容量为SIZE)*/
l->data = (ElemType *)malloc(sizeof(ElemType) * SIZE);
if (NULL == l->data)
{
return FAILURE;
}
return SUCCESS;
}
/*在某个位置p插入元素e*/
int SequenceInsert(SeList *l, ElemType e)
{
int i, j;
if(l->length == 0)
{
l->length = 1;
l->data[0] = e; /*将第一个元素放入第一个位置*/
printf("%d ", l->data[0]);
return SUCCESS;
}
if(l->length > 0)
{
printf("%d ", e);
for(i = 0; i < l->length; i++)
{
if(e <= l->data[i])
{
for(j = 0; j < l->length - i + 1; j++)
{
l->data[l->length - j] = l->data[l->length - j - 1];
}
l->data[i] = e;
l->length++;
return SUCCESS;
}
}
if(e > l->data[i - 1])
{
l->data[i] = e;
l->length++;
return SUCCESS;
}
}
}
/*求出线性表的长度*/
int SequenceLength(SeList l)
{
return l.length;
}
/*判断线性表是否为空*/
int SequenceEmpty(SeList l)
{
return (l.length == 0) ? TRUE : FALSE;
}
/*得到线性表某个位置的元素*/
int GetElem(SeList l, int p, ElemType *e)
{
if(p < 1 || p > l.length)
{
return FAILURE;
}
*e = l.data[p - 1];
return SUCCESS;
}
/*遍历线性表*/
int SequenceTraverse(SeList l, void (*p)(ElemType))
{
if(p == NULL)
{
return FAILURE;
}
int i;
for(i = 0; i < l.length; i++) /*按顺序输出*/
{
p(l.data[i]); /*print函数打印*/
}
return SUCCESS;
}
/*查找元素e在线性表中的位置*/
int LocateElem(SeList l, ElemType e)
{
int i;
for(i = 0; i < l.length; i++)
{
if(e == l.data[i])
{
return i + 1;
}
}
return FAILURE;
}
/*删除线性表位置p的元素*/
int SequenceDelete(SeList *l, int p, ElemType *e)
{
if(NULL == l)
{
return FAILURE;
}
if(p < 1 || p > l->length)
{
return FAILURE;
}
int i;
*e = l->data[p - 1]; /*将该位置的值赋给e*/
for(i = 0; i < l->length - p; i++) /*删除位置后面的数依次向前移动*/
{
l->data[p - 1 + i] = l->data[p + i];
}
l->length--; /*长度-1删除成功*/
return SUCCESS;
}
/*清空线性表*/
int SequenceClear(SeList *l)
{
if(l == NULL)
{
return FAILURE;
}
l->length = 0;
return SUCCESS;
}
/*销毁线性表*/
int SequenceDestroy(SeList *l)
{
if(l == NULL)
{
return FAILURE;
}
l->length = 0;
free(l->data);
l->data = NULL;
return SUCCESS;
}
#include "SequenceList.h"
#include
#include
void print(ElemType e)
{
printf("%d ", e);
}
int main()
{
int ret, i;
SeList list; /*建立一个结构体对象list*/
srand(time(NULL)); /*以系统时间作为随机数种子产生随机数*/
ret = SequenceInit(&list); /*初始化,建立一个空线性表*/
if(ret == SUCCESS)
{
printf("Init Success!\n");
}
else
{
printf("Init Failure!\n");
}
for(i = 0; i < 5; i++)
{
ret = SequenceInsert(&list, rand()%10); /*插入随机数*/
if(FAILURE == ret) /*判断是否插入成功*/
{
printf("Insert Failure!\n");
}
else
{
printf("Insert Success!\n");
}
}
ret = SequenceLength(list); /*输出线性表长度*/
printf("length is %d\n", ret);
ret = SequenceEmpty(list); /*判断线性表是否为空*/
if(ret == TRUE)
{
printf("is empty!\n");
}
else
{
printf("not empty!\n");
}
/*得到线性表某个位置p的元素*/
int p = 3;
ElemType e;
ret = GetElem(list, p, &e);
if(FAILURE == ret) /*判断是否得到元素*/
{
printf("Get Element Failure!\n");
}
else
{
printf("%dth element is %d\n", p, e);
}
/*遍历线性表*/
ret = SequenceTraverse(list, print);
if(ret == FAILURE)
{
printf("Traverse Failure!\n");
}
else
{
printf("Traverse Success!\n");
}
/*查找元素e在线性表中的位置*/
e = 3;
ret = LocateElem(list, e);
if(ret == FAILURE)
{
printf("%d not exist!\n", e);
}
else
{
printf("%d is %dth element!\n", e, ret);
}
/*删除指定位置p的数*/
p = 3;
ret = SequenceDelete(&list, p, &e);
if(SUCCESS == ret)
{
printf("Delete %d Success!\n", e);
}
else
{
printf("Delete Failure!\n");
}
/*清空线性表*/
ret = SequenceClear(&list);
if(SUCCESS == ret)
{
printf("Clear Success!\n");
}
else
{
printf("Clear Failure!\n");
}
/*销毁线性表*/
ret = SequenceDestroy(&list);
if(SUCCESS ==ret)
{
printf("Destroy Success!\n");
}
else
{
printf("Destroy Failure!\n");
}
return 0;
}