别摸鱼啦,说的就是你,学习编程从入门到放弃。掌握编程思维方式,远胜于死记硬背编程语法,不能再迷路了。
图一 链表结构示意图
图二 链表创建流程示意图
为了清晰明了的理解单项链表的操作,请参考链表创建流程示意图
/* 新创建的一个节点 */
myData *newNode = new myData;/* 1. 将当前节点指向 newNode, 2. 并将新创建节点的nextNode指向nullptr3. 将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指向nullptr3. 将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 << "nn" << 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:%sn", LinkedHeader->name.data());printf("tstaff ages:%dn", LinkedHeader->ages);printf("tstaff education:%sn", LinkedHeader->education.data());printf("tstaff post:%sn", LinkedHeader->post.data());printf("tstaff seniority:%dn", LinkedHeader->seniority);printf("tstaff salary:%dn", 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: ateLinkedList(); 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;
}
下期讲解双向链表,动动发财的小手点个关注再走呗
本文发布于:2024-01-31 17:34:55,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170669369630224.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |