#K2971. Doubly Linked List Operations

    ID: 24855 Type: Default 1000ms 256MiB

Doubly Linked List Operations

Doubly Linked List Operations

You are required to implement a doubly linked list that supports several operations. The operations are provided as commands through standard input. The supported commands are:

  • append x: Adds a node with value \(x\) to the end of the list.
  • prepend x: Adds a node with value \(x\) to the beginning of the list.
  • find x: Searches for the first node with value \(x\) in the list and outputs its 1-indexed position. If \(x\) is not found, outputs \(-1\).
  • delete x: Deletes the first occurrence of a node with value \(x\) from the list. If \(x\) is not found, no action is performed.
  • reverse: Reverses the order of the nodes in the list.
  • print_list: Prints the values of the list in two lines. The first line prints the list from head to tail, and the second line prints it from tail to head. Values are separated by a single space.

The input starts with an integer \(n\) which indicates how many commands follow. Each of the subsequent \(n\) lines contains one command. Your program should process the commands sequentially and output the result for each find command and each print_list command.

inputFormat

The first line contains an integer \(n\), the number of commands. Each of the next \(n\) lines contains a command. A command is one of the following:

  • append x
  • prepend x
  • find x
  • delete x
  • reverse
  • print_list

Here \(x\) is an integer.

outputFormat

For each find command, output a single line with the 1-indexed position of \(x\) in the list (or \(-1\) if not found). For each print_list command, output two lines. The first line is a space-separated list of node values from head to tail, and the second line is the list of node values from tail to head. Other commands do not produce any output.

## sample
6
append 1
append 2
find 1
find 2
find 3
print_list
1

2 -1 1 2 2 1

</p>