【数据结构】线性顺序结构----单链表(实现头插、删,尾插、删,任意位置插、删)

 

1、链表的概念
链表是一种 物理存储结构上非连续 、非顺序的存储结构,数据元素的 逻辑顺序 是通过链表中的 指针链 次序实现的 。
 

2、链表的分类

由单向 / 双向,带头 / 不带头,循环 / 非循环组成8种链表结构
 
3、顺序表和链表的优缺点
顺序表:支持随机访问,但是插入时间复杂度较大O(n),扩容代价也大
链表:插入删除时间复杂度O(1),没有扩容问题
 
4、链表的实现
 
 
#pragma once 

#include <iostream>
#include <assert.h>

typedef int slint;


struct SLNode
{
  SLNode* next;
  slint val;

  SLNode(slint data = 0)
    : next(nullptr)
      , val(data)
  {}

};

//   尾插/删
void PushBack(SLNode** head, slint data)
{
  assert(head);
  SLNode* Node = new SLNode(data);
  if((*head) == nullptr)
  {
    (*head) = Node; 
    return;
  }
  SLNode* cur = *head;
  while(cur->next)
  {
    cur = cur->next;
  }
  cur->next = Node;
}

void PopBack(SLNode** head)
{
  assert(head);
  if(*head == nullptr)
  {
    std::cout << "链表为空,不能删除!!!" << std::endl;
    return;
  }
  else if((*head)->next == nullptr)
  {
    // 考虑是一个节点的情况
    SLNode* cur = *head;
    (*head) = nullptr;
    delete cur;
    cur = nullptr;
  }
  else 
  {
    SLNode* cur = *head;
    SLNode* prev = nullptr;
    while(cur->next)
    {
      prev = cur;
      cur = cur->next;
    }
    prev->next = nullptr;
    delete cur;
    cur = nullptr;
  }

}

//  头插/删
void PushFront(SLNode** head, slint data)
{
  assert(head);
  SLNode* Node = new SLNode(data);
  if(*head == nullptr)
  {
    *head = Node;
    return;
  }
  else 
  {
    SLNode* cur = (*head)->next;
    (*head) = Node;
    Node->next = cur;
  }
}

void PopFront(SLNode** head)
{
  assert(head);
  if(*head == nullptr)
  {
    std::cout << "链表为空,不能删除!!!" << std::endl;
    return;
  }
  else if((*head)->next == nullptr)
  {
    SLNode* del = (*head)->next;
    *head = nullptr;
    delete del;
    del = nullptr;
  }
  else 
  {
    SLNode* del = (*head)->next;
    SLNode* next = del->next;
    (*head)->next = next;
    delete del;
    del = nullptr;
  }
}


//在任意位置插入节点
void SLInsertAfter(SLNode* pos, slint data)
{
  assert(head);
  if(pos == nullptr)
    return;
  SLNode* Node = new SLNode(data);
  // SLNode* next = pos->next;
  // pos->next = Node;
  // Node->next = next;
  Node->next = pos->next;
  pos->next = Node;
}

//在任意位置删除节点
void SLErase(SLNode**head, SLNode* pos)
{
  assert(head);
  SLNode* cur = *head;
  while(cur->next != pos)
  {
    cur = cur->next;
  }
  cur->next = pos->next;
  delete pos;
  pos = nullptr;
}

//链表的长度
int SLSize(SLNode* head)
{
  assert(head);
  SLNode* cur = (head)->next;
  int count = 0;
  while(cur)
  {
    ++count;
    cur = cur->next;
  }
  return count;
}

//寻找值为data的位置
SLNode* SLFind(SLNode* head, int data)
{
  assert(head);
  SLNode* cur = (head)->next;
  while(cur)
  {
    if(cur->val == data)
    {
      return cur;
    }
    cur = cur->next;
  }
  return nullptr;
}

//打印
void Print(SLNode* head)
{
  assert(head);
  SLNode* cur = head->next;
  while(cur)
  {
    std::cout << cur->val << " "; 
  }
  std::cout << std::endl;
}

//销毁链表
void Destroy(SLNode** head)
{
  SLNode* cur = (*head)->next;
  while(cur)
  {
    *head = cur->next;
    delete cur;
    cur = *head;
  }
}

 

void test1()
{
    //测试尾插函数PushBack
    SLNode* L = NULL;
    PushBack(&L, 1);
    PushBack(&L, 2);
    PushBack(&L, 3);
    PushBack(&L, 4);
    PushBack(&L, 5);
    //打印
    Print(L);

    
}

void test()
{
    test1();
}


int main()
{
    test();
    system("pause");
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值