📄 wex9_16.cpp
字号:
#include <iostream.h>
#pragma hdrstop
#include "link.h"
template <class T>
void PrintList(LinkedList<T>& L)
{
int pos = L.CurrentPosition();
for(L.Reset();!L.EndOfList();L.Next())
cout << L.Data() << " ";
cout << endl;
if (pos != L.ListSize())
L.Reset(pos);
}
// create two new lists L1 and L2. L1 contains the first,
// third, fifth and successive odd-numbered nodes of L.
// L2 contains the even numbered nodes
template <class T>
void Split(LinkedList<T>& L,
LinkedList<T>& L1, LinkedList<T>& L2)
{
// i determines if node is odd or even numbered
int i = 1;
// clear both L1 and L2
L1.ClearList();
L2.ClearList();
// traverse L, building L1 and L2
for(L.Reset();!L.EndOfList();L.Next())
{
// if i is odd, insert data value at rear of L1;
// otherwise, insert data value at rear of L2
if (i%2 != 0)
L1.InsertRear(L.Data());
else
L2.InsertRear(L.Data());
i++;
}
}
void main(void)
{
LinkedList<int> L, L1, L2;
int i;
for(i=1;i <= 13;i++)
L.InsertRear(i);
cout << "List L: ";
PrintList(L);
cout << endl;
Split(L,L1,L2);
cout << "After split:" << endl;
cout << "L1: ";
PrintList(L1);
cout << "L2: ";
PrintList(L2);
}
/*
<Run>
List L: 1 2 3 4 5 6 7 8 9 10 11 12 13
After split:
L1: 1 3 5 7 9 11 13
L2: 2 4 6 8 10 12
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -