一.线性表
1.线性表
线性表(linear list)是n个具有相同特性的数据元素的有限序列。 线性表是一种在实际中广泛使 用的数据结构,常见的线性表:顺序表、链表、栈、队列、字符串...
线性表在逻辑上是线性结构,也就说是连续的一条直线。但是在物理结构上并不一定是连续的, 线性表在物理上存储时,通常以数组和链式结构的形式存储。
二.顺序表
2.1概念
顺序表是用一段物理地址连续的存储单元依次存储数据元素的线性结构,一般情况下采用数组存 储。在数组上完成数据的增删查改。
2.2 分类
2.2.1.静态顺序表:使用定长数组存储元素。
#define N 100
typedef int SLDataType;
typedef struct SeqList
{
SLDataType array[N];//定长数组
size_t size; //有效数据个数
}SeqList;
2.2.2.动态顺序表:使用动态开辟的数组存储。
typedef struct SeqList
{
SLDataType* array;//指向动态开辟的数组
size_t size; //有效数据个数
size_t capacity; //容量空间的大小
}SeqList;
2.2.3.动态顺序表实现
#include "SeqList.h"
#include <malloc.h>
#include <assert.h>
//对顺序表进行初始化
void SeqListInit(SeqList* ps)
{
assert(ps);
ps->array = (DataType*)malloc(3 * sizeof(DataType));
if (NULL == ps->array)
{
printf("SeqList:malloc空间失败!!!\n");
exit(0);
}
ps->capacity = 3;
ps->size = 0;
}
//对顺序表进行销毁
void SeqListDestroy(SeqList* ps)
{
assert(ps);
if (ps->array);
{
free(ps->array);
ps->array = NULL;
ps->capacity = 0;
ps->size = 0;
}
}
//检查空间,如果满了,进行扩容
void CheckCapacity(SeqList* ps)
{
assert(ps);
if (ps->capacity == ps->size)//进行扩容
{
int newcapacity = 2 * ps->capacity;
DataType* temp = (DataType*)realloc(ps->array,sizeof(DataType)*newcapacity);
if (temp == NULL)
{
printf("扩容失败!!!\n");
exit(0);
}
ps->array = temp;
ps->capacity = newcapacity;
}
}
/*
void CheckCapacity(SeqList* ps)
{
assert(ps);
if (ps->capacity == ps->size)//进行扩容
{
int newcapacity = 2 * ps->capacity;
DataType* temp = (DataType*) malloc(sizeof(DataType)*newcapacity);
if (temp == NULL)
{
printf("扩容失败!!!\n");
exit(0);
}
for (int i = 0; i < ps->size; ++i)
{
temp[i] = ps->array[i];
}
free(ps->array);
ps->array = temp;
ps->capacity = newcapacity;
}
}
*/
//顺序表尾插
void SeqListPushBack(SeqList* ps, DataType x)
{
assert(ps);
CheckCapacity(ps);
ps->array[ps->size] = x;
ps->size++;
}
//顺序表尾删
void SeqListPopBack(SeqList* ps)
{
if (SeqListEmpty(ps))
{
return;
}
ps->size--;
}
//顺序表查找
int SeqListFind(SeqList* ps, DataType x)
{
assert(ps);
for (int i = 0; i < ps->size; ++i)
{
if (ps->array[i] == x)
{
return i;
}
}
return -1;
}
//顺序表在pos位置插入x
void SeqListInsert(SeqList* ps, int pos, DataType x)
{
assert(ps);
if (pos<0||pos > ps->size)
{
printf("SeqListInsert:越界了!!!\n");
exit(0);
}
CheckCapacity(ps);
for (int i = ps->size-1;i>=pos; i--)
{
ps->array[i+1]= ps->array[i];
}
ps->array[pos] = x;
ps->size++;
}
//顺序表删除pos位置的值
void SeqListErase(SeqList* ps, size_t pos)
{
assert(ps);
if (pos >= ps->size)
{
printf("SeqListErase:非法!!!\n");
exit(0);
}
for (int i = pos + 1; i < ps->size; ++i)
{
ps->array[i-1] = ps->array[i];
}
ps->size--;
}
//获取任意位置的元素
DataType SeqListAt(SeqList* ps, size_t pos)
{
assert(ps);
assert(pos < ps->size);
return ps->array[pos];
}
//获取起始位置的元素
DataType SeqListFront(SeqList* ps)
{
assert(ps);
return ps->array[0];
}
//获取末尾位置的元素
DataType SeqListBack(SeqList* ps)
{
assert(ps);
return ps->array[ps->size - 1];
}
//获取有效元素的个数
size_t SeqListSize(SeqList* ps)
{
assert(ps);
return ps->size;
}
//获取空间的容量
size_t SeqListCapacity(SeqList* ps)
{
assert(ps);
return ps->capacity;
}
//检查顺序表是否为空,是空返回真,否则返回假
size_t SeqListEmpty(SeqList* ps)
{
assert(ps);
return ps->size == 0;
}
三.链表
3.1概念及结构
概念:链表是一种物理存储结构上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表 中的指针链接次序实现的 。
3.2链表的分类
3.2.1.单向或者双向
3.2.2.带头或者不带头
3.2.3.循环或者非循环
常用的两种结构:
3.3链表的实现
不带头的单链表
#include"Slist.h"
#include<stdio.h>
#include<malloc.h>
#include<assert.h>
//创建结点
SListNode* BuySListNode(DataType x)
{
SListNode* newNode = (SListNode*)malloc(sizeof(SListNode));
if (NULL == newNode)
{
printf("创建结点失败!\n");
exit(0);
}
newNode->data = x;
newNode->next = NULL;
return newNode;
}
//单链表尾插
void SListPushBack(SListNode** pplist, DataType x)
{
assert(pplist);//断言链表是否存在
if (NULL == *pplist)
{
*pplist = BuySListNode(x);
}
else
{ //找到链表中的最后一个结点
SListNode* cur = *pplist;
while (cur->next)
{
cur = cur->next;
}
//插入新结点
cur->next = BuySListNode(x);
}
}
//单链表尾删
void SListPopBack(SListNode** pplist)
{
assert(pplist);
if (NULL == *pplist)
{
return;
}
else if (NULL == (*pplist)->next)//2.只有一个结点
{
free(*pplist);
*pplist = NULL;
}
else //3.有两个以上结点
{
SListNode* cur = *pplist;
SListNode* prev = NULL;
while (cur->next)
{
prev = cur;
cur = cur->next;
}
free(cur);
prev->next = NULL;
}
}
//单链表头插
void SListPushFront(SListNode** pplist, DataType x)
{
assert(pplist);
SListNode* newNode = BuySListNode(x);
newNode->next = *pplist;
*pplist = newNode;
}
//单链表头删
void SListPopFront(SListNode** pplist)
{
assert(pplist);
/*
if (NULL == *pplist)
{
return;
}
else if (NULL == (*pplist)->next)
{
free(*pplist);
*pplist = NULL;
}
else
{
SListNode* delNode = *pplist;
*pplist = delNode->next;
free(delNode);
}
*/
SListNode* delNode = NULL;
if (NULL == *pplist)
return;
delNode = *pplist;
*pplist = delNode->next;
free(delNode);
}
//单链表查找
SListNode* SListFind(SListNode* plist, DataType x)
{
SListNode* cur=plist;
while (cur)
{
if (cur->data == x)
{
return cur;
}
cur = cur->next;
}
}
//单链表pos位置之后插入x
void SListInsertAfter(SListNode* pos, DataType x)
{
SListNode* newNode = NULL;
if (NULL == pos)
{
return;
}
newNode = BuySListNode(x);
newNode->next = pos->next;
pos->next = newNode;
}
//单链表删除pos位置之后的值
void SListEraseAfter(SListNode* pos)
{
SListNode* delNode = NULL;
if (NULL == pos || NULL == pos->next)
return;
delNode = pos->next;
pos->next = delNode->next;
free(delNode);
}
//单链表中的结点数
int SListSize(SListNode* plist)
{
int count = 0;
SListNode* cur = plist;
while (cur)
{
count++;
cur = cur->next;
}
return count;
}
//销毁链表
void SListDestroy(SListNode** pplist)
{
assert(pplist);
SListNode* cur = *pplist;
while (cur)
{
*pplist = (*pplist)->next;
free(cur);
cur = *pplist;
}
}
双向循环链表
#include"Dlist9.12.h"
#include <stdlib.h>
#include <assert.h>
DListNode* BuyDListNode(DataType data)
{
DListNode* newNode = (DListNode*)malloc(sizeof(DListNode));
if (NULL == newNode)
{
assert(0);
return NULL;
}
newNode->data = data;
newNode->next = NULL;
newNode->prev = NULL;
return newNode;
}
void DListInit(DListNode** head)
{
assert(head);
*head = BuyDListNode(0);
(*head)->next = *head;
(*head)->prev = *head;
}
void DListPushBack(DListNode* head, DataType data)
{
DListInsert(head, data);
}
void DListPopBack(DListNode* head)
{
if (DListEmpty(head))
return;
DListErase(head->prev);
}
void DListPushFront(DListNode* head, DataType data)
{
DListInsert(head->next,data);
}
void DListPopFront(DListNode* head)
{
if (DListEmpty(head))
return;
DListErase(head->next);
}
DListNode* DListFind(DListNode* head, DataType data)
{
DListNode* cur = head->next;
while (cur != head)
{
if (cur->data == data)
return cur;
cur = cur->next;
}
return NULL;
}
void DListInsert(DListNode* pos, DataType data)
{
if ( NULL==pos)
{
return;
}
DListNode* newNode = BuyDListNode(data);
newNode->prev = pos->prev;
newNode->next = pos;
newNode->prev->next = newNode;
pos->prev = newNode;
}
void DListErase(DListNode* pos)
{
if (NULL == pos)
return;
pos->prev->next = pos->next;
pos->next->prev = pos->prev;
free(pos);
}
int DListSize(DListNode* head)
{
DListNode* cur = head->next;
int count = 0;
while (cur != head)
{
count++;
cur = cur->next;
}
return count;
}
int DListEmpty(DListNode* head)
{
assert(head);
return head->next == head;
}
// 清空链表中的所有有效节点
void DListClear(DListNode* head)
{
DListNode* cur = head->next;
while (cur != head)
{
head->next = cur->next;
free(cur);
cur = head->next;
}
head->next = head;
head->prev = head;
}
void DListDestroy(DListNode** head)
{
assert(head);
DListClear(*head);
free(*head);
*head = NULL;
}
四.顺序表和链表的区别
不同点 | 顺序表 | 链表 |
存储空间 | 物理上一定连续 | 逻辑上连续,但物理上不一定 连续 |
随机访问 | 支持O(1) | 不支持:O(N) |
任意位置插入或者删除 元素 | 可能需要搬移元素,效率低O(N) | 只需修改指针指向 |
插入 | 动态顺序表,空间不够时需要扩容 | 没有容量的概念 |
应用场景 | 元素高效存储+频繁访问 | 任意位置插入和删除频繁 |
缓存利用率 | 高 | 低 |