#include <iostream> class node{ public: int score; node *last,*next; node() :score(0),next(nullptr),last(nullptr){}; }; node * creat(int n){ node *head=new node,*end=head,*node_media= head; std::cout<<"请输入第1个节点的分数"<<std::endl; std::cin>>head->score; head->last= nullptr; head->next= nullptr; for (int i = 1; i < n; ++i) { end->next = new node; end = end->next; std::cout<<"请输入第"<<i+1<<"个节点的分数"<<std::endl; std::cin>>end->score; end->last=node_media; node_media = end; } return head; } int main() { node *aaa = creat(5); node *bbb =aaa; while (bbb!= nullptr) { std::cout<<bbb->score<<std::endl; bbb=bbb->next; } bbb =aaa; while (bbb->next!= nullptr) { bbb = bbb->next; } while (bbb!= nullptr) { std::cout<<bbb->score<<std::endl; bbb=bbb->last; } }
双向遍历都成功完成,双向遍历的时候瞬间明白了stl标准库迭代器的方便之处.
自己用C++实现的乞丐版的双向链表
最新推荐文章于 2024-05-19 12:01:11 发布