鉴于自己的数据结构知识较为薄弱,并且自己常常学完没几天又忘了,每次用起来都要去查阅,所以现在开个博客来记录自己在学数据结构过程中自己的一些学习总结,也可以加深自己的理解。
第一篇要总结的是线性表。
线性表:零个或多个相同特性的数据元素的有限序列。首先它是一个序列,元素之间是有顺序 的,第一个元素无前驱,最后一个元素无后继,其他的都有且只有一个前驱和后继。而所有元素按这种1对1的邻接关系构成的整体就是线性表。
线性表的存储结构:线性表有顺序表和链式两类存储结 构
(一)顺序表
顺序表就是把线性表中的所有元素按照其逻辑顺序,依次存储到从计算机存储器中指定存储位置开始的一块连续的存储空间,如下图所示,即用一维数组来实现顺序表。

#include<iostream>
#include<stdlib.h>
#define listmax 100
#define listadd 10
#define OK 1
#define ERROR 0
using namespace std;
typedef int Elemtype;
typedef int Status;
typedef struct
{
Elemtype *elem;//memory space de bese add
int length;
int listsize;
}Sqlist;
Status Initlist(Sqlist &L)
{
L.elem=(Elemtype*)malloc(listmax*sizeof(Elemtype));
if(!L.elem)
exit(0);
L.length=0;
L.listsize=listmax;
return OK;
}//25
Status Clearlist(Sqlist &L)
{
L.length=0;
return OK;
}
Status Destorylist(Sqlist &L)
{
free(L.elem);
L.elem=NULL;
L.length=0;
L.listsize=0;
return OK;
}
bool Listempty(Sqlist L)
{
return (L.length==0)?true:false;
}
bool Listfull(Sqlist L)
{
return (L.length==L.listsize)?true:false;
}
Status Getnumlist(Sqlist L)
{
return L.length;
}
Status Getelem(Sqlist L,int i,Elemtype &e)//51
{
if(i<1||i>L.length)
exit(0);
e=*(L.elem+i-1);
return e;
}
int Locateelem(Sqlist L,Elemtype e,int a)//58
{
int i,j=1;
Elemtype *p;
p=L.elem;
for(i=0;i<L.length;i++,p++)
{
if(*p==e)
{ a=j;
return a;}
else
j++;
}
}
Status Priorelem(Sqlist L,Elemtype a,Elemtype &b)
{
int i;
Elemtype *p;
for(i=1;i<L.length;i++)
{
if(*p==a)
{
b=*(L.elem+i-2);
return b;
}
else
return ERROR;
}
return OK;
}
Status Nextelem(Sqlist L,Elemtype a,Elemtype &c)
{
int i;
Elemtype *p;
for(i=0;i<L.length-1;i++) //91
{
if(*p==a)
{
c=*(L.elem+i);
return c;
}
else
return ERROR;
}
return OK;
}
Status Listinsert(Sqlist &L,Elemtype e,int i)
{
Elemtype *newbase,*p,*q;
if(i<1||i>L.length+1)
return ERROR;
if(L.length>L.listsize)
{
if(!(newbase=(Elemtype*)realloc(L.elem,(L.listsize+listadd)*sizeof(Elemtype))))
exit(0);
L.elem=newbase;
L.listsize+=listadd;
}
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 Listdele(Sqlist &L,Elemtype &e,int i)
{
Elemtype *q,*p;//127
if(i<1||i>L.length)//i值不合法
return ERROR;
q=L.elem+i-1;
*q=e;
p=L.elem+L.length-1;
for(;q<=p;++q)
{
*(q-1)=*q;
L.length--;
return OK;
}
}
Status ListTraverse(Sqlist L)
{
Elemtype *p=L.elem;
int i;
for(i=1;i<=L.length;i++,p++)
{ cout<<*p;
cout<<endl;}
return OK;
}
int main()
{
Sqlist L;
Initlist(L);
Listinsert(L,10,1);
Listinsert(L,11,2);
Listinsert(L,12,3);
ListTraverse(L);
return 0;
}
版权声明:本文为weixin_43408374原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。