别摸鱼啦,说的就是你,学习编程从入门到放弃。掌握编程思维方式,远胜于死记硬背编程语法,不能再迷路了。
单向链表
图一 链表结构示意图
图二 链表创建流程示意图
为了清晰明了的理解单项链表的操作,请参考链表创建流程示意图
/* 新创建的一个节点 */
myData *newNode = new myData;
/* 1. 将当前节点指向 newNode,
2. 并将新创建节点的nextNode指向nullptr
3. 将currentNode 指向新创建的节点
*/
currentNode->nextNode = newNode;
newNode->nextNode = nullptr
currentNode = newNode;
链表创建核心代码
为防止出现野指针需要将尾部node的nextNode指向nullptr
StaffInfomation *headerNode,
*currentNode;
void SingleLinkedList::createLinkedList()
{
StaffInfomation *staffInfo = new StaffInfomation{0};
/*
headerNode 指向的是头节点
currentNode 指向的是当前节点
这样链表的node 就串起来了
*/
if (!headerNode && !currentNode) {
headerNode = currentNode = staffInfo;
} else {
currentNode->nextNode = staffInfo;
currentNode = staffInfo;
}
return;
}
单向循环链表
图三 单项循环链表示意图
链表创建核心代码
要将尾部节点的nextNode指向头部节点headerNode
/* 新创建的一个节点 */
myData *newNode = new myData;
/* 1. 将当前节点指向 newNode,
2. 并将新创建节点的nextNode指向nullptr
3. 将currentNode 指向新创建的节点
*/
currentNode->nextNode = newNode;
newNode->nextNode = nullptr
currentNode = newNode;
单向链表的插入操作
图四 链表插入流程示意图
需要根据自己的业务逻辑找到要进行插入的节点myNode
/* 新创建的一个节点 */
myData *newNode = new myData;
newNode->nextNode = myNode->nextNode;
myNode->nexNode = newNode;
单向链表的遍历操作
单向链表的遍历先找到headerNode, 每次遍历需要将currentNode指向currentNode的下一个节点,直到尾部节点
MyData* currentNode = headerNode;
while (currentNode) {
/* doSomething */
currentNode = currentNode->nextNode;
}
呈上单向链表的完整示例代码,供给小伙伴学习交流,赶紧 ctrl-c ctrl-v 测试一下吧
/********************************************************
* Description: simply C++ linked demo
* Author: jiangxiaoyu
* Data: Fir Apr 28 2023
*********************************************************/
#include <iostream>
#include <string>
#include <regex>
#include <limits>
#include <ctype.h>
typedef struct {
int ages;
int salary;
int seniority;
std::string name;
std::string post;
std::string education;
void * nextNode;
void * previousNode;
} StaffInfomation;
class SingleLinkedList {
public:
SingleLinkedList();
void helper();
bool isRegexInput(std::string str);
std::string regexDigit(std::string str, std::string msg);
void createLinkedList();
void collectStaffInfomation(StaffInfomation *);
void printStaffInfomation();
void destoryStaffInfomation();
void exitSystem();
private:
StaffInfomation* headerNode,
* currentNode;
};
/* initialize member */
SingleLinkedList::SingleLinkedList():
headerNode(nullptr),
currentNode(nullptr) {
}
void SingleLinkedList::helper()
{
std::cout << "\n\n" << std::endl;
std::cout << "Simply staff information manager syatem" << std::endl;
std::cout << "1) Create New Staff Information" << std::endl;
std::cout << "2) Printf All Staff Information" << std::endl;
std::cout << "3) Destory All Staff Information" << std::endl;
std::cout << "4) Helper Manual" << std::endl;
std::cout << "5) Exit" << std::endl;
}
void SingleLinkedList::collectStaffInfomation(StaffInfomation * staffInfo)
{
std::cout << "-------------------------------" << std::endl;
std::cout << "staff information system" << std::endl;
std::cout << "\nstaff name:"; std::cin >> staffInfo->name;
std::cout << "\nstaff ages:"; std::cin >> staffInfo->ages;
std::cout << "\nstaff education:"; std::cin >> staffInfo->education;
std::cout << "\nstaff post:"; std::cin >> staffInfo->post;
std::cout << "\nstaff seniority:"; std::cin >> staffInfo->seniority;
std::cout << "\nstaff salary:"; std::cin >> staffInfo->salary;
}
void SingleLinkedList::printStaffInfomation()
{
StaffInfomation * LinkedHeader = headerNode;
while (LinkedHeader) {
std::cout << "-------------------------------" << std::endl;
std::cout << "\tstaff information system" << std::endl;
printf("\tstaff name:%s\n", LinkedHeader->name.data());
printf("\tstaff ages:%d\n", LinkedHeader->ages);
printf("\tstaff education:%s\n", LinkedHeader->education.data());
printf("\tstaff post:%s\n", LinkedHeader->post.data());
printf("\tstaff seniority:%d\n", LinkedHeader->seniority);
printf("\tstaff salary:%d\n", LinkedHeader->salary);
std::cout << "-------------------------------" << std::endl;
LinkedHeader = (StaffInfomation *)LinkedHeader->nextNode;
}
}
void SingleLinkedList::createLinkedList()
{
StaffInfomation * staffInfo = new StaffInfomation{0};
staffInfo->nextNode = nullptr;
/* linked list header->next -> next list/ header->next list */
if (!headerNode && !currentNode) {
headerNode = currentNode = staffInfo;
}
else {
currentNode->nextNode = staffInfo;
currentNode = staffInfo;
}
collectStaffInfomation(staffInfo);
}
void SingleLinkedList::destoryStaffInfomation()
{
while (headerNode) {
delete headerNode;
headerNode = (StaffInfomation *)currentNode->nextNode;
}
}
int main(int argc, char ** argv)
{
SingleLinkedList test;
do {
test.helper();
int cmd = 0; std::cout << "please input flag:"; std::cin >> cmd;
switch (cmd) {
case 1: test.createLinkedList(); break;
case 2: test.printStaffInfomation(); break;
case 3: test.destoryStaffInfomation(); break;
case 4: test.helper(); break;
default: std::cout << "input invalid!" << std::endl;
}
} while(true);
return 0;
}
下期讲解双向链表,动动发财的小手点个关注再走呗