14053 - OJ4 - Linked List   

Description

In this quiz, you should write a program that can handle a linked list and can operate three commands: insert, delete and show.
For insert, you should insert a certain integer value into a certain position of your linked list.
For delete, you should first get the element N of certain position in your linked list, then compare with a certain integer number M. If N is smaller M, then delete N from your linked list, otherwise delete N and append N to the last element of the linked list.
For show, you should print the value between certain positions in certain order.

Note that you may not use STL in this quiz.

Input

  1. Each line of input contains three values with the format of “[value1] [value2] [value3]”. The value1 represents the commands, which data type is character. The value2 and the value3 are integer, and their meaning will be explained below case by case. The values are separated by blankspace and each line ends with a newline character(\n).
  2. If [value1] is “i”, you should insert [value3] into [value2] position of your linked list. If [value2] is equal to 0, insert [value3] into "first" (root) of your linked list, and if [value2] is equal to -1, append [value3] to the last elements of the linked list.
  3. If [value1] is “d”, you should compare the element N of position [value2] in your linked list with [value3]. If element N is smaller than [value3], then delete N form your linked list, otherwise delete N and append N to the last element of the linked list.
  4. If [value1] is “s”, you should print all the elements form position [value2] to position [value3], include the elements of position [value2] and position [value3]. Note that [value2] is not necessarily smaller than [value3]. If [value2] is larger than [value3], you should print the elements reversely.

Note: In this question, we use 0-indexed to index elements in linked list. For example, a linked list 3 - > 4 -> 5, the element of position 0 is 3, the element of position 1 is 4 and the element of position 2 is 5.

Output

Print the elements between the positions required by the command.
You should separate elements by a blankspace and end up with a blankspace and a newline character (\n).

Sample Input  Download

Sample Output  Download

Tags




Discuss