11423 - Simulating Iterators   

Description

Warning: You are not allowed to use:

1. any static variables

2. any variables which is not inside a function

3. malloc and free

You are required to implement a iterator.

Random_iter is a array-like pointer which goes next or previous element by increasing or decreasing one memory address.

Bidirect_iter is a linked list-like pointer which goes next or previous element by the next or prev data member of Node.

(After you learning template, you will know how iterator exactly work.)

Input

The first line contains a non-negative integer N (0<N<100) that specifies the number of elements.

The second line contains 2*N integer that specifies the next position of elements and the value ([-32767,32768)) of the next element.

For example, 4 24713 2 21367 5 -3338 1 1893 3 -4723 0 -16458 means

node 0: value is -16458, next is node 4

node 1: value is 1893, next is node 2

node 2: value is 21367, next is node 5

node 3: value is -4723, next is node 1

...

The thrid line contains a non-negative integer H (H<N) that specifies which element is head.

The following lines contain an instruction I (0<=I<4). The meaning of I is shown below:

0 means distance. Containg a non-negative P (P<N, from input) that specifies the distance from head. You have to display the distance between current position and P.

1 means set value. Replace the value of current position with input integer.

2 means next. Set the current position to next.

3 means prev. Set the current position to previous.

Current position is initialized with 0, which points to head.

Output

Complete Node (constructor), Iter (constructor), Random_iter (distance_, next_, prev_ and constructor) and Bidirect_iter (distance_, next_, prev_ and constructor).

Sample Input  Download

Sample Output  Download

Partial Judge Code

11423.cpp

Partial Judge Header

11423.h

Tags




Discuss