一、线性表
线性表(linear list)是n个具有相同特性的数据元素的有限序列。 线性表是一种在实际中广泛使用的数据结构,常见的线性表:顺序表、链表、栈、队列、字符串…
线性表在逻辑上是线性结构,也就说是连续的一条直线。但是在物理结构上并不一定是连续的,线性表在物理上存储时,通常以数组和链式结构的形式存储。
二、顺序表
顺序表是用一段物理地址连续的存储单元依次存储数据元素的线性结构,一般情况下采用数组存储。在数组上完成数据的增删查改。
顺序表一般可以分为:
-
静态顺序表:使用定长数组存储元素。
-
动态顺序表:使用动态开辟的数组存储。
1.顺序表的接口
代码如下(示例)SeqList.h:
#pragma once
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
typedef int SeqDateType;
typedef struct SeqList
{
SeqDateType* a;
int size; //有效数据的个数
int Capacity;//表的容量
}SeqList;
void SeqListv(SeqList* seq);//检查容量
void SeqListInit(SeqList* seq);//初始化
void SeqListDestory(SeqList* seq);//销毁
void SeqListpushback(SeqList* seq, SeqDateType x);//尾插
void SeqListpushFront(SeqList* seq, SeqDateType x);//首插
void SeqListPopback(SeqList* seq);//尾删
void SeqListPopFront(SeqList* seq);//首删
int SeqListFind(SeqList* seq, SeqDateType x); //顺序表查找
void SeqListErase(SeqList* seq, size_t pos);// 顺序表删除pos位置的值
void SeqListInsert(SeqList* seq, size_t pos, SeqDateType x);// 顺序表在pos位置插入x
2.各个接口的实现
SeqList.c 代码如下(示例):
#define _CRT_SECURE_NO_WARNINGS 1
#include "SeqList.h"
//初始化
void SeqListInit(SeqList* seq)
{
seq->a = NULL;
seq->size = 0;
seq->Capacity = 0;
}
//销毁
void SeqListDestory(SeqList* seq)
{
assert(seq);
free(seq->a);
seq->a = NULL;
seq->size = 0;
seq->Capacity = 0;
}
//尾插
void SeqListpushback(SeqList* seq, SeqDateType x)
{
assert(seq);
SeqListv(seq);//检查容量
seq->a[seq->size] = x;
seq->size++;
}
//首插
void SeqListpushFront(SeqList* seq, SeqDateType x)
{
assert(seq);
SeqListv(seq);//检查容量
int i = 0;
//将顺序表中的数据往后移
for (i = seq->size - 1; i >= 0; i--)
{
seq->a[i+1] = seq->a[i];
}
seq->a[0] = x;
seq->size++;
}
//尾删
void SeqListPopback(SeqList* seq)
{
assert(seq);
seq->size--;//只需要将有效数据个数数目减一
}
//头删
void SeqListPopFront(SeqList* seq)
{
assert(seq);//直接简单粗暴的判断seq是否为空
int end = seq->size - 1;
int i = 0;
while (i < end)
{
seq->a[i] = seq->a[i + 1];
i++;
}
seq->size--;
}
//检查容量
void SeqListv(SeqList* seq)
{
assert(seq);
if (seq->Capacity == seq->size)
{
int newCapacity = seq->Capacity == 0 ? 4 : seq->Capacity * 2;
SeqDateType* newA = (SeqDateType*)realloc(seq->a, sizeof(SeqDateType) * newCapacity);
if (newA != NULL)
{
seq->a = newA;
seq->Capacity = newCapacity;
return;
}
else
{
printf("检查容量有误\n");
}
}
}
//顺序表的查找
int SeqListFind(SeqList* seq, SeqDateType x)
{
assert(seq);
int index= 0;//从下标为0开始找
for (index = 0; index < seq->size; index++)
{
if (seq->a[index] == x)
{
//找到以后返回下标
return index;
}
}
//没找到返回-1
return -1;
}
// 顺序表在pos位置插入x
void SeqListInsert(SeqList* seq, size_t pos, SeqDateType x)
{
assert(seq);
SeqListv(seq);
size_t index = 0;
//将pos位置和以后的数据往后移动
for (index = seq->size+1; index >= pos; index--)
{
seq->a[index ] = seq->a[index-1];
}
//在pos处插入数据
seq->a[pos-1] = x;
seq->size++;
}
// 顺序表删除pos位置的值
void SeqListErase(SeqList* seq, size_t pos)
{
assert(seq);
size_t index = 0;
//将pos位置和以后的数据往前移动
for (index = pos-1; index < seq->size-1; index++ )
{
seq->a[index] = seq->a[index + 1];
}
seq->size--;
}
//打印顺序表内容
void SLprint(SeqList* arr)
{
assert(arr&&arr->a);
int i = 0;
for (i = 0; i < arr->size; ++i)
{
printf("%d ", arr->a[i]);
}
printf("\n");
}
3.测试代码
代码如下:
#define _CRT_SECURE_NO_WARNINGS 1
#include "SeqList.h"
int main()
{
SeqList arr;
SeqListInit(&arr);
//尾插
printf("尾插测试:\n");
SeqListpushback(&arr, 1);
SeqListpushback(&arr, 2);
SeqListpushback(&arr, 3);
SeqListpushback(&arr, 4);
SeqListpushback(&arr, 5);
SeqListpushback(&arr, 6);
SLprint(&arr);
printf("\n\n");
//头插测试
printf("头插测试:\n");
SeqListpushFront(&arr, 6);
SeqListpushFront(&arr, 9);
SeqListpushFront(&arr, 12);
SLprint(&arr);
printf("\n\n");
//尾删测试
printf("尾删测试:\n");
SeqListPopback(&arr);
SeqListPopback(&arr);
SLprint(&arr);
printf("\n\n");
//头删测试
printf("头删测试:\n");
SeqListPopFront(&arr);
SLprint(&arr);
printf("\n\n");
//顺序表查找
printf("顺序表查找数字3的位置:\n");
int m=SeqListFind(&arr, 3);
SLprint(&arr);
printf("%d\n\n", m);
// 顺序表在pos位置插入x测试
printf("顺序表在2位置插入10:\n");
SeqListInsert(&arr, 2, 10);
SLprint(&arr);
printf("\n\n");
// 顺序表删除pos位置的值测试
printf("顺序表删除2位置的值:\n");
SeqListErase(&arr, 2);
SLprint(&arr);
return 0;
}
三 链表
3.1 链表的概念及结构
链表是一种物理存储结构上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的 。
![在这里插入图片描述](https://img-blog.csdnimg.cn/34b186df12284a20b62e4a759837
8d1.png)
3.2 链表的分类
- 单向或者双向
- 带头或者不带头
- 循环或者非循环
这三种情况可组合成8种链表结构
在这里插入图片描述,我们日常生活中用的最多的是以下情况的链表
3.3无头单向非循环链表的实现
3.3.1无头单向非循环链表的接口
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
// slist.h
typedef int SLTDateType;
typedef struct SListNode
{
SLTDateType data;
struct SListNode* next;
}SListNode;
// 动态申请一个节点
SListNode* BuySListNode(SLTDateType x);
// 单链表打印
void SListPrint(SListNode* plist);
// 单链表尾插
void SListPushBack(SListNode** pplist, SLTDateType x);
// 单链表的头插
void SListPushFront(SListNode** pplist, SLTDateType x);
// 单链表的尾删
void SListPopBack(SListNode** pplist);
// 单链表头删
void SListPopFront(SListNode** pplist);
// 单链表查找
SListNode* SListFind(SListNode* plist, SLTDateType x);
// 单链表在pos位置之后插入x
// 分析思考为什么不在pos位置之前插入?
void SListInsertAfter(SListNode* pos, SLTDateType x);
// 单链表删除pos位置之后的值
// 分析思考为什么不删除pos位置?
void SListEraseAfter(SListNode* pos);
// 单链表的销毁
void SListDestory(SListNode* plist);
3.3.2各个接口的实现
#define _CRT_SECURE_NO_WARNINGS 1
#include "SList.h"
// 动态申请一个节点
SListNode* BuySListNode(SLTDateType x)
{
SListNode* NewNode = (SListNode*)malloc( sizeof(SListNode));
assert(NewNode);
NewNode->data = x;
NewNode->next = NULL;
return NewNode;
}
// 单链表打印
void SListPrint(SListNode* plist)
{
SListNode* cur = plist;
if (cur == NULL)
{
printf("链表为NULL\n");
}
while (cur->next != NULL)
{
printf("%d->",cur->data);
cur = cur->next;
}
printf("%d->NULL",cur->data);
}
// 单链表尾插
void SListPushBack(SListNode** pplist, SLTDateType x)
{
SListNode* NewNode = BuySListNode(x);
assert(NewNode);
SListNode* cur = *pplist;
if (cur== NULL)
{
cur = NewNode;
}
else
{
//找尾结点
while (cur->next != NULL)
{
cur = (cur)->next;
}
cur->next = NewNode;
}
}
// 单链表的头插
void SListPushFront(SListNode** pplist, SLTDateType x)
{
SListNode* NewNode = BuySListNode(x);
assert(NewNode);
NewNode->next = *pplist;
*pplist = NewNode;
}
// 单链表的尾删
void SListPopBack(SListNode** pplist)
{
assert(*pplist);
//只有一个节点
if ((*pplist)->next == NULL)
{
free(*pplist);
*pplist = NULL;
}
else
{
SListNode* cur = *pplist;
SListNode* prev = *pplist;
while (cur->next != NULL)
{
prev = cur;
cur = cur->next;
}
prev->next = NULL;
free(cur);
}
}
// 单链表头删
void SListPopFront(SListNode** pplist)
{
assert(pplist);
if ((*pplist)->next == NULL)
{
return;
}
SListNode* next = (*pplist)->next;
free(*pplist);
*pplist = next;
}
// 单链表查找
SListNode* SListFind(SListNode* plist, SLTDateType x)
{
assert(plist);
SListNode* cur = plist;
while (cur)
{
if (cur->data == x)
{
return cur;
}
else
{
cur = cur->next;
}
}
return NULL;//遍历链表都没找到,返回NULL
}
// 单链表在pos位置之后插入x
// 分析思考为什么不在pos位置之前插入?
void SListInsertAfter(SListNode* pos, SLTDateType x)
{
assert(pos);
SListNode* newNode = BuySListNode(x);
newNode->next = pos->next;
pos->next = newNode;
}
// 单链表删除pos位置之后的值
// 分析思考为什么不删除pos位置?
void SListEraseAfter(SListNode* pos)
{
assert(pos);
if (pos->next == NULL)//pos->next为空说明后面无可删除的位置
{
return;
}
SListNode* del = pos->next;//找要删除的节点
SListNode* net = pos->next->next;//找下下个节点
free(del);
pos->next = net;
}
// 单链表的销毁
void SListDestory(SListNode* plist)
{
assert(plist);
SListNode* cur = plist;
while (cur != NULL)
{
plist = plist->next;//找下一个节点
free(cur);//销毁当前节点
cur = plist;
}
printf("链表已销毁\n");
}
3.3.3 代码测试
#include "SList.h"
void TestList2()
{
SListNode* n1 = BuySListNode(1);
SListNode* n2 = BuySListNode(2);
SListNode* n3 = BuySListNode(3);
SListNode* n4 = BuySListNode(4);
n1->data = 1;
n2->data = 2;
n3->data = 3;
n4->data = 4;
n1->next = n2;
n2->next = n3;
n3->next = n4;
n4->next = NULL;
// 单链表尾插
printf("尾插测试\n");
SListPushBack(&n1, 5);
SListPushBack(&n1, 2);
SListPushBack(&n1, 6);
SListPrint(n1);
printf("\n\n");
//头插测试
printf("头插测试\n");
SListPushFront(&n1, 0);
SListPrint(n1);
printf("\n\n");
printf("尾删测试\n");
SListPopBack(&n1);
SListPrint(n1);
printf("\n\n");
printf("头删测试\n");
SListPopFront(&n1);
SListPrint(n1);
printf("\n\n");
printf("查找测试:查找3的位置\n");
SListNode* tmp=SListFind(n1, 3);
if (tmp != NULL)
{
printf("找到了\n");
}
printf("\n\n");
//在n3后插入一个数
printf("在n3后插入一个数\n");
SListInsertAfter(n3, 9);
SListPrint(n1);
printf("\n\n");
//删除pos位置后的值
printf("删除n2位置后的值\n");
SListEraseAfter(n2);
SListPrint(n1);
printf("\n\n");
SListDestory(n1);
}
int main()
{
TestList2();
return 0;
}
运行结果
3.4带头双向循环链表的实现
3.4.1带头双向循环链表的接口
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
typedef int LTDataType;
typedef struct ListNode
{
LTDataType _data;
struct ListNode* next;
struct ListNode* prev;
}ListNode;
// 创建返回链表的头结点
ListNode* ListCreate();
//创建一个新节点
ListNode* ListnewNode(LTDataType x);
// 双向链表销毁
void ListDestory(ListNode* plist);
// 双向链表打印
void ListPrint(ListNode* plist);
// 双向链表尾插
void ListPushBack(ListNode* plist, LTDataType x);
// 双向链表尾删
void ListPopBack(ListNode* plist);
// 双向链表头插
void ListPushFront(ListNode* plist, LTDataType x);
// 双向链表头删
void ListPopFront(ListNode* plist);
// 双向链表查找
ListNode* ListFind(ListNode* plist, LTDataType x);
// 双向链表在pos的前面进行插入
void ListInsert(ListNode* pos, LTDataType x);
// 双向链表删除pos位置的结点
void ListErase(ListNode* pos);
3.4.2带头双向循环链表接口的实现
#define _CRT_SECURE_NO_WARNINGS 1
#include "List.h"
// 创建返回链表的头结点.
ListNode* ListCreate()
{
ListNode* headNode = (ListNode*)malloc(sizeof(ListNode));
assert(headNode);
headNode->next = headNode;
headNode->prev = headNode;
return headNode;
}
//创建一个新节点
ListNode* ListnewNode(LTDataType x)
{
ListNode* newNode = (ListNode*)malloc(sizeof(ListNode));
assert(newNode);
newNode->_data = x;
newNode->next = NULL;
newNode->prev = NULL;
return newNode;
}
// 双向链表销毁
void ListDestory(ListNode* plist)
{
assert(plist);
ListNode* cur = plist->next;
while (cur != plist)
{
ListNode* next = cur->next;
free(cur);
cur = next;
}
free(plist);
}
// 双向链表打印
void ListPrint(ListNode* plist)
{
ListNode* cur = plist->next;
if (cur == NULL)
{
return ;
}
while (cur != plist)
{
printf("%3d", cur->_data);
cur = cur->next;
}
}
// 双向链表尾插
void ListPushBack(ListNode* plist, LTDataType x)
{
ListNode* Tail = plist->prev;
ListNode* newTail = ListnewNode(x);
Tail->next = newTail;
newTail->prev = Tail;
newTail->next = plist;
plist->prev = newTail;
}
// 双向链表尾删
void ListPopBack(ListNode* plist)
{
assert(plist);
if (plist->prev == plist)
{
return;
}
ListNode* Tail = plist->prev;
ListNode* newTail = Tail->prev;
newTail->next = plist;
plist->prev = newTail;
free(Tail);
return;
}
// 双向链表头插
void ListPushFront(ListNode* plist, LTDataType x)
{
assert(plist);
ListNode* head = plist->next;
ListNode* newhead = ListnewNode(x);
if (newhead == NULL)
{
perror("ListnewNode:");
}
newhead->prev = plist;
newhead->next = head;
head->prev = newhead;
plist->next = newhead;
}
// 双向链表头删
void ListPopFront(ListNode* plist)
{
assert(plist);
if (plist->next == NULL)
{
return;
}
ListNode* head = plist->next;
ListNode* newhead = plist->next->next;
plist->next = newhead;
newhead->prev = plist;
free(head);
}
// 双向链表查找
ListNode* ListFind(ListNode* plist, LTDataType x)
{
assert(plist);
ListNode* cur = plist->next;
while (cur != plist)
{
if (cur->_data == x)
{
return cur;
}
else
{
cur = cur->next;
}
}
return NULL;
}
// 双向链表在pos的前面进行插入
void ListInsert(ListNode* pos, LTDataType x)
{
assert(pos);
ListNode* pre = pos->prev;
ListPushFront(pre, x);
}
// 双向链表删除pos位置的结点
void ListErase(ListNode* pos)
{
assert(pos);
ListNode* pre = pos->prev;
ListNode* net = pos->next;
pre->next = net;
net->prev = pre;
free(pos);
}
4.总结
4.1顺序表的优缺点
顺序表的优点:
1.结构简单,尾插尾删时间复杂度为O(1);
2.可以随机访问;
3.缓存命中率较高(相对于链式),物理空间连续,预加载有优势;
顺序表缺点:
1.中间和头部的插入和删除的时间复杂度为O(N);
2.增容需要增长空间,就会需要拷贝数据,会产生消耗;
3.空间的浪费:扩容一般以两倍的速度扩容,这是因为不会因为扩容太小而频繁扩容,也不至于太大浪费太多空间,但是在整体上来说还是会有一定的空间浪费;
适用场景:
适合频繁访问的场景;
4.2链表的优缺点
链表的优点:
1.不会造成空间浪费,用一个节点开辟一个节点;
2.双向链表在任意位置的插入删除都是O(1);
链表的缺点:
1.以节点为存储单位,不支持随机访问;
2.非连续结构,结构较为复杂;
3.缓存命中率低,并且容易造成内存碎片;
适用场景:
频繁插入删除的场景;