线性表是逻辑结构,线性表和链表是存储结构、且是具有 相同数据类型 的 有限序列。
线性表
- 顺序表
任一数组元素都可以随机存取的随机存取结构,随机访问,结点只存数据元素,插入删除时需要移动大量元素
sizeof()是每个数组元素所占存储空间的大小
一维数组也可以动静态分配内存,动态分配存储时不是联试存储,只是分配的空间大小在运行时动态决定。
顺序表基本运算算法
//顺序表基本运算算法
#include <stdio.h>
#include <malloc.h>
#define MaxSize 50
typedef int ElemType;
typedef struct
{ ElemType data[MaxSize]; //存放顺序表元素
int length; //存放顺序表的长度
} SqList; //顺序表的类型
void CreateList(SqList *&L,ElemType a[],int n)
//建立顺序表
{
L=(SqList *)malloc(sizeof(SqList));
for (int i=0;i<n;i++)
L->data[i]=a[i];
L->length=n;
}
//初始化顺序表
void InitList(SqList *&L)
{
L=(SqList *)malloc(sizeof(SqList)); //分配存放线性表的空间
L->length=0;
}
//销毁顺序表
void DestroyList(SqList *&L)
{
free(L);
}
//判断线性表是否为空
bool ListEmpty(SqList *L)
{
return(L->length==0);
}
//求线性表的长度
int ListLength(SqList *L)
{
return(L->length);
}
//输出线性表
void DispList(SqList *L)
{
for (int i=0;i<L->length;i++)
printf("%d ",L->data[i]);
printf("\n");
}
//求线性表中的某个数据元素值
bool GetElem(SqList *L,int i,ElemType &e)
{
if (i<1 || i>L->length)
return false;
e=L->data[i-1];
return true;
}
//按元素值查找
int LocateElem(SqList *L, ElemType e)
{
int i=0;
while (i<L->length && L->data[i]!=e) i++;
if (i>=L->length)
return 0;
else
return i+1;
}
//插入数据元素
bool ListInsert(SqList *&L,int i,ElemType e)
{
int j;
if (i<1 || i>L->length+1)
return false;
i--; //将顺序表位序转化为elem下标
for (j=L->length;j>i;j--) //将data[i]及后面元素后移一个位置
L->data[j]=L->data[j-1];
L->data[i]=e;
L->length++; //顺序表长度增1
return true;
}
//删除数据元素
bool ListDelete(SqList *&L,int i,ElemType &e)
{
int j;
if (i<1 || i>L->length)
return false;
i--; //将顺序表位序转化为elem下标
e=L->data[i];
for (j=i;j<L->length-1;j++) //将data[i]之后的元素前移一个位置
L->data[j]=L->data[j+1];
L->length--; //顺序表长度减1
return true;
}
- 链表
可以反映数据之间的逻辑关系
单链表访问前驱节点的时间复杂度为O(n),因为只能按前后顺序查找;
单链表访问后继结点的时间复杂度为O(1)
单链表的长度不包括头结点
头插法和尾插法示例图
双链表
循环单链表
循环双链表
静态链表
单链表基本运算算法
//单链表基本运算算法
#include <stdio.h>
#include <malloc.h>
typedef int ElemType;
typedef struct LNode
{
ElemType data;
struct LNode *next; //指向后继结点
} LinkNode; //声明单链表结点类型
//头插法建立单链表
void CreateListF(LinkNode *&L,ElemType a[],int n)
{
LinkNode *s;
L=(LinkNode *)malloc(sizeof(LinkNode)); //创建头结点
L->next=NULL;
for (int i=0;i<n;i++)
{
s=(LinkNode *)malloc(sizeof(LinkNode));//创建新结点s
s->data=a[i];
s->next=L->next; //将结点s插在原开始结点之前,头结点之后
L->next=s;
}
}
//尾插法建立单链表
void CreateListR(LinkNode *&L,ElemType a[],int n)
{
LinkNode *s,*r;
L=(LinkNode *)malloc(sizeof(LinkNode)); //创建头结点
L->next=NULL;
r=L; //r始终指向终端结点,开始时指向头结点
for (int i=0;i<n;i++)
{
s=(LinkNode *)malloc(sizeof(LinkNode));//创建新结点s
s->data=a[i];
r->next=s; //将结点s插入结点r之后
r=s;
}
r->next=NULL; //终端结点next域置为NULL
}
//初始化
void InitList(LinkNode *&L)
{
L=(LinkNode *)malloc(sizeof(LinkNode)); //创建头结点
L->next=NULL;
}
//销毁单链表
void DestroyList(LinkNode *&L)
{
LinkNode *pre=L,*p=pre->next;
while (p!=NULL)
{ free(pre);
pre=p;
p=pre->next;
}
free(pre); //此时p为NULL,pre指向尾结点,释放它
}
//判断线性表是否为空
bool ListEmpty(LinkNode *L)
{
return(L->next==NULL);
}
//求线性表的长度
int ListLength(LinkNode *L)
{
LinkNode *p=L;int i=0;
while (p->next!=NULL)
{ i++;
p=p->next;
}
return(i);
}
//输出线性表
void DispList(LinkNode *L)
{
LinkNode *p=L->next;
while (p!=NULL)
{ printf("%d ",p->data);
p=p->next;
}
printf("\n");
}
//求线性表中的某个元素值
bool GetElem(LinkNode *L,int i,ElemType &e)
{
int j=0;
LinkNode *p=L;
if (i<=0) return false; //i错误返回假
while (j<i && p!=NULL)
{ j++;
p=p->next;
}
if (p==NULL) //不存在第i个数据结点
return false;
else //存在第i个数据结点
{ e=p->data;
return true;
}
}
//按元素值查找
int LocateElem(LinkNode *L,ElemType e)
{
LinkNode *p=L->next;
int n=1;
while (p!=NULL && p->data!=e)
{ p=p->next;
n++;
}
if (p==NULL)
return(0);
else
return(n);
}
//插入数据元素
bool ListInsert(LinkNode *&L,int i,ElemType e)
{
int j=0;
LinkNode *p=L,*s;
if (i<=0) return false; //i错误返回假
while (j<i-1 && p!=NULL) //查找第i-1个结点p
{ j++;
p=p->next;
}
if (p==NULL) //未找到位序为i-1的结点
return false;
else //找到位序为i-1的结点*p
{ s=(LinkNode *)malloc(sizeof(LinkNode));//创建新结点*s
s->data=e;
s->next=p->next; //将s结点插入到结点p之后
p->next=s;
return true;
}
}
//删除数据元素
bool ListDelete(LinkNode *&L,int i,ElemType &e)
{
int j=0;
LinkNode *p=L,*q;
if (i<=0) return false; //i错误返回假
while (j<i-1 && p!=NULL) //查找第i-1个结点
{ j++;
p=p->next;
}
if (p==NULL) //未找到位序为i-1的结点
return false;
else //找到位序为i-1的结点p
{ q=p->next; //q指向要删除的结点
if (q==NULL)
return false; //若不存在第i个结点,返回false
e=q->data;
p->next=q->next; //从单链表中删除q结点
free(q); //释放q结点
return true;
}
}
双链表基本运算算法
//双链表基本运算算法
#include <stdio.h>
#include <malloc.h>
typedef int ElemType;
typedef struct DNode //定义双链表结点类型
{
ElemType data;
struct DNode *prior; //指向前驱结点
struct DNode *next; //指向后继结点
} DLinkNode;
void CreateListF(DLinkNode *&L,ElemType a[],int n)
//头插法建双链表
{
DLinkNode *s;
L=(DLinkNode *)malloc(sizeof(DLinkNode)); //创建头结点
L->prior=L->next=NULL;
for (int i=0;i<n;i++)
{
s=(DLinkNode *)malloc(sizeof(DLinkNode));//创建新结点
s->data=a[i];
s->next=L->next; //将结点s插在原开始结点之前,头结点之后
if (L->next!=NULL) L->next->prior=s;
L->next=s;s->prior=L;
}
}
void CreateListR(DLinkNode *&L,ElemType a[],int n)
//尾插法建双链表
{
DLinkNode *s,*r;
L=(DLinkNode *)malloc(sizeof(DLinkNode)); //创建头结点
L->prior=L->next=NULL;
r=L; //r始终指向终端结点,开始时指向头结点
for (int i=0;i<n;i++)
{
s=(DLinkNode *)malloc(sizeof(DLinkNode));//创建新结点
s->data=a[i];
r->next=s;s->prior=r; //将结点s插入结点r之后
r=s;
}
r->next=NULL; //尾结点next域置为NULL
}
void InitList(DLinkNode *&L)
{
L=(DLinkNode *)malloc(sizeof(DLinkNode)); //创建头结点
L->prior=L->next=NULL;
}
void DestroyList(DLinkNode *&L)
{
DLinkNode *pre=L,*p=pre->next;
while (p!=NULL)
{
free(pre);
pre=p;
p=pre->next;
}
free(pre);
}
bool ListEmpty(DLinkNode *L)
{
return(L->next==NULL);
}
int ListLength(DLinkNode *L)
{
DLinkNode *p=L;
int i=0;
while (p->next!=NULL)
{
i++;
p=p->next;
}
return(i);
}
void DispList(DLinkNode *L)
{
DLinkNode *p=L->next;
while (p!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
printf("\n");
}
bool GetElem(DLinkNode *L,int i,ElemType &e)
{
int j=0;
DLinkNode *p=L;
if (i<=0) return false; //i错误返回假
while (j<i && p!=NULL)
{
j++;
p=p->next;
}
if (p==NULL)
return false;
else
{
e=p->data;
return true;
}
}
int LocateElem(DLinkNode *L,ElemType e)
{
int n=1;
DLinkNode *p=L->next;
while (p!=NULL && p->data!=e)
{
n++;
p=p->next;
}
if (p==NULL)
return(0);
else
return(n);
}
bool ListInsert(DLinkNode *&L,int i,ElemType e)
{
int j=0;
DLinkNode *p=L,*s;
if (i<=0) return false; //i错误返回假
while (j<i-1 && p!=NULL)
{
j++;
p=p->next;
}
if (p==NULL) //未找到第i-1个结点
return false;
else //找到第i-1个结点p
{
s=(DLinkNode *)malloc(sizeof(DLinkNode)); //创建新结点s
s->data=e;
s->next=p->next; //将结点s插入到结点p之后
if (p->next!=NULL)
p->next->prior=s;
s->prior=p;
p->next=s;
return true;
}
}
bool ListDelete(DLinkNode *&L,int i,ElemType &e)
{
int j=0;
DLinkNode *p=L,*q;
if (i<=0) return false; //i错误返回假
while (j<i-1 && p!=NULL)
{
j++;
p=p->next;
}
if (p==NULL) //未找到第i-1个结点
return false;
else //找到第i-1个结点p
{
q=p->next; //q指向要删除的结点
if (q==NULL)
return false; //不存在第i个结点
e=q->data;
p->next=q->next; //从单链表中删除*q结点
if (p->next!=NULL) p->next->prior=p;
free(q); //释放q结点
return true;
}
}
循环单链表基本运算算法
//循环单链表基本运算算法
#include <stdio.h>
#include <malloc.h>
typedef int ElemType;
typedef struct LNode //定义单链表结点类型
{
ElemType data;
struct LNode *next;
} LinkNode;
头插法建立循环单链表
void CreateListF(LinkNode *&L,ElemType a[],int n)
{
LinkNode *s;int i;
L=(LinkNode *)malloc(sizeof(LinkNode)); //创建头结点
L->next=NULL;
for (i=0;i<n;i++)
{
s=(LinkNode *)malloc(sizeof(LinkNode));//创建新结点
s->data=a[i];
s->next=L->next; //将结点s插在原开始结点之前,头结点之后
L->next=s;
}
s=L->next;
while (s->next!=NULL) //查找尾结点,由s指向它
s=s->next;
s->next=L; //尾结点next域指向头结点
}
//尾插法建立循环单链表
void CreateListR(LinkNode *&L,ElemType a[],int n)
{
LinkNode *s,*r;int i;
L=(LinkNode *)malloc(sizeof(LinkNode)); //创建头结点
L->next=NULL;
r=L; //r始终指向终端结点,开始时指向头结点
for (i=0;i<n;i++)
{
s=(LinkNode *)malloc(sizeof(LinkNode));//创建新结点
s->data=a[i];
r->next=s; //将结点s插入结点r之后
r=s;
}
r->next=L; //尾结点next域指向头结点
}
void InitList(LinkNode *&L)
{
L=(LinkNode *)malloc(sizeof(LinkNode)); //创建头结点
L->next=L;
}
void DestroyList(LinkNode *&L)
{
LinkNode *p=L,*q=p->next;
while (q!=L)
{
free(p);
p=q;
q=p->next;
}
free(p);
}
bool ListEmpty(LinkNode *L)
{
return(L->next==L);
}
int ListLength(LinkNode *L)
{
LinkNode *p=L;int i=0;
while (p->next!=L)
{
i++;
p=p->next;
}
return(i);
}
void DispList(LinkNode *L)
{
LinkNode *p=L->next;
while (p!=L)
{
printf("%d ",p->data);
p=p->next;
}
printf("\n");
}
bool GetElem(LinkNode *L,int i,ElemType &e)
{
int j=0;
LinkNode *p;
if (L->next!=L) //单链表不为空表时
{
if (i==1)
{
e=L->next->data;
return true;
}
else //i不为1时
{
p=L->next;
while (j<i-1 && p!=L)
{
j++;
p=p->next;
}
if (p==L)
return false;
else
{
e=p->data;
return true;
}
}
}
else //单链表为空表时
return false;
}
int LocateElem(LinkNode *L,ElemType e)
{
LinkNode *p=L->next;
int n=1;
while (p!=L && p->data!=e)
{
p=p->next;
n++;
}
if (p==L)
return(0);
else
return(n);
}
bool ListInsert(LinkNode *&L,int i,ElemType e)
{
int j=0;
LinkNode *p=L,*s;
if (p->next==L || i==1) //原单链表为空表或i==1时
{
s=(LinkNode *)malloc(sizeof(LinkNode)); //创建新结点s
s->data=e;
s->next=p->next; //将结点s插入到结点p之后
p->next=s;
return true;
}
else
{
p=L->next;
while (j<i-2 && p!=L)
{
j++;
p=p->next;
}
if (p==L) //未找到第i-1个结点
return false;
else //找到第i-1个结点p
{
s=(LinkNode *)malloc(sizeof(LinkNode)); //创建新结点s
s->data=e;
s->next=p->next; //将结点s插入到结点p之后
p->next=s;
return true;
}
}
}
bool ListDelete(LinkNode *&L,int i,ElemType &e)
{
int j=0;
LinkNode *p=L,*q;
if (p->next!=L) //原单链表不为空表时
{
if (i==1) //i==1时
{
q=L->next; //删除第1个结点
e=q->data;
L->next=q->next;
free(q);
return true;
}
else //i不为1时
{
p=L->next;
while (j<i-2 && p!=L)
{
j++;
p=p->next;
}
if (p==L) //未找到第i-1个结点
return false;
else //找到第i-1个结点p
{
q=p->next; //q指向要删除的结点
e=q->data;
p->next=q->next; //从单链表中删除q结点
free(q); //释放q结点
return true;
}
}
}
else return false;
}
循环双链表基本运算算法
//循环双链表基本运算算法
#include <stdio.h>
#include <malloc.h>
typedef int ElemType;
typedef struct DNode //定义双链表结点类型
{
ElemType data;
struct DNode *prior; //指向前驱结点
struct DNode *next; //指向后继结点
} DLinkNode;
void CreateListF(DLinkNode *&L,ElemType a[],int n)
{
DLinkNode *s;int i;
L=(DLinkNode *)malloc(sizeof(DLinkNode)); //创建头结点
L->next=NULL;
for (i=0;i<n;i++)
{
s=(DLinkNode *)malloc(sizeof(DLinkNode));//创建新结点
s->data=a[i];
s->next=L->next; //将结点s插在原开始结点之前,头结点之后
if (L->next!=NULL) L->next->prior=s;
L->next=s;s->prior=L;
}
s=L->next;
while (s->next!=NULL) //查找尾结点,由s指向它
s=s->next;
s->next=L; //尾结点next域指向头结点
L->prior=s; //头结点的prior域指向尾结点
}
//尾插法建立循环双链表
void CreateListR(DLinkNode *&L,ElemType a[],int n)
{
DLinkNode *s,*r;int i;
L=(DLinkNode *)malloc(sizeof(DLinkNode)); //创建头结点
L->next=NULL;
r=L; //r始终指向尾结点,开始时指向头结点
for (i=0;i<n;i++)
{
s=(DLinkNode *)malloc(sizeof(DLinkNode));//创建新结点
s->data=a[i];
r->next=s;s->prior=r; //将结点s插入结点r之后
r=s;
}
r->next=L; //尾结点next域指向头结点
L->prior=r; //头结点的prior域指向尾结点
}
void InitList(DLinkNode *&L)
{
L=(DLinkNode *)malloc(sizeof(DLinkNode)); //创建头结点
L->prior=L->next=L;
}
void DestroyList(DLinkNode *&L)
{
DLinkNode *p=L,*q=p->next;
while (q!=L)
{
free(p);
p=q;
q=p->next;
}
free(p);
}
bool ListEmpty(DLinkNode *L)
{
return(L->next==L);
}
int ListLength(DLinkNode *L)
{
DLinkNode *p=L;
int i=0;
while (p->next!=L)
{
i++;
p=p->next;
}
return(i);
}
void DispList(DLinkNode *L)
{
DLinkNode *p=L->next;
while (p!=L)
{
printf("%d ",p->data);
p=p->next;
}
printf("\n");
}
bool GetElem(DLinkNode *L,int i,ElemType &e)
{
int j=0;
DLinkNode *p;
if (L->next!=L) //双链表不为空表时
{
if (i==1)
{
e=L->next->data;
return true;
}
else //i不为1时
{
p=L->next;
while (j<i-1 && p!=L)
{
j++;
p=p->next;
}
if (p==L)
return false;
else
{
e=p->data;
return true;
}
}
}
else //双链表为空表时
return 0;
}
int LocateElem(DLinkNode *L,ElemType e)
{
int n=1;
DLinkNode *p=L->next;
while (p!=NULL && p->data!=e)
{
n++;
p=p->next;
}
if (p==NULL)
return(0);
else
return(n);
}
bool ListInsert(DLinkNode *&L,int i,ElemType e)
{
int j=0;
DLinkNode *p=L,*s;
if (p->next==L) //原双链表为空表时
{
s=(DLinkNode *)malloc(sizeof(DLinkNode)); //创建新结点s
s->data=e;
p->next=s;s->next=p;
p->prior=s;s->prior=p;
return true;
}
else if (i==1) //原双链表不为空表但i=1时
{
s=(DLinkNode *)malloc(sizeof(DLinkNode)); //创建新结点s
s->data=e;
s->next=p->next;p->next=s; //将结点s插入到结点p之后
s->next->prior=s;s->prior=p;
return true;
}
else
{
p=L->next;
while (j<i-2 && p!=L)
{ j++;
p=p->next;
}
if (p==L) //未找到第i-1个结点
return false;
else //找到第i-1个结点*p
{
s=(DLinkNode *)malloc(sizeof(DLinkNode)); //创建新结点s
s->data=e;
s->next=p->next; //将结点s插入到结点p之后
if (p->next!=NULL) p->next->prior=s;
s->prior=p;
p->next=s;
return true;
}
}
}
bool ListDelete(DLinkNode *&L,int i,ElemType &e)
{
int j=0;
DLinkNode *p=L,*q;
if (p->next!=L) //原双链表不为空表时
{
if (i==1) //i==1时
{
q=L->next; //删除第1个结点
e=q->data;
L->next=q->next;
q->next->prior=L;
free(q);
return true;
}
else //i不为1时
{
p=L->next;
while (j<i-2 && p!=NULL)
{
j++;
p=p->next;
}
if (p==NULL) //未找到第i-1个结点
return false;
else //找到第i-1个结点p
{
q=p->next; //q指向要删除的结点
if (q==NULL) return 0; //不存在第i个结点
e=q->data;
p->next=q->next; //从单链表中删除q结点
if (p->next!=NULL) p->next->prior=p;
free(q); //释放q结点
return true;
}
}
}
else return false; //原双链表为空表时
}