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 }

 

 

转载于:https://www.cnblogs.com/YipWingTim/archive/2011/11/08/2241635.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值