《算法思维——一种问题驱动的思维方式》之第5篇:数据结构之动态链表篇——采用C++编程语言实现

《算法思维——一种问题驱动的思维方式》之第5篇:数据结构之动态链表篇——采用C++编程语言实现

在这里插入图片描述

在数据结构的学习中,链表是动态数据结构的最基本的形式,也是最常见的一种线性数据结构,使用范围广。

在创建动态链表时,对不同节点之间的链接关系梳理清楚,这一点十分重要。。。

比如,下面开始用C++编程语言来实现基本的链表操作,包括:

链表的插入、链表的删除、链表的求长、链表的打印输出等。。。

链表的节点,可以按如下方式定义:

在这里插入图片描述

C++类头文件:Node.h


/*
作者:文方俊
日期:2020年11月18日 
*/

#pragma once
#ifndef __NODE_H__
#define __NODE_H__
class Node
{
public:
  Node();
  Node(int value);
  ~Node();
  int getValue();
  Node* next;
private:
  int value;  
};

#endif 


C++类实现文件:Node.cpp


/*
作者:文方俊
日期:2020年11月18日 
*/

#include "stdafx.h"
#include "Node.h"


Node::Node(){}

Node::Node(int value)
{
  this->value = value;
  this->next = NULL;
}

int Node::getValue(void)
{
  return this->value;
}

Node::~Node()
{
}

链表头文件:LinkedList.h


/*
作者:文方俊
日期:2020年11月18日 
*/

#pragma once

#ifndef __LINKEDLIST_H__
#define __LINKEDLIST_H__


#include "Node.h"

class LinkedList
{
public:
  LinkedList();
  ~LinkedList();
  void insertNode(int value); 
  Node* deleteNode(int value);
  int lengthLinkedList();
  void printValues();
private:
  Node* head;
  int* printLinkedList();
};

#endif 

链表中插入节点,


/*
作者:文方俊
日期:2020年11月18日 
*/

void LinkedList::insertNode(int value)
{
  Node* newNode = new Node(value);
  /*头节点为空*/
  if (NULL == this->head)
  {
    this->head = newNode;
  }
  else {
    /*头节点不为空*/
    newNode->next = this->head;
    this->head = newNode;
  }
}

链表中删除节点


/*
作者:文方俊
日期:2020年11月18日 
*/

Node* LinkedList::deleteNode(int value)
{
  /*头节点为空*/
  if (NULL == this->head)
  {
    return NULL; 
  }
  /*删除头节点*/
  Node* deleteNode = NULL;
  if (value == this->head->getValue())
  {
    deleteNode = this->head; 
    this->head = this->head->next; 
  }
  else {
    /*删除非头节点*/
    Node* preNode = this->head; 
    while ((NULL != preNode->next) && (value != preNode->next->getValue()))
    {
      preNode = preNode->next; 
    }
    if (NULL == preNode->next)
    {
      /*删除节点不存在*/
      return NULL; 
    }
    if (value == preNode->next->getValue())
    {
      /*删除节点*/
      deleteNode = preNode->next; 
      preNode->next = preNode->next->next; 
    }
  }
  return deleteNode;
}

链表的长度


/*
作者:文方俊
日期:2020年11月18日 
*/
int LinkedList::lengthLinkedList(void)
{
  Node* tmp = this->head;
  int len_list = 0;
  while (NULL!=tmp)
  {
    len_list++;
    tmp = tmp->next;
  }
  return len_list;
}

打印输出链表


/*
作者:文方俊
日期:2020年11月18日 
*/
int *values = NULL;
int* LinkedList::printLinkedList(void)
{
  Node* tmp = this->head;
  values = new int[this->lengthLinkedList()];
  int index = 0;
  while (NULL!=tmp)
  {
    values[index] = tmp->getValue();
    tmp = tmp->next;
    index++;
  }
  return values;
}


void LinkedList::printValues(void)
{
  std::cout << "打印输出链表:";
  int *values = this->printLinkedList();
  int len_list = this->lengthLinkedList();
  int index = 0;
  for (; index < len_list; index++)
  {
    if ((index + 1) != len_list)
    {
      std::cout << values[index] << "->";
    }
    else
    {
      std::cout << values[index];
    }
  }
  std::cout << std::endl;
}


链表实现文件:LinkedList.cpp

/*
作者:文方俊
日期:2020年11月18日 
*/
#include "stdafx.h"
#include "LinkedList.h"

#include <iostream>
using std::cout;

LinkedList::LinkedList()
{
  this->head = NULL;
}


LinkedList::~LinkedList()
{
}


void LinkedList::insertNode(int value)
{
  Node* newNode = new Node(value);
  /*头节点为空*/
  if (NULL == this->head)
  {
    this->head = newNode;
  }
  else {
    /*头节点不为空*/
    newNode->next = this->head;
    this->head = newNode;
  }
}

Node* LinkedList::deleteNode(int value)
{
  /*头节点为空*/
  if (NULL == this->head)
  {
    return NULL; 
  }
  /*删除头节点*/
  Node* deleteNode = NULL;
  if (value == this->head->getValue())
  {
    deleteNode = this->head; 
    this->head = this->head->next; 
  }
  else {
    /*删除非头节点*/
    Node* preNode = this->head; 
    while ((NULL != preNode->next) && (value != preNode->next->getValue()))
    {
      preNode = preNode->next; 
    }
    if (NULL == preNode->next)
    {
      /*删除节点不存在*/
      return NULL; 
    }
    if (value == preNode->next->getValue())
    {
      /*删除节点*/
      deleteNode = preNode->next; 
      preNode->next = preNode->next->next; 
    }
  }
  return deleteNode;
}


int LinkedList::lengthLinkedList(void)
{
  Node* tmp = this->head;
  int len_list = 0;
  while (NULL!=tmp)
  {
    len_list++;
    tmp = tmp->next;
  }
  return len_list;
}

int *values = NULL;
int* LinkedList::printLinkedList(void)
{
  Node* tmp = this->head;
  values = new int[this->lengthLinkedList()];
  int index = 0;
  while (NULL!=tmp)
  {
    values[index] = tmp->getValue();
    tmp = tmp->next;
    index++;
  }
  return values;
}




void LinkedList::printValues(void)
{
  std::cout << "打印输出链表:";
  int *values = this->printLinkedList();
  int len_list = this->lengthLinkedList();
  int index = 0;
  for (; index < len_list; index++)
  {
    if ((index + 1) != len_list)
    {
      std::cout << values[index] << "->";
    }
    else
    {
      std::cout << values[index];
    }
  }
  std::cout << std::endl;
}




完整C++代码实现链表,请关注公众号“AI早知道”获取。。。

未完待续,下一篇继续讲解动态链表的C#语言实现。。。

在这里插入图片描述

在这里插入图片描述

关注“AI早知道”公众号二维码,第一回完,更多精彩下回待续。。。

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

梦-无-殇

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

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

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

打赏作者

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

抵扣说明:

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

余额充值