在数据结构的学习中,链表是动态数据结构的最基本的形式,也是最常见的一种线性数据结构,使用范围广。
在创建动态链表时,对不同节点之间的链接关系梳理清楚,这一点十分重要。。。
比如,下面开始用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日
*/
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日
*/
class LinkedList
{
public:
LinkedList();
~LinkedList();
void insertNode(int value);
Node* deleteNode(int value);
int lengthLinkedList();
void printValues();
private:
Node* head;
int* printLinkedList();
};
链表中插入节点,
/*
作者:文方俊
日期: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 = ;
int* :: ( )
{
* = -> ;
values = new int[-> ()];
int index = 0;
while (!= )
{
values[index] = -> ();
= ->next;
index++;
}
return values;
}
:: ( )
{
:: << "打印输出链表:";
int *values = -> ();
int = -> ();
int index = 0;
for (; index < ; index++)
{
if ((index + 1) != )
{
:: << values[index] << "->";
}
else
{
:: << values[index];
}
}
:: << :: ;
}
附录:完成C++代码实现链表
1.链表实现文件:LinkedList.cpp
/*
作者:文方俊
日期:2020年11月18日
*/
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;
}
2.链表测试文件:testLinkedList.cpp
// LinkedListC++.cpp : 定义控制台应用程序的入口点。
//
/*
作者:文方俊
日期:2020年11月18日
*/
/*system("pause");*/
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
Node* node = new Node(123);
std::cout << "Node.value = " << node->getValue()<< std::endl;
delete node;
/*定义新链表*/
LinkedList* list = new LinkedList;
/*在新链表中插入值*/
list->insertNode(1);
list->insertNode(2);
list->insertNode(3);
list->insertNode(4);
for (int i = 10; i < 20; i++)
list->insertNode(i);
std::cout << "LinkedList length = " << list->lengthLinkedList() << std::endl;
std::cout << "int(NULL) = " << int(NULL) << std::endl;
/*打印输出链表*/
list->printValues();
/*删除节点*/
std::cout << "删除节点1" << std::endl;
list->deleteNode(1);
std::cout << "删除节点10" << std::endl;
list->deleteNode(10);
std::cout << "删除节点19" << std::endl;
list->deleteNode(19);
/*打印输出链表*/
list->printValues();
system("pause");
/*
system()是调用DOS系统命令和shell命令;
"pause"即DOS命令集合中的暂停命令;
说明:
void system(char *cmd);
参数cmd,DOS命令,如Pause, cls
返回值:无。
在VC 6.0下,要添加如下的头文件!
#include <stdlib.h> */
return 0;
}
程序打印输出结果如下图所示,
未完待续,下一篇继续讲解动态链表的C#语言实现。。。
关注“AI早知道”公众号二维码,第一回完,更多精彩下回待续。。。
遇见爱or遇见自己or遇见幸福,
再出发,
遇见更精彩的自己
。。。