📄 wex9_17.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 linked lists L1 and L2. L1 contains the nodes of L
// whose data values are an odd number, and L2 contains nodes
// of L whose data values are even numbers
void OddEven(LinkedList<int>& L,LinkedList<int>& L1,
LinkedList<int>& L2)
{
// move to the first node of L
L.Reset();
// clear L1 and L2
L1.ClearList();
L2.ClearList();
// traverse L and build L1/L2
while (!L.EndOfList())
{
if (L.Data() % 2 == 1)
// insert odd values from L into L1
L1.InsertAfter(L.Data());
else
// insert even values from L into L2
L2.InsertAfter(L.Data());
// move to next node of L
L.Next();
}
}
void main(void)
{
LinkedList<int> L, L1, L2;
int a[] = {5,1,4,7,8,23,56,79,25,3,0,12,15,66,98};
int i;
for(i=0;i < 15;i++)
L.InsertRear(a[i]);
cout << "List L: ";
PrintList(L);
cout << endl;
OddEven(L,L1,L2);
cout << "After OddEven:" << endl;
cout << "L1: ";
PrintList(L1);
cout << "L2: ";
PrintList(L2);
}
/*
<Run>
List L: 5 1 4 7 8 23 56 79 25 3 0 12 15 66 98
After OddEven:
L1: 5 1 7 23 79 25 3 15
L2: 4 8 56 0 12 66 98
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -