zoukankan      html  css  js  c++  java
  • List二级链表

    二级链表的实现,以及把二级链表展开成一级链表,

    关键是数据结构的设计,

    这里我用了二级链表的节点类,它包括了两个指针,一个指向一个二级链表节点,一个指向单链表的头结点。

    生成一个二级链表默认构造函数是只含有一个头结点为二级链表节点(空),

    插入的时候是插入一个单链表指针不为空,但是二级链表节点为空的二级链表节点。

    View Code
      1 //二级单链表
    2 #include<iostream>
    3 using namespace std;
    4
    5 template<class Type> struct LinkNode
    6 {
    7 public:
    8 Type data;
    9 LinkNode<Type> *link;
    10
    11 LinkNode(LinkNode<Type>* ptr=NULL)
    12 {
    13 link=ptr;
    14 }
    15 LinkNode(const Type& item,LinkNode<Type>* ptr=NULL)
    16 {
    17 data=item;
    18 link=ptr;
    19 }
    20 };
    21
    22 template<class Type> struct CascadeNode
    23 {
    24 public:
    25 CascadeNode<Type>* next;
    26 LinkNode<Type>* head;
    27
    28 CascadeNode(LinkNode<Type> *Head=NULL,CascadeNode<Type> *Next=NULL)
    29 {
    30 next=Next;
    31 head=Head;
    32 }
    33 };
    34
    35 template<class Type> class List
    36 {
    37 // friend class CascadeList<Type>;
    38 public:
    39 List()
    40 {
    41 first=new LinkNode<Type>;
    42 }
    43 List(LinkNode<Type>* head)
    44 {
    45 first=head;
    46 }
    47 void init(const Type& end)
    48 {
    49 LinkNode<Type> *curr=first;
    50 Type val;
    51 cin>>val;
    52 while(val!=end)
    53 {
    54 LinkNode<Type> *newNode=new LinkNode<Type>(val);
    55 curr->link=newNode;
    56 curr=curr->link;
    57 cin>>val;
    58 }
    59 }
    60 void show()
    61 {
    62 LinkNode<Type> *curr=first;
    63 while(curr->link!=NULL)
    64 {
    65 cout<<curr->link->data<<"";
    66 curr=curr->link;
    67 }
    68 cout<<endl;
    69 }
    70
    71 LinkNode<Type>* getFirst()
    72 {
    73 return first;
    74 }
    75 private:
    76 LinkNode<Type>* first;
    77 };
    78
    79 template<class Type> class CascadeList
    80 {
    81 public:
    82 CascadeList()
    83 {
    84 first=new CascadeNode<Type>;
    85 }
    86
    87 void addCascadeNode(LinkNode<Type>* ptr)
    88 {
    89 CascadeNode<Type> *newCNode=new CascadeNode<Type>(ptr);
    90 CascadeNode<Type> *currCNode=first;
    91 while(currCNode->next!=NULL)
    92 currCNode=currCNode->next;
    93 currCNode->next=newCNode;
    94 }
    95
    96 void show()
    97 {
    98 CascadeNode<Type> *currCNode=first;
    99 int k=1;
    100 while(currCNode->next!=NULL)
    101 {
    102 currCNode=currCNode->next;
    103 cout<<""<<k<<"个节点处的单链表如下:"<<endl;
    104 LinkNode<Type>* currFirst=currCNode->head;
    105 List<Type>* list=new List<Type>(currFirst);
    106 list->show();
    107 k++;
    108 delete list;
    109 }
    110 }
    111
    112 List<Type>* convertToList()
    113 {
    114 List<Type>* list=new List<Type>();
    115 LinkNode<Type>* firstOfList=list->getFirst();
    116 LinkNode<Type>* currNode=firstOfList;
    117 CascadeNode<Type>* currCNode=first->next;
    118 while(currCNode!=NULL)
    119 {
    120 LinkNode<Type>* curr=currCNode->head;
    121 while(curr->link!=NULL)
    122 {
    123 currNode->link=curr->link;
    124 currNode=currNode->link;
    125 curr=curr->link;
    126 }
    127 currCNode=currCNode->next;
    128 }
    129 return list;
    130
    131 }
    132 private:
    133 CascadeNode<Type>* first;
    134 };
    135
    136 int main()
    137 {
    138 List<string> list1,list2,list3,list4,list5;
    139 list1.init("end");
    140 list2.init("end");
    141 list3.init("end");
    142 list4.init("end");
    143 list5.init("end");
    144
    145 list1.show();
    146 list2.show();
    147 list3.show();
    148 list4.show();
    149 list5.show();
    150
    151 CascadeList<string> caslist;
    152 caslist.addCascadeNode(list1.getFirst());
    153 caslist.addCascadeNode(list2.getFirst());
    154 caslist.addCascadeNode(list3.getFirst());
    155 caslist.addCascadeNode(list4.getFirst());
    156 caslist.addCascadeNode(list5.getFirst());
    157 caslist.show();
    158
    159 cout<<"合并后看看:"<<endl;
    160 List<string>* newlist=caslist.convertToList();
    161 newlist->show();
    162 return 0;
    163 }
  • 相关阅读:
    jmeter(46) redis
    jmeter(45) tcp/ip协议
    Codeforces Round #538 (Div. 2)D(区间DP,思维)
    Codeforces Global Round 1D(DP,思维)
    Educational Codeforces Round 57D(DP,思维)
    UPC11073(DP,思维)
    Yahoo Progamming Contest 2019D(DP,思维)
    Atcoder Beginner Contest 118D(DP,完全背包,贪心)
    Xuzhou Winter Camp 1C(模拟)
    Educational Codeforces Round 57 (Rated for Div. 2)D(动态规划)
  • 原文地址:https://www.cnblogs.com/YipWingTim/p/2241635.html
Copyright © 2011-2022 走看看