定义:
线性表是由
n
(
n
≥
0
)
\bm n(n\ge 0)
n(n≥0)个相同类型的数据元素组成的有限序列。如:
L
(
a
1
,
a
2
,
a
3
,
.
.
.
a
i
−
1
,
a
i
,
a
i
+
1
,
.
.
a
n
)
L\ (a_1,a_2,a_3,...a_{i-1},a_i,a_{i+1},..a_n)
L (a1,a2,a3,...ai−1,ai,ai+1,..an)
基本操作:
Init() | Insert() | Locate() | Retrieve() | Delete() | Previous() |
---|---|---|---|---|---|
Next() | MakeNull() | First() | End() | number() |
顺序表:
该顺序表设置与《数据结构与算法》(廖明宏)中设置相同,数组索引下标为0处不存放数值。
#include<iostream>
using namespace std;
#define maxsize 100
typedef int ElemType,position;
struct List{
ElemType *data;
int last; //最后一个元素在数组中的位置
};
void Init(List &L)
{
L.data= new ElemType[maxsize];
L.last = 0;
}
void Insert(ElemType x,position p,List &L)
{
if(L.last==sizeof(L)-1)
{
cout<<"线性表已满,插入失败"<<endl;
}
else if(p<1||p>L.last+1)
{
cout<<"插入索引越界,插入失败"<<endl;
}
else
{
for(int j=L.last;j>=p;j--)
L.data[j+1]=L.data[j];
L.data[p]=x;
L.last++;
}
}
position Locate(ElemType x,List L)
{
for(int j=1;j<=L.last;j++)
{
if(L.data[j]==x)
return j;
}
return End(L);
}
ElemType Retrieve(position p,List L)
{
if(p<1||p>=End(L))
{
cout<<"索引越界"<<endl;
return 0;
}
else
return L.data[p];
}
void Delete(position p,List &L)
{
if(p<1||p>L.last)
{
cout<<"索引越界,无法删除"<<endl;
}
else
{
for(position j=p;j<L.last;j++)
{
L.data[j]=L.data[j+1];
}
L.last--;
}
}
position Previous(position p,List L)
{
if(p<=1||p>=End(L))
cout<<"没有前驱"<<endl;
else
return p-1;
}
position Next(position p,List L)
{
if(p<1||p>=L.last)
cout<<"没有后继"<<endl;
else
return p+1;
}
int MakeNull(List &L)
{
L.last=0;
return End(L);
}
int First(List L)
{
if(L.last>=1)
return 1;
else
{
cout<<"该表是空表"<<endl;
return 0;
}
}
int End(List L)
{
return L.last+1;
}
int number(ElemType x,List L)
{
int count=0;
for(int i=1;i<=L.last;i++)
{
if(L.data[i]==x)
count++;
}
return count;
}
链表:
此链表为带头节点的链表
#include<iostream>
using namespace std;
#define maxsize 100
typedef int ElemType;
typedef struct Node{
ElemType data;
Node *next;
} Node,*Linklist;
void Init(Linklist &L)
{
L=new Node;
L->next=NULL;
}
void Insert(ElemType x,int position,Linklist &L)
{
Linklist p=L,s=NULL;
if(position<1)
cout<<"索引越界"<<endl;
return ;
for(int i=0;p&&i<position-1;i++)
p=p->next;
if(!p)
cout<<"索引越界"<<endl;
return ;
s=new Node;
s->data=x;
s->next=p->next;
p->next=s;
return ;
}
Linklist Locate(ElemType x,Linklist L)
{
Linklist p;
for(p=L;p->next!=NULL;p=p->next)
if(p->next->data==x)
return p;
return p;
}
ElemType Retrieve(Linklist p,Linklist L)
{
return p->next->data;
}
void Delete(int position,Linklist L)
{
if(position<1)
{
cout<<"索引越界"<<endl;
return ;
}
Linklist p=L;
for(int j=0;p->next&&j<position-1;j++)
p=p->next;
if(!p->next)
{
cout<<"索引越界"<<endl;
return ;
}
else
{
Linklist q=p->next;
p->next=q->next;
delete q;
}
}
Linklist Previous(Linklist p,Linklist L)
{
if(p==L->next)
{
cout<<"没有前驱"<<endl;
return NULL;
}
else
{
Linklist q=L;
while(q->next!=p)
q=q->next;
return q;
}
}
Linklist Next(Linklist p,Linklist L)
{
if(!(p->next))
{
cout<<"没有后继"<<endl;
return NULL;
}
else
return p->next;
}
Linklist MakeNull(Linklist &L)
{
L=new Node;
L->next==NULL;
return L;
}
Linklist First(Linklist L)
{
return L;
}
Linklist End(Lisklist L)
{
Linklist q=L;
while(q->next!=NULL)
{
q=q->next;
}
return q;
}
int number(ElemType x,Linklist L)
{
int count=0;
Linklist p=L->next;
while(p)
{
if(p->data==x)
count++;
p=p->next;
}
return count;
}