数据结构与算法(陈越版)第二讲 线性结构—线性表及其表现
一、线性表的定义
线性表(Linear List):由同一类型的数据元素构成的有序序列的线性结构。
线性表中元素的个数称为线性表的长度
表起始位置称为:表头,表结束位置称为:表尾。
二、线性表的抽象数据类型描述
类型名称:线性表(List)
数据对象集: 线性表是
n
(
≥
n
)
n ( \geq n)
n(≥n)个元素构成的有序序列
(
a
1
,
a
2
,
.
.
.
,
a
n
)
(a_1, a_2,...,a_n)
(a1?,a2?,...,an?)
操作集:初始化,查找,插入,删除等等。
三、线性表的实现
3.1、线性表的顺序存储实现
# include<iostream>
using namespace std;
# define MaxSize 100
# include<malloc.h>
typedef int ElementType;
typedef int Position;
struct LNode
{
ElementType Data[MaxSize];
Position Last;
};
typedef LNode * List;
List CreateList()
{
List L = (List)malloc(sizeof(struct LNode));
L->Last = -1;
return L;
}
void Insert(ElementType x, int i, List L)
{
if (L->Last == MaxSize - 1)
{
cout << "线性表已经满了" << endl;
return;
}
if (i < 0 || i >= MaxSize)
{
cout << "插入不合法" << endl;
return;
}
for (int j = L->Last; j >= i; --j)
{
L->Data[j + 1] = L->Data[j];
}
L->Data[i] = x;
++L->Last;
return;
}
int Findx(ElementType x, List L)
{
int i = 0;
while (i <= L->Last && L->Data[i] != x)
++i;
if (i > L->Last)
{
cout << "查找元数不在线性表中" << endl;
return -1;
}
else
return i;
}
ElementType FindK(int k, List L)
{
if (k < 0 || k > L->Last)
{
cout << "下标为" << k << "的元素不存在" << endl;
return -1;
}
else
return L->Data[k];
}
void Delete(int i, List L)
{
if (i < 0 || i > L->Last)
{
cout << "下标为" << i << "的元素不存在" << endl;
}
for (int j = i; j < L->Last; ++j)
{
L->Data[j] = L->Data[j + 1];
}
--L->Last;
return;
}
int main()
{
List L = CreateList();
Insert(11, 0, L);
Insert(25, 0, L);
Insert(33, 0, L);
Insert(77, 0, L);
Insert(85, 2, L);
for (int i = 0; i <= L->Last; ++i)
{
cout << L->Data[i] << " ";
}
cout << endl;
Insert(60, 102, L);
int a = Findx(25, L);
cout << "25的位置在:" << a << endl;
a = Findx(90, L);
int X = FindK(4, L);
cout << "下标为4的元素是: " << X << endl;
X = FindK(20, L);
Delete(3, L);
cout << "删除之后的排序" << endl;
for (int i = 0; i <= L->Last; ++i)
{
cout << L->Data[i] << " ";
}
cout << endl;
Delete(20, L);
return 0;
}
3.2、线性表的链式储存方式
# include<iostream>
# include<malloc.h>
using namespace std;
typedef struct LNode * PLNode;
typedef int ElementType;
typedef int Position;
struct LNode
{
ElementType Data;
PLNode Next;
};
typedef PLNode List;
List CreateList()
{
List L = (List)malloc(sizeof(struct LNode));
L = NULL;
return L;
}
int Length(List L)
{
int count = 0;
while (L)
{
L = L->Next;
++count;
}
return count;
}
List FindK(int K, List L)
{
int i = 1;
List P = L;
while (P && i < K)
{
P = P->Next;
++i;
}
if (i == K)
return P;
else
{
cout << "查找失败" << endl;
return NULL;
}
}
List Insert(ElementType X, int i, List L)
{
List P = L;
List s,p;
p = (List)malloc(sizeof(struct LNode));
p->Data = X;
p->Next = NULL;
if (i == 1)
{
p->Next = P;
return p;
}
s = FindK(i - 1, P);
if(s == NULL)
{
cout << "插入位置有误" << endl;
return NULL;
}
else
{
p->Next = s->Next;
s->Next = p;
}
return P;
}
int Findx(ElementType X, List L)
{
int i = 1;
List P = L;
while (P && P->Data != X)
{
P = P->Next;
++i;
}
if (P)
return i;
else
cout << "查找失败" << endl;
return 0;
}
List Delete(int i, List L)
{
List tmp, s;
if (i == 1)
{
tmp = L;
if (L)
L = L->Next;
else
return NULL;
free(tmp);
return L;
}
s = FindK(i-1, L);
if (!s || !(s->Next))
{
cout << "释放的位置节点错误" << endl;
return NULL;
}
else
{
tmp = s->Next;
s->Next = tmp->Next;
free(tmp);
return L;
}
}
void print(List L)
{
while (L)
{
cout << L->Data << " ";
L = L->Next;
}
cout << endl;
}
int main()
{
List L = CreateList();
int i = 0;
List P;
L = Insert(22, 1, L);
L = Insert(50, 1, L);
L = Insert(64, 1, L);
L = Insert(70, 2, L);
L = Insert(55, 4, L);
int len = Length(L);
cout << "链表长度为:" << len << endl;
print(L);
P = FindK(3, L);
cout << "第3个结点元素为:" << P->Data << endl;
P = FindK(7, L);
Position pt = Findx(22, L);
cout << "22这个元素在第" << pt << "结点" << endl;
pt = Findx(80, L);
L = Delete(2, L);
cout << "删除第二个结点后的链表排序:" << endl;
print(L);
L = Delete(7, L);
return 0;
}
|