线性表定义:是具有相同类型的n(>=0)个数据元素的有限序列 (a1,a2,a3.....an)。a0是线性表的第一个元素,只有一个后继;an是最后一个元素,只有一个前驱;其他元素既有前驱也有后继;线性表能够逐项访问和顺序存取
集合---创建线性表
解散---销毁线性表
长度---得到线性表的长度
出列---从线性表删除一个元素
添加---在线性表特定位置增加元素
线性表在程序中表现为一种特殊的数据类型,线性表的操作在程序中的表现为一组函数
用c语言描述线性表:
1、线性表的顺序存储结构,指的是用一段地址连续的存储单元依次存储线性表的数据元素
下面一段是SeqList.c
#include <malloc.h>
#include <stdio.h>
#include "SeqList.h"
typedef unsigned int TSeqListNode;
typedef struct _tag_SeqList
{
int capacity;
int length;
TSeqListNode* node;
}TSeqList;
SeqList* SeqList_Create(int capacity)
{
TSeqList* ret = NULL;
if(capacity >= 0)
{
ret = (TSeqList*)malloc(sizeof(TSeqList)+sizeof(TSeqListNode)*capacity); //申请一段内存空间,首地址为ret
}
if(ret != NULL)
{
ret->capacity = capacity;
ret->length = 0;
ret->node = (TSeqListNode*)(ret+1); //指向的是结构体的末尾
}
return ret;
}
void SeqList_Destroy(SeqList* list)
{
free(list);
}
void SeqList_Clear(SeqList* list)
{
TSeqList* sList = (TSeqList*)list;
if(sList!=NULL)
{
sList->length=0;
}
}
int SeqList_Length(SeqList* list) //实际包含数据元素的个数
{
TSeqList* sList = (TSeqList*)list;
int ret = -1;
if(sList!=NULL)
{
ret = sList->length;
}
return ret;
}
int SeqList_capacity(SeqList* list)
{
TSeqList* sList = (TSeqList*)list;
int ret = -1;
if(sList!=NULL)
{
ret = sList->capacity;
}
return ret;
}
int SeqList_Insert(SeqList* list, SeqListNode* node, int pos)
{
TSeqList* sList = (TSeqList*)list;
int ret = (sList != NULL);
int i=0;
ret = ret && (sList->length +1 <= sList->capacity);
ret = ret && (pos >= 0);
if(ret)
{
if(pos >= (sList->length))
{
pos = sList->length;
}
for(i=(sList->length); i>pos; i--)
{
sList->node[i] = sList->node[i-1];
}
sList->node[i] = (TSeqListNode)node;
sList->length++;
}
return ret;
}
SeqListNode* SeqList_Get(SeqList* list, int pos)
{
TSeqList* sList = (TSeqList*)list;
SeqListNode* ret = NULL;
if((sList != NULL)&&(pos >= 0)&&(pos <= sList->length))
{
ret = (SeqListNode*)(sList->node[pos]);
}
return ret;
}
SeqListNode* SeqList_Delete(SeqList* list, int pos)
{
TSeqList* sList = (TSeqList*)list;
SeqListNode* ret = NULL;
int i=0;
if((sList != NULL)&&(pos >= 0)&&(pos < sList->length))
{
ret = (SeqListNode*)(sList->node[pos]);
for(i=pos+1; i<sList->length;i++)
{
sList->node[i-1] = sList->node[i];
}
sList->length--;
}
return ret;
}
下面一段是头文件
SeqList.h
#ifndef _SEQLIST_H_
#define _SEQLIST_H_
typedef void SeqList;
typedef void SeqListNode;
/*
用于创建并且返回一个空的线性表,最大容量为capacity
*/
SeqList* SeqList_Create(int capacity);
/*
用于销毁一个线性表
*/
void SeqList_Destroy(SeqList* list);
/*
用于讲一个线性表SeqList中的所有元素清空,线性表
回到创建时的初始状态
*/
void SeqList_Clear(SeqList* list);
/*
用于返回一个线性表中所有元素个数
*/
int SeqList_Length(SeqList* list);
/*
用于返回一个线性表中所有元素个数
*/
int SeqList_capacity(SeqList* list);
/*
用于向一个线性表SeqList的pos位置插入新元素node
返回值为1表示插入成功,返回0表示插入失败
*/
int SeqList_Insert(SeqList* list, SeqListNode* node, int pos);
/*
用于获取线性表SeqList的pos位置的元素node
返回值为pos位置处的元素,NULL表示获取失败
*/
SeqListNode* SeqList_Get(SeqList* list, int pos);
/*
用于删除线性表SeqList的pos位置的元素node
返回值删除的pos位置处的元素,NULL表示删除失败
*/
SeqListNode* SeqList_Delete(SeqList* list, int pos);
#endif // __2_1_H_
下面是main函数,测试顺序链表
#include <stdio.h>
#include <stdlib.h>
#include "SeqList.h"
int main()
{
int index=0;
SeqList* list = SeqList_Create(5);
int i=0;
int j=1;
int k=2;
int x=3;
int y=4;
int z=5;
SeqList_Insert(list,&i,0);
SeqList_Insert(list,&j,0);
SeqList_Insert(list,&k,0);
SeqList_Insert(list,&x,0);
SeqList_Insert(list,&y,0);
SeqList_Insert(list,&z,0);
for(index=0; index<SeqList_Length(list); index++)
{
int* p = (int*)SeqList_Get(list,index);
printf("%d\n",*p);
}
while(SeqList_Length(list)>0)
{
int *p = SeqList_Delete(list,0);
printf("%d\n",*p);
}
SeqList_Destroy(list);
return 0;
}