顺序表和链表

1.今天给大家介绍线性表中两个常见的结构顺序表和链表,其中链表又包括单链表和带头双向循环链表。

2.此部分的全部代码放在个人gitee中 ,需要的自行拿取,前后文件依次对应SeqList SList DListgitee链接点这里

一、线性表

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.单向还是双向

在这里插入图片描述

  • 虽然单链表的结构众多,但大部分常用的还是两种结构。无头单向不循环链表和带头双向循环链表
  1. 无头单向非循环链表:结构简单,一般不会单独用来存数据。实际中更多是作为其他数据结构的子结构,如哈希桶、图的邻接表等等。另外这种结构在笔试面试中出现很多。
  2. 带头双向循环链表:结构最复杂,一般用在单独存储数据。实际中使用的链表数据结构,都是带头双向循环链表。另外这个结构虽然结构复杂,但是使用代码实现以后会发现结构会带来很多优势,实现反而简单了,后面我们代码实现了就知道了。

四、顺序表和链表的区别和联系

不同点顺序表链表
存储空间物理上一定连续逻辑上连续,物理上不一定连续
随机访问支持O(1)不支持O(n)
任意位置插入或者删除可能移动元素,效率低修改指针指向
插入动态顺序表,空间不够增容没有容量概念
应用场景高效存储+频繁访问任意位置插入删除频繁
缓存利用率

五、链表的常见OJ题

5.OJ题

  1. OJ1

    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;
}
  • 15
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sense the warmth

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值