目录
【本节目标】
- 线性表
- 顺序表
- 链表
- 顺序表和链表的区别和联系
1.线性表
线性表(linear list)是n个具有相同特性的数据元素的有限序列。 线性表是一种在实际中广泛使 用的数据结构,常见的线性表:顺序表、链表、栈、队列、字符串..
线性表在逻辑上是线性结构,也就说是连续的一条直线。但是在物理结构上并不一定是连续的, 线性表在物理上存储时,通常以数组和链式结构的形式存储。
链表
2.顺序表
2.1概念及结构
顺序表一般可以分为:
1. 静态顺序表:使用定长数组存储元素。
2. 动态顺序表:使用动态开辟的数组存储
//顺序表的动态存储
typedef struct SeqList
{
SLDataType* array;//指向动态开辟的数组
size_t size;//数据个数
size_t capacity;//容量空间大小
}SeqList;
2.2 接口实现
静态顺序表只适用于确定知道需要存多少数据的场景。静态顺序表的定长数组导致N定大了,空 间开多了浪费,开少了不够用。所以现实中基本都是使用动态顺序表,根据需要动态的分配空间 大小,所以下面我们实现动态顺序表。
typedef int SLDataType;
// 顺序表的动态存储
typedef struct SeqList
{
SLDataType* array; // 指向动态开辟的数组
size_t size; // 有效数据个数
size_t capicity; // 容量空间的大小
}SeqList;
// 基本增删查改接口
// 顺序表初始化
void SeqListInit(SeqList* psl);
// 检查空间,如果满了,进行增容
void CheckCapacity(SeqList* psl);
// 顺序表尾插
void SeqListPushBack(SeqList* psl, SLDataType x);
// 顺序表尾删
void SeqListPopBack(SeqList* psl);
// 顺序表头插
void SeqListPushFront(SeqList* psl, SLDataType x);
// 顺序表头删
void SeqListPopFront(SeqList* psl);
// 顺序表查找
int SeqListFind(SeqList* psl, SLDataType x);
// 顺序表在pos位置插入x
void SeqListInsert(SeqList* psl, size_t pos, SLDataType x);
// 顺序表删除pos位置的值
void SeqListErase(SeqList* psl, size_t pos);
// 顺序表销毁
void SeqListDestory(SeqList* psl);
// 顺序表打印
void SeqListPrint(SeqList* psl);
顺序表初始化
//初始化顺序表
void SLInit(SL* psl)
{
assert(psl);
psl->a = NULL;
psl->size = 0;
psl->capacity = 0;
}
检查顺序表增容
//扩大容量
void SLCheckCapacity(SL* psl)
{
assert(psl);
if (psl->capacity == psl->size)
{
int newcapCity = psl->capacity == 0 ? 4 : (psl->capacity) * 2;
SLDataType* tmp = (SLDataType*)realloc(psl->a, sizeof(SLDataType) * newcapCity);
if (tmp == NULL)
{
perror("realloc fail");
return;
}
psl->a = tmp;
psl->capacity = newcapCity;
}
}
顺序表尾插
//尾插
void SLPushBack(SL* psl, SLDataType x)
{
assert(psl);
SLCheckCapacity(psl);
psl->a[psl->size] = x;
psl->size++;
}
顺序表头插
//头插
void SLPushFront(SL* psl, SLDataType x)
{
assert(psl);
SLCheckCapacity(psl);
int end = psl->size - 1;
while (end >= 0)
{
psl->a[end + 1] = psl->a[end];
end--;
}
psl->a[0] = x;
psl->size++;
}
顺序表尾删
//尾删
void SLPopBack(SL* psl)
{
assert(psl);
assert(psl->size > 0);
psl->size--;
}
顺序表头删
//头删
void SLPopFront(SL* psl)
{
assert(psl);
int begin = 1;
while (begin < psl->size)
{
psl->a[begin - 1] = psl->a[begin];
++begin;
}
psl->size--;
}
顺序表查找
顺序表在pos位置插入x
// 任意下标位置的插入删除
//插入
void SLInsert(SL* psl, int pos, SLDataType x)
{
assert(psl);
assert(pos >= 0 || pos <= psl->size - 1);
SLCheckCapacity(psl);
int end = psl->size - 1;
while (end >= pos)
{
psl->a[end + 1] = psl->a[end];
end--;
}
psl->a[pos] = x;
psl->size++;
}
顺序表删除pos位置的值
//删除
void SLErase(SL* psl, int pos)
{
assert(psl);
assert(pos >= 0 && pos < psl->size);
/*int begin = psl->size - 1;
while (begin > pos)
{
psl->a[begin-1] = psl->a[begin];
--begin;
}*/
// 挪动覆盖
int begin = pos + 1;
while (begin < psl->size)
{
psl->a[begin - 1] = psl->a[begin];
++begin;
}
psl->size--;
}
顺序表销毁
//销毁顺序表
void SLDestroy(SL* psl)
{
assert(psl);
if (psl->a != NULL)
{
free(psl->a);
psl->a = NULL;
psl->size = 0;
psl->capacity = 0;
}
}
顺序表打印
//打印顺序表数据
void SLPrint(SL* psl)
{
assert(psl);
for (int i = 0; i < psl->size; i++)
{
printf("%d ", psl->a[i]);
}
printf("\n");
}
2.3 数组相关面试题
1. 原地移除数组中所有的元素val,要求时间复杂度为O(N),空间复杂度为O(1)。OJ链接
src遍历数组
src查找等于val的值时,将src赋值给dst来存储,src继续走,当找到不等val的值,将其在赋给dst之后dst++
代码如下:
2. 删除排序数组中的重复项。OJ链接
(1) dst和src相等
++dst
(2) dst和src不相等
++src;
a[src] = a[dst];
++dst;
本质,dst依次查找跟src位置不相等值,依次从前往后覆盖存储
排序,去重算法
代码如下:
3. 合并两个有序数组。OJ链接
注意:用三个下标 i1,i2,j
从后向前取
i1小于0,不需要处理
i2大于0,不需要处理
一种可能,当i1排序完,i2还未排序完,但程序已经结束,故
while(i2>=0)
{
nums1[j--] = nums2[i2--];
}
2.4 顺序表的问题及思考
问题:
1. 中间/头部的插入删除,时间复杂度为O(N)
2. 增容需要申请新空间,拷贝数据,释放旧空间。会有不小的消耗。
3. 增容一般是呈2倍的增长,势必会有一定的空间浪费。例如当前容量为100,满了以后增容到 200,我们再继续插入了5个数据,后面没有数据插入了,那么就浪费了95个数据空间。
思考:如何解决以上问题呢?下面给出了链表的结构来看看。
3.链表
3.1 链表的概念及结构
概念:链表是一种物理存储结构上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表 中的指针链接次序实现的 。
现实中 数据结构中
注意:
- 从上图可看出,链式结构在逻辑上是连续的,但是在物理上不一定连续
- 现实中的节点一般都是从堆上申请的
- 从堆上申请的空间,是按照一定的策略来分配的,再次申请的空间可能连续,也可能不连续
3.2 链表的分类
实际中链表的结构非常多样,以下情况组合起来就有8种链表结构:
1. 单向或者双向
2. 带头或者不带头
3. 循环或者非循环
虽然有这么多的链表的结构,但是我们实际中最常用还是两种结构:
1. 无头单向非循环链表:结构简单,一般不会单独用来存数据。实际中更多是作为其他数据结 构的子结构,如哈希桶、图的邻接表等等。另外这种结构在笔试面试中出现很多。
2. 带头双向循环链表:结构最复杂,一般用在单独存储数据。实际中使用的链表数据结构,都 是带头双向循环链表。另外这个结构虽然结构复杂,但是使用代码实现以后会发现结构会带 来很多优势,实现反而简单了,后面我们代码实现了就知道了。
3.3 链表的实现
// 1、无头+单向+非循环链表增删查改实现
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);
创建一个数据空间(动态申请一个字节)
SLNode* CreateNode(SLNDataType x)
{
SLNode* newnode = (SLNode*)malloc(sizeof(SLNode));
if (newnode == NULL)
{
perror("malloc fail");
return;
}
newnode->val = x;
newnode->next = NULL;
return newnode;
}
单链表打印
//打印
void SLTPrint(SLNode* phead)
{
SLNode* cur = phead;
while (cur!= NULL)
{
printf("%d->", cur->val);
cur = cur->next;
}
printf("NULL");
}
单链表尾插
//尾插
void SLTPushBack(SLNode** pphead, SLNDataType x)
{
assert(pphead);
SLNode* newnode = CreateNode(x);
//单个节点
if (*pphead == NULL)
{
*pphead = newnode;
}
else
{
//多个节点
SLNode* tail = *pphead;
while (tail->next != NULL)
{
tail = tail->next;
}
tail->next = newnode;
}
}
单链表头插
//头插
void SLTPushFront(SLNode** pphead, SLNDataType x)
{
assert(pphead);
//单个节点和多个节点
SLNode* newnode = CreateNode(x);
newnode->next = *pphead;
(*pphead) = newnode;
}
单链表尾删
//尾删
void SLTPopBack(SLNode** pphead)
{
assert(pphead);
//一个节点
if ((*pphead)->next == NULL)
{
*pphead = NULL;
free(*pphead);
}
else
{
//多个节点
//找尾
SLNode* tail = *pphead;
SLNode* prev = NULL;
while (tail->next != NULL)
{
prev = tail;
tail = tail->next;
}
free(tail);
tail = NULL;
prev->next = NULL;
}
}
单链表头删
//头删
void SLTPopFront(SLNode** pphead)
{
assert(pphead);
//多个节点和一个节点
SLNode* tmp = *pphead;
(*pphead) = (tmp)->next;
free(tmp);
}
单链表查找
// 单链表查找
SLNode* SLTFind(SLNode* phead, SLNDataType x)
{
assert(phead);
SLNode* temp = phead;
while (temp)
{
if (temp->val == x)
{
return temp;
}
temp = temp->next;
}
return NULL;
}
单链表在pos位置之后插入x
// 单链表在pos位置之后插入x
void SLTInsertAfter(SLNode* pos, SLNDataType x)
{
assert(pos);
SLNode* newnode = CreateNode(x);
pos->next = newnode;
newnode->next = pos->next;
}
单链表删除pos位置之后的值
// 单链表删除pos位置之后的值
void SLTEraseAfter(SLNode* pos)
{
assert(pos);
assert(pos->next);
SLNode* tmp = pos->next;
pos->next = tmp->next;
free(tmp);
tmp = NULL;
}
在pos的前面插入
// 在pos的前面插入
SLNode* SLTInsert(SLNode** pphead, SLNode* pos, SLNDataType x)
{
assert(pphead);
assert(*pphead);
assert(pos);
//单个节点
if (*pphead == pos)
{
//头插
SLTPushFront(pphead, x);
}
else//多个节点
{
SLNode* prev = *pphead;
while (prev->next != pos)
{
prev = prev->next;
}
SLNode* newnode = CreateNode(x);
prev->next = newnode;
newnode->next = pos;
}
}
删除pos位置
// 删除pos位置
void SLTErase(SLNode** pphead, SLNode* pos)
{
assert(pphead);
assert(pos);
assert(*pphead);
if (*pphead == pos)
{
//头删
SLTPopFront(pphead);
}
else
{
SLNode* prev = *pphead;
while (prev->next != pos)
{
prev = prev->next;
}
prev->next = pos->next;
free(pos);
pos = NULL;
}
}
销毁链表
//销毁链表
void SLTDestroy(SLNode** pphead)
{
assert(pphead);
SLNode* cur = *pphead;
while (cur)
{
SLNode* next = cur->next;
free(cur);
cur = next;
}
*pphead = NULL;
}
3.4 链表面试题
1. 删除链表中等于给定值 val 的所有结点。 OJ链接
用三个指针prev,cur,next
分为两种可能:
1.头节点数据等于val
2.头节点数据不等于val
代码如下:
2. 反转一个单链表。 OJ链接
struct ListNode* reverseList(struct ListNode* head) {
if(head == NULL)
{
return NULL;
}
struct ListNode* n1 = NULL;
struct ListNode* n2 = head;
struct ListNode* n3 = head->next;
while(n2)
{
n2->next = n1;
n1 = n2;
n2 = n3;
if(n3)
n3 = n3->next;
}
return n1;
}
运用不带哨兵位的链表,用n1,n2,n3三个指针进行判断,3个指针分别存放头节点之前的指针(即为空),头节点,头节点的下一个节点,防止转换一个而导致之后的节点无法找到,三个节点以前向后走。
也要判断特殊情况,空表情况;还有n3最后会为空的情况
3. 给定一个带有头结点 head 的非空单链表,返回链表的中间结点。如果有两个中间结点,则 返回第二个中间结点。OJ链接
用两个指针指向这个链表slow和fast
fast每次变动两次
slow每次变动一次
代码如下:
struct ListNode* middleNode(struct ListNode* head) {
//快慢指针
struct ListNode*fast=head;//快
struct ListNode*slow=head;//慢
//fast每次走两步,slow每次走一步
while(fast!=NULL && fast->next!=NULL)
{
slow=slow->next;
fast=fast->next->next;
}
return slow;
}
4. 输入一个链表,输出该链表中倒数第k个结点。 OJ链接
struct ListNode* FindKthToTail(struct ListNode* pListHead, int k ) {
struct ListNode*p1=(struct ListNode*)malloc(sizeof(struct ListNode));//快
struct ListNode*p2=(struct ListNode*)malloc(sizeof(struct ListNode));//慢
p1=p2=pListHead;
//p1先走k
while(k--)
{
if(p1==NULL)
{
return NULL;
}
p1=p1->next;
}
//之后再一起走
while(p1!=NULL)
{
p1=p1->next;
p2=p2->next;
}
return p2;
}
运用快慢指针,p1,p2指针,p1先走k步,之后两个指针在一起走,最后当p1为空时,p2ze为倒数第k个节点
5. 将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有 结点组成的。OJ链接
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2) {
if(list1==NULL)
{
return list2;
}
if(list2==NULL)
{
return list1;
}
struct ListNode*head=NULL,*tail=NULL;
while(list1 && list2)
{
if(list1->val < list2->val)
{
if(tail == NULL)
{
head=tail=list1;
}
else
{
tail = list1;
tail=tail->next;
}
list1=list1->next;
}
else
{
if(tail == NULL)
{
head=tail=list2;
}
else
{
tail = list2;
tail=tail->next;
}
list2=list2->next;
}
}
if(list1)
{
tail=list1;
}
if(list2)
tail=list2;
return head;
}
6. 编写代码,以给定值x为基准将链表分割成两部分,所有小于x的结点排在大于或等于x的结 点之前 。OJ链接
有可能是全部小于,所以最后tail2->next=NULL;
7. 链表的回文结构。OJ链接
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};*/
struct ListNode*reverseList(struct ListNode* head)
{
struct ListNode*cur=head;
struct ListNode*newnode=NULL;
while(cur)
{
struct ListNode*next=cur->next;
//头插
cur->next=newnode;
newnode=cur;
cur=next;
}
return newnode;
}
struct ListNode*middleList(struct ListNode* head)
{
struct ListNode*fast=head,*slow=head;
while(fast && fast->next)
{
slow=slow->next;
fast=fast->next->next;
}
return slow;
}
class PalindromeList {
public:
bool chkPalindrome(ListNode* head) {
struct ListNode*mid=middleList(head);
struct ListNode*rhead=reverseList(mid);
while(head && rhead)
{
if(head->val != rhead->val)
{
return false;
}
head=head->next;
rhead=rhead->next;
}
return true;
}
};
8. 输入两个链表,找出它们的第一个公共结点。OJ链接
struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {
struct ListNode* curA = headA,*curB = headB;
for(struct ListNode* curA = headA;curA != NULL;curA=curA->next)
{
for(struct ListNode* curB = headB;curB != NULL;curB=curB->next)
{
if(curA == curB)
{
return curA;
}
}
}
return NULL;
}
3.5 双向链表的实现
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef int LTDataType;
typedef struct ListNode
{
struct ListNode* prev;
struct ListNode* next;
LTDataType val;
}LTNode;
//链表初始化
LTNode* LTInit();
//打印链表
void LTPrint(LTNode* phead);
//尾插
void LTPushBack(LTNode* phead, LTDataType x);
//尾删
void LTPopBack(LTNode* phead);
// 双向链表头插
void LTPushFront(LTNode* phead, LTDataType x);
// 双向链表头删
void LTPopFront(LTNode* phead);
// 双向链表查找
LTNode* LTFind(LTNode* phead, LTDataType x);
// 双向链表在pos的前面进行插入
void LTInsert(LTNode* pos, LTDataType x);
// 双向链表删除pos位置的节点
void LTErase(LTNode* pos);
// 双向链表销毁
void LTDestroy(LTNode* pHead);
申请一段空间
//申请一段空间
LTNode* CreateLTNode(LTDataType x)
{
LTNode* newnode = (LTNode*)malloc(sizeof(LTNode));
if (newnode == NULL)
{
perror("malloc fail");
exit(-1);
}
newnode->next = NULL;
newnode->prev = NULL;
newnode->val = x;
return newnode;
}
链表初始化
//链表初始化
LTNode* LTInit()
{
LTNode* phead = CreateLTNode(-1);
phead->next = phead;
phead->prev = phead;
return phead;
}
打印链表
//打印链表
void LTPrint(LTNode* phead)
{
assert(phead);
printf("哨兵位<=>");
LTNode* cur = phead->next;
while (cur != phead)
{
printf("%d<=>",cur->val);
cur = cur->next;
}
printf("\n");
}
双向链表尾插
//尾插
void LTPushBack(LTNode* phead, LTDataType x)
{
assert(phead);
LTNode* newnode = CreateLTNode(x);
LTNode* tail = phead->prev;
tail->next = newnode;
newnode->prev = tail;
newnode->next = phead;
phead->prev = newnode;
}
双向链表尾删
//尾删
void LTPopBack(LTNode* phead)
{
assert(phead);
assert(phead->next != phead);
LTNode* tail = phead->prev;
LTNode* prevtail = tail->prev;
prevtail->next = phead;
free(tail);
phead->prev = prevtail;
}
双向链表头插
// 双向链表头插
void LTPushFront(LTNode* phead, LTDataType x)
{
assert(phead);
LTNode* tail = phead->next;
LTNode* newnode = CreateLTNode(x);
newnode->next = tail;
tail->prev = newnode;
phead->next = newnode;
newnode->prev = phead;
}
双向链表头删
// 双向链表头删
void LTPopFront(LTNode* phead)
{
assert(phead);
assert(phead->next != phead);
LTNode* tail = phead->next;
LTNode* tailNext = tail->next;
phead->next = tailNext;
tailNext->prev = phead;
free(tail);
}
双向链表查找
// 双向链表查找
LTNode* LTFind(LTNode* phead, LTDataType x)
{
assert(phead);
LTNode* tail = phead;
while (tail != phead)
{
if (tail->val == x)
{
return tail;
}
tail = tail->next;
}
return NULL;
}
双向链表在pos位置前面插入
// 双向链表在pos的前面进行插入
void LTInsert(LTNode* pos, LTDataType x)
{
assert(pos);
LTNode* tail = pos->prev;
LTNode* newnode = CreateLTNode(x);
tail->next = newnode;
newnode->prev = tail;
newnode->next = pos;
pos->prev = newnode;
}
双向链表删除pos位置的节点
// 双向链表删除pos位置的节点
void LTErase(LTNode* pos)
{
assert(pos);
LTNode* posNext = pos->next;
LTNode* posPrev = pos->prev;
posPrev->next = posNext;
posNext->prev = posPrev;
free(pos);
//pos = NULL;
}
双向链表销毁
// 双向链表销毁
void LTDestroy(LTNode* phead)
{
assert(phead);
LTNode* tail = phead->next;
while (tail != phead)
{
LTNode* cur = tail->next;
free(tail);
tail = tail->next;
}
free(phead);
}