顺序表与链表
顺序表
概念与结构
顺序表是用一段物理地址连续的存储单元依次存储数据元素的线性结构,一般情况下采用数组存储。在数组上完成数据的增删查改。
顺序表一般可以分为:
- 静态顺序表:使用定长数组存储元素
#define N 1000
typedef int SlDataType;
typedef struct SeqList
{
SLDataType arr[N];
int size;
}SL;
- 动态顺序表:使用动态开辟的内存空间存储元素
typedef int SLDataType;
typedef struct SeqList
{
SLDataType* a;
int size;
int capacity;
}SL;
接口实现
静态顺序表只适用于确定知道需要存多少数据的场景。静态顺序表的定长数组导致N定大了,空间开多了浪费,开少了不够用。所以现实中基本都是使用动态顺序表,根据需要动态的分配空间大小,所以下面我们实现动态顺序表。
SeqList.h
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef int SLDataType ;
typedef struct SeqList
{
SLDataType* a;
int size;//数组中存储了多少个数据
int capacity;//数组实际能存数据空间容量的大小
}SL;
//接口函数 -- 命名风格是跟着STL走的,建议大家也跟着我们上课走,方便后续学习STL
void SeqListInit(SL* ps);
void SeqListPushBack(SL* ps, SLDataType x);
void SeqListPrint(SL* ps);
void SeqListPopBack(SL* ps);
void SeqListDestory(SL* ps);
void SeqListPushFront(SL* ps, SLDataType x);
void SeqListPopFront(SL* ps);
int SeqListFind(SL* ps, SLDataType x);
void SeqListInsert(SL* ps, int pos, SLDataType x);
void SeqListErase(SL* ps, int pos);
SeqList.c
#include "SeqList.h"
void SeqListInit(SL* ps)
{
ps->a = NULL;
ps->size = 0;
ps->capacity = 0;
}
void SeqListCheckCapacity(SL* ps)
{
if (ps->size == ps->capacity)
{
int newcapacity = capacity = 0 ? 4 : 2 * ps->capacity;
SLDataType* tmp = (SLDataType*)realloc(ps->a, sizeof(SLDataType) * newcapacity);
if (tmp == NULL)
exit(-1);
ps->a = tmp;
ps->capacity = newcapacity;
}
}
void SeqListPushBack(SL* ps, SLDataType x)
{
//SeqListCheckCapacity(ps);
//ps->a[ps->size] = x;
//ps->size++;
SeqListInsert(ps, ps->size, x);
}
void SeqListPrint(SL* ps)
{
int i = 0;
for (i=0; i<ps->size; i++)
{
printf("%d ", ps->a[i]);
}
}
void SeqListPopBack(SL* ps)
{
//温柔的方式
//if (ps->size > 0)
//{
//ps->size--;
//}
//暴力的方式
//assert(ps->size > 0);
//ps->size--;
SeqListErase(ps, ps->size-1)
}
void SeqListDestory(SL* ps)
{
ps->a = NULL;
ps->size = 0;
ps->capacity = 0;
}
void SeqListPushFront(SL* ps, SLDataType x)
{
//SeqListCheckCapacity(ps);
//int end = ps->size - 1;
//while (end >= 0)
//{
//ps->a[end+1] = ps->a[end];
//end--;
//}
//ps->a[0] = x;
//ps->size++;
SeqListInsert(ps, 0, x);
}
void SeqListPopFront(SL* ps)
{
//int begin = 0;
//while (begin < ps->size-1)
//{
//ps->a[begin] = ps->a[begin+1];
//begin++;
//}
//ps->size--;
SeqListErase(ps, 0);
}
int SeqListFind(SL* ps, SLDataType x)
{
int i = 0;
for (i=0; i<ps->size; i++)
{
if (x == ps->a[i])
{
return i;
}
}
return -1;
}
void SeqListInsert(SL* ps, int pos, SLDataType x)
{
assert(pos >= 0 && pos <= ps->size);
SeqListCheckCapacity(ps);
int end = ps->size-1;
while (end >= pos)
{
ps->a[end+1] = ps->a[end];
end--;
}
ps->a[pos] = x;
ps->size++;
}
void SeqListErase(SL* ps, int pos)
{
assert(pos >= 0 && pos < ps->size);
int begin = ps;
while (begin < ps->size-1)
{
ps->a[begin] = ps->a[begin+1];
begin++;
}
ps->size--;
}
数组的相关面试题
链接: link.
int removeElement(int* nums, int numsSize, int val)
{
int src = 0;
int det = 0;
while (src < numsSize)
{
if (nums[src] != val)
{
nums[det] = nums[src];
det++;
src++;
}
else
{
src++;
}
}
return det;
}
链接: link.
int removeDuplicates(int* nums, int numsSize)
{
if (numsSize == 0)
{
return 0;
}
int i = 0;
int j = 1;
int det = 0;
while (j < numsSize)
{
if (nums[i] == nums[j])
{
j++;
}
else
{
nums[det] = nums[i];
det++;
i = j;
j++;
}
}
nums[det] = nums[i];
det++;
return det;
}
链接: link.
void merge(int* nums1, int nums1Size, int m, int* nums2, int nums2Size, int n)
{
int i = m - 1;
int j = n - 1;
int det = m + n - 1;
while (i >= 0 && j >= 0)
{
if (nums1[i] > nums2[j])
{
nums1[det] = nums1[i];
det--;
i--;
}
else
{
nums1[det] = nums2[j];
det--;
j--;
}
}
if (i < 0)
{
while (j >= 0)
{
nums1[det] = nums2[j];
det--;
j--;
}
}
if (j < 0)
{
while (i >= 0)
{
nums1[det] = nums1[i];
det--;
i--;
}
}
}
链表
链表的概念与结构
链表是一种物理存储结构上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的。
注意:
1.从上图可知,链式结构在逻辑上是连续的,但是在物理上不一定连续。
2.现实中的结点一般多是从堆上申请的。
3.从堆上申请的空间,是按照一定的的策略来分配的,两次申请的空间可能连续,也可能不连续。
链表的分类
最常用还是两种结构:
无头单向非循环链表
无头单向非循环链表:结构简单,一般不会单独用来存数据。实际中更多是作为其他数据结构的子结构,如哈希桶、图的邻接表等等。另外这种结构在笔试面试中出现很多。
带头双向循环链表
带头双向循环链表:结构最复杂,一般用在单独存储数据。实际中使用的链表数据结构,都是带头双向循环链表。另外这个结构虽然结构复杂,但是使用代码实现以后会发现结构会带来很多优势,实现反而
简单了,后面我们代码实现了就知道了。
链表的实现
SList.h
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef int SLTDataType;
typedef struct SListNode
{
SLTDataType data;
struct SListNode* next;
}SLTNode;
void SListPrint(SLTNode* phead);
void SListPushBack(SLTNode** pphead, SLTDataType x);
void SListPushFront(SLTNode** pphead, SLTDataType x);
void SListPopback(SLTNode** pphead);
void SListPopFront(SLTNode** pphead);
SLTNode* SListFind(SLTNode* phead, SLTDataType x);
void SListInsertAfter(SLTNode* pos, SLTDataType x);
void SListEraseAfter(SLTNode* pos);
void SListDestroy(SLTNode** pphead);
SList.c
#include "SList.h"
SLTNode* BuyListNode(SLTDataType x)
{
SLTNode* newnode = (SLTNode*)malloc(sizeof(SLTNode));
if (newnode == NULL)
{
printf("malloc fail\n");
exit(-1);
}
newnode->data = x;
newnode->next = NULL;
return newnode;
}
void SListPrint(SLTNode* phead)
{
SLTNode* cur = phead;
while (cur)
{
printf("%d->", cur->data);
cur = cur->next;
}
printf("NULL\n');
}
void SListPushBack(SLTNode** pphead, SLTDataType x)
{
assert(pphead);
SLTNode* newnode = BuyListNode(x);
if (*pphead == NULL)
{
*pphead = newnode;
}
else
{
SLTNode* tail = *pphead;
while (tail->next)
{
tail = tail->next;
}
tail->next = newnode;
}
}
void SListPushFront(SLTNode** pphead, SLTDataType x)
{
assert(pphead);
SLTNode* newnode = BuyListNode(x);
newnode->next = *pphead;
*pphead = newnode;
}
void SListPopback(SLTNode** pphead)
{
assert(pphead);
assert(*pphead);
if (*pphead->next == NULL)
{
free(*pphead);
*pphead = NULL;
}
else
{
SLTNode* tail = *pphead;
SLTNode* prev = NULL;
while (tail->next)
{
prev = tail;
tail = tail->next;
}
free(tail);
tail = NULL;
prev->next = NULL;
}
}
void SListPopFront(SLTNode** pphead)
{
assert(pphead);
assert(*pphead);
SLTNode* next = (*pphead)->next;
free(*pphead);
*pphead = next;
}
SLTNode* SListFind(SLTNode* phead, SLTDataType x)
{
SLTNode* cur = phead;
while (cur)
{
if (cur->data == x)
{
return cur;
}
cur = cur->next;
}
return NULL;
}
//在pos位置的后面插入,这个更适合,也更简单
void SListInsertAfter(SLTNode* pos, SLTDataType x)
{
assert(pos);
SLTNode* newnode = BuyListNode(x);
SLTNode* next = pos->next;
pos->next = newnode;
newnode->next = next;
}
//在pos位置的后面删除,这个更适合,也更简单
void SListEraseAfter(SLTNode* pos)
{
assert(pos);
assert(pos->next);
SLTNode* next = pos->next;
pos->next = next->next;
free(next);
next = NULL;
}
void SListDestroy(SLTNode** pphead)
{
assert(pphead);
SLTNode* cur = *pphead;
while (cur)
{
SLTNode* next = cur->next;
free(cur);
cur = next;
}
*pphead = NULL;
}
链表面试题
链接: link
链接: link
链接: link
链接: link
链接: link
链接: link
链接: link
链接: link
链接: link
链接: link
链接: link
链接: link
双向链表的实现
List.h
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef int LTDataType;
typedef struct ListNode
{
LTDataType data;
struct ListNode* prev;
struct ListNode* next;
}LTNode;
LTNode* ListInit();
void ListPrint(LTNode* phead);
void ListPushBack(LTNode* phead, LTDataType x);
void ListPushFront(LTNode* phead, LTDataType x);
void ListPopBack(LTNode* phead);
void ListPopFront(LTNode* phead);
LTNode* ListFind(LTNode* phead, LTDataType x);
//pos位置前面插入
void ListInsert(LTNode* pos, LTDataType x);
//删除pos位置节点
void ListErase(LTNode* pos);
void ListDestroy(LTNode* phead);
List.c
#include "List.h"
LTNode* ListInit()
{
LTNode* phead = (LTNode*)malloc(sizeof(LTNode));
assert(phead);
phead->prev = NULL;
phead->next = NULL;
return phead;
}
LTNode* BuyListNode(LTDataType x)
{
LTNode* newnode = (LTNode*)malloc(sizeof(LTNode));
assert(newnode);
newnode->data = x;
newnode->prev = NULL;
newnode->next = NULL;
return newnode;
}
void ListPushBack(LTNode* phead, LTDataType x)
{
assert(phead);
LTNode* newnode = BuyListNode(LTDataType x);
LTNode* tail = phead->prev;
tail->next = newnode;
newnode->prev = tail;
phead->prev = newnode;
newnode->next = phead;
}
void ListPushFront(LTNode* phead, LTDataType x)
{
assert(phead);
LTNode* newnode = BuyListNode(LTDataType x);
LTNode* next = phead->next;
phead->next = newnode;
newnode->prev = phead;
newnode->next = next;
next->prev = newnode;
}
void ListPrint(LTNode* phead)
{
assert(phead);
LTNode* cur = phead->next;
while (cur != phead)
{
printf("%d ", cur->data);
cur = cur->next;
}
printf("\n");
}
void ListPopBack(LTNode* phead)
{
assert(phead);
assert(phead->next != phead);
LTNode* tail = phead->prev;
LTNode* prev = tail->prev;
prev->next = phead;
phead->prev = prev;
free(tail);
tail = NULL;
}
void ListPopFront(LTNode* phead)
{
assert(phead);
assert(phead->next != phead);
LTNode* next = phead->next;
LTNode* nextNext = next->next;
phead->next = nextNext;
nextNext->prev = phead;
free(next);
next = NULL;
}
LTNode* ListFind(LTNode* phead, LTDataType x);
{
assert(phead);
LTNode* cur = phead->next;
while (cur != phead)
{
if (cur->data == x)
return cur;
cur = cur->next;
}
return NULL;
}
//pos位置前面插入
void ListInsert(LTNode* pos, LTDataType x);
{
assert(pos);
LTNode* newnode = BuyListNode(LTDataType x);
LTNode* posPrev = pos->prev;
posPrev->next = newnode;
newnode->prev = posPrev;
newnode->next = pos;
pos->prev = newnode;
}
//删除pos位置节点
void ListErase(LTNode* pos)
{
assert(pos);
LTNode* posPrev = pos->prev;
LTNode* posAfter = pos->next;
posPrev->next = posAfter;
posAfter->prev = posPrev;
free(pos);
pos = NULL;
}
void ListDestroy(LTNode* phead)
{
assert(phead);
LTNode* cur = phead->next;
while (cur != phead)
{
LTNode* next = cur->next;
free(cur);
cur = next;
}
free(phead);
phead = NULL;
}
顺序表和链表的区别
这两个结构各有优势,很难说谁更优,严格来说,是相辅相成的两个结构。
顺序表:
优点:(用下标访问)
1.支持随机访问。需要随机访问结构支持算法可以很好的适用。
2.cpu高速缓存命中率更高。(不好理解)
缺点:
1.头部中部插入删除时间效率低。
2.连续的物理空间,空间不够了以后需要增容。
增容有一定程度消耗
为了避免连续增容,一般我们多按倍数去增,用不完可能存在一定的空间浪费
链表(双向带头循环链表)
优点:
1.任意位置插入删除效率高。
2.按需申请释放空间。
缺点:
1.不支持随机访问。(用下标访问)意味着:一些排序,二分查找等在这些结构上不适用。
2.链表存储一个值,同时要存储链表指针,也有一定的消耗。
3.cpu高速缓存命中率更低。(不好理解)