目录
- 顺序表
- 单链表
顺序表
类似于数组,元素都是相邻的,这也决定了它比较容易和比较适合查询。但缺点就是长度有限。
时间复杂度
- 查询操作 O(1)
- 插入和删除操作 O(n)
代码实现
#pragma once
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
typedef int SLDateType;
typedef struct SeqList
{
SLDateType* a;//动态顺序表
size_t size;
size_t capacity; // unsigned int
}SeqList;
// 对数据的管理:增删查改
void SeqListInit(SeqList* ps);
void SeqListDestory(SeqList* ps);
void SeqListPrint(SeqList* ps);
void SeqListPushBack(SeqList* ps, SLDateType x);
void SeqListPushFront(SeqList* ps, SLDateType x);
void SeqListPopFront(SeqList* ps);
void SeqListPopBack(SeqList* ps);
// 顺序表查找
int SeqListFind(SeqList* ps, SLDateType x);
// 顺序表在pos位置插入x
void SeqListInsert(SeqList* ps, size_t pos, SLDateType x);
// 顺序表删除pos位置的值
void SeqListErase(SeqList* ps, size_t pos);
void SeqListInit(SeqList* ps)
{
assert(ps);
ps->a = NULL;
ps->capacity = 0;
ps->size = 0;
}
void SeqListDestory(SeqList* ps)
{
assert(ps);
free(ps->a);
ps->a = NULL;
ps->capacity = 0;
ps->size = 0;
}
void SeqListPrint(SeqList* ps)
{
assert(ps);
for (int i = 0; i < ps->size; i++)
{
printf("%d", ps->a[i]);
}
printf("\n");
}
void SeqListCheckCapacity(SeqList* ps)
{
assert(ps);
if (ps->size == ps->capacity)
{
size_t newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
SLDateType* tmp = realloc(ps->a, sizeof(SLDateType) * newCapacity);
if (tmp == NULL)
{
printf("realloc fail\n");
exit(-1);
}
else
{
ps->a = tmp;
ps->capacity = newCapacity;
}
}
}
void SeqListPushBack(SeqList* ps, SLDateType x)
{
assert(ps);
SeqListCheckCapacity(ps);
ps->a[ps->size] = x;
ps->size++;
}
void SeqListPushFront(SeqList* ps, SLDateType x)
{
assert(ps);
SeqListCheckCapacity(ps);
int end = ps->size - 1;
while (end >= 0)
{
ps->a[end + 1] = ps->a[end];
--end;
}
ps->a[0] = x;
ps->size++;
}
void SeqListPopFront(SeqList* ps)
{
assert(ps);
if (ps->size > 0)
{
int begin = 1;
while (begin < ps->size)
{
ps->a[begin - 1] = ps->a[begin];
++begin;
}
--ps->size;
}
}
void SeqListPopBack(SeqList* ps)
{
assert(ps);
ps->a[ps->size - 1] = 0;
if(ps->size > 0)
{
ps->size;
}
}
int SeqListFind(SeqList* ps, SLDateType x)
{
assert(ps);
for (int i = 0; i < ps->size; i++)
{
if (ps->a[i] == x)
{
return i;
}
}
return -1;
}
void SeqListInsert(SeqList* ps, size_t pos, SLDateType x)
{
assert(ps);
if (pos > ps->size)
{
printf("数组越界");
return;
}
SeqListCheckCapacity(ps);
size_t end = ps->size;
while (end > pos)
{
ps->a[end] = ps->a[end - 1];
end--;
}
ps->a[pos] = x;
ps->size++;
}
void SeqListErase(SeqList* ps, size_t pos)
{
assert(ps);
assert(pos < ps->size);
size_t begin = pos + 1;
while (begin < ps->size)
{
ps->a[begin - 1] = ps->a[begin];
begin++;
}
ps->size--;
}
缺点
- 长度有限,不灵活
- 查询和删除操作太慢
- 会有多余的空间被浪费 :
MAXSIZE-length
- 当改变长度时候,内存可能需要进行大面积的数值迁移,浪费内存资源
单链表
长度随心,非常灵活,创建麻烦些,但是一劳永逸
组成:结点
- 数据域:存储数据元素信息的域
- 指针域:存储直接后继位置的域
- 头指针:标志作用,无论链表是否为空,它不为空。且位于第一个元素之前,它的数据域一般无意义(可以用来存放链表长度,但最好不要存放链表长度)。
时间复杂度
- 查询操作:O(n)
- 插入/删除操作:O(1)
代码实现
#pragma once
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
typedef int SLTDateType;
typedef struct SListNode
{
SLTDateType date;
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
void SListInsertAfter(SListNode* pos, SLTDateType x);
// 单链表删除pos位置之后的值
void SListEraseAfter(SListNode* pos);
void SListErase(SListNode** pplist, SListNode* pos);
// 单链表的销毁
void SListDestory(SListNode** pplist);
SListNode* BuySListNode(SLTDateType x)
{
SListNode* newNode = malloc(sizeof(SListNode));
if (newNode == NULL)
{
printf("malloc fail\n");
exit(-1);
}
else
{
newNode->date = x;
newNode->next = NULL;
}
}
void SListPrint(SListNode* plist)
{
SListNode* cur = plist;
while (cur != NULL)
{
printf("%d->", cur->date);
cur = cur->next;
}
printf("NULL");
}
void SListPushBack(SListNode** pplist, SLTDateType x)
{
SListNode* newnode = BuySListNode(x);
if (*pplist == NULL)
{
*pplist = newnode;
}
else
{
SListNode* tail = *pplist;
while (tail->next != NULL)
{
tail = tail->next;
}
tail->next = newnode;
}
}
void SListPushFront(SListNode** pplist, SLTDateType x)
{
SListNode* newnode = BuySListNode(x);
newnode->next = *pplist;
*pplist = newnode;
}
void SListPopBack(SListNode** pplist)
{
if (*pplist == NULL)
{
return;
}
SListNode* pre = NULL;
SListNode* cur = *pplist;
while (cur->next != NULL)
{
pre = cur;
cur = cur->next;
}
pre->next = NULL;
return;
}
void SListPopFront(SListNode** pplist)
{
assert(pplist);
if (*pplist == NULL)
{
return;
}
else
{
SListNode* head = (*pplist)->next;
free(*pplist);
*pplist = head;
}
}
SListNode* SListFind(SListNode* plist, SLTDateType x)
{
if (plist == NULL)
{
return NULL;
}
SListNode* head = plist;
while (head->next != NULL)
{
if (head->date == x)
return head;
head = head->next;
}
return NULL;
}
void SListInsertAfter(SListNode* pos, SLTDateType x)
{
assert(pos);
SListNode* next = pos->next;
SListNode* newnode = BuySListNode(x);
pos->next = newnode;
newnode->next = next;
//newnode->next = pos->next;
//pos->next = newnode;
}
void SListErase(SListNode** pplist, SListNode* pos)
{
assert(pplist);
assert(pos);
SListNode* cur = *pplist;
if (pos == *pplist)
{
void SListPopFront(pplist);
}
else
{
while (cur->next != pos)
{
cur = cur->next;
}
cur->next = pos->next;
free(pos);
pos = NULL;
}
}
void SListEraseAfter(SListNode* pos)
{
assert(pos);
SListNode* next = pos->next;
if (next)
{
pos->next = next->next;
free(next);
next = NULL;
}
}
void SListDestory(SListNode** pplist)
{
assert(pplist);
SListNode* cur = *pplist;
while (cur )
{
SListNode* next = cur->next;
free(cur);
cur = next;
}
}
缺点
- 查询消耗时间
- 在中间位置插入时候,时间复杂度大