1.今天给大家介绍线性表中两个常见的结构顺序表和链表,其中链表又包括单链表和带头双向循环链表。
2.此部分的全部代码放在个人gitee中 ,需要的自行拿取,前后文件依次对应SeqList SList DList。gitee链接点这里
一、线性表
1.线性表
线性表(linear list)是n个具有相同特性的数据元素的有限序列。 线性表是一种在实际中广泛使用的数据结构,常见的线性表:顺序表、链表、栈、队列、字符串…
线性表在逻辑上是线性结构,也就说是连续的一条直线。但是在物理结构上并不一定是连续的,线性表在物理上存储时,通常以数组和链式结构的形式存储。
二、顺序表
2.顺序表
2.1.概念及结构
顺序表是用一段物理地址连续的存储单元依次存储数据元素的线性结构,一般情况下采用数组存储。在数组上完成数据的增删查改。
1.静态顺序表
typedef int SLDataType;
typedef struct SeqList
{
SLDataType array[100];
SLDataType size;
}SeqList;
2.动态顺序表
typedef int SLDataType;
typedef struct SeqList
{
SLDataType* array;
SLDataType size;
SLDataType capacity;
}SeqList;
静态顺序表只适用于确定知道需要存多少数据的场景。静态顺序表的定长数组导致N定大了,空间开多了浪费,开少了不够用。所以现实中基本都是使用动态顺序表,根据需要动态的分配空间大小,所以下面我们实现动态顺序表,也就是在上面的gitee里。
三、链表
3.链表
1.链表的概念
概念:链表是一种物理存储结构上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的
2.链表的结构
- 链表的结构非常多样,大概可以分为八种结构
1.带头还是不带头
2.循环还是不循环
3.单向还是双向
- 虽然单链表的结构众多,但大部分常用的还是两种结构。无头单向不循环链表和带头双向循环链表
- 无头单向非循环链表:结构简单,一般不会单独用来存数据。实际中更多是作为其他数据结构的子结构,如哈希桶、图的邻接表等等。另外这种结构在笔试面试中出现很多。
- 带头双向循环链表:结构最复杂,一般用在单独存储数据。实际中使用的链表数据结构,都是带头双向循环链表。另外这个结构虽然结构复杂,但是使用代码实现以后会发现结构会带来很多优势,实现反而简单了,后面我们代码实现了就知道了。
四、顺序表和链表的区别和联系
不同点 | 顺序表 | 链表 |
---|---|---|
存储空间 | 物理上一定连续 | 逻辑上连续,物理上不一定连续 |
随机访问 | 支持O(1) | 不支持O(n) |
任意位置插入或者删除 | 可能移动元素,效率低 | 修改指针指向 |
插入 | 动态顺序表,空间不够增容 | 没有容量概念 |
应用场景 | 高效存储+频繁访问 | 任意位置插入删除频繁 |
缓存利用率 | 高 | 低 |
五、链表的常见OJ题
5.OJ题
-
struct Node* copyRandomList(struct Node* head) { //先把两个链表合并为一个链表 struct Node* cur = head; while(cur) { struct Node* copy = (struct Node*)malloc(sizeof(struct Node)); copy->val = cur->val; copy->next = cur->next; cur->next = copy; cur = copy->next; } //把random指向合适位置的值 cur = head; while(cur) { struct Node* copy = cur->next; if(cur->random == NULL) { copy->random = NULL; } else { copy->random = cur->random->next; } cur = copy->next; } //分开为两条链表 cur = head; struct Node* copyhead = NULL; struct Node* copytail = NULL; while(cur) { struct Node* next = cur->next; if(copyhead == NULL) { copyhead = next; copytail = next; } else { copytail->next = next; copytail = next; } cur = next->next; } return copyhead; }
2.OJ2
struct ListNode* hasCycle(struct ListNode *head)
{
struct ListNode* slow = head;
struct ListNode* fast = head;
while(fast != NULL && fast->next != NULL)
{
slow = slow->next;
fast = fast->next->next;
if(slow == fast)
{
return slow;
}
}
return NULL;
}
struct ListNode *detectCycle(struct ListNode *head)
{
struct ListNode* side = hasCycle(head);
if(side == NULL)
{
return NULL;
}
while(head != side)
{
head = head->next;
side = side->next;
}
return side;
}
3.OJ3
bool hasCycle(struct ListNode *head)
{
struct ListNode* slow = head;
struct ListNode* fast = head;
while(fast != NULL && fast->next != NULL)
{
slow = slow->next;
fast = fast->next->next;
if(slow == fast)
{
return true;
}
}
return false;
}
4.OJ4
struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2)
{
struct ListNode* guard = (struct ListNode*)malloc(sizeof(struct ListNode));
if(guard == NULL)
{
exit(-1);
}
guard->next = NULL;
struct ListNode* tail = guard;
struct ListNode* cur1 = list1;
struct ListNode* cur2 = list2;
while(cur1 && cur2)
{
if(cur1->val < cur2->val)
{
tail->next = cur1;
tail = tail->next;
cur1 = cur1->next;
}
else
{
tail->next = cur2;
tail = tail->next;
cur2 = cur2->next;
}
}
if(cur1)
tail->next=cur1;
if(cur2)
tail->next=cur2;
struct ListNode* head = guard->next;
free(guard);
guard = NULL;
return head;
}
5.OJ5
struct ListNode* removeElements(struct ListNode* head, int val)
{
if(head==NULL)
{
return NULL;
}
while(head!=NULL && head->val==val)
{
struct ListNode* empty=head;
head=head->next;
free(empty);
empty=NULL;
}
struct ListNode* prev=NULL;
struct ListNode* cur=head;
while(cur!=NULL)
{
if(cur->val==val)
{
struct ListNode* empty=cur;
prev->next=cur->next;
cur=cur->next;
free(empty);
empty=NULL;
}
else
{
prev=cur;
cur=cur->next;
}
}
return head;
}