《算法思维——一种问题驱动的思维方式》之第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早知道”公众号二维码,第一回完,更多精彩下回待续。。。