import java.io.*;
import java.util.Scanner;
// Class for Doubly Linked List
public class DoublyLinkedList {
// class for Node
class Node {
int data;
Node prev;
Node next;
// Constructor to create a new node with given data d
// next and prev is by default initialized as null
Node(int d) { data = d; }
}
Node head;
Node tail;
int size;
// Inserting node at the front of the list
public void insertFirst(int data)
{
Node node = new Node(data);
if(head==null){
tail = node;
}
else{
head.prev = node;
}
node.next = head;
head = node;
size++;
}
// Inserting at node at the end of the list
void insertLast(int data)
{
Node node = new Node(data);
if(head==null){
head = node;
}
else{
tail.next = node;
node.prev = tail;
}
tail = node;
size++;
}
public int deleteFirst(){
if(head == null){
throw new RuntimeException("List is empty");
}
int temp = head.data;
if(head.next == null){
tail = null;
}
else{
head.next.prev = null;
}
head = head.next;
size--;
return temp;
}
public int deleteLast(){
if(tail == null){
throw new RuntimeException("List is empty");
}
Node last = tail;
if(head.next == null){
head = null;
}
else{
tail.prev.next = null;
}
tail = tail.prev;
size--;
return last.data;
}
public void printList()
{
Node node = head;
while (node != null) {
System.out.print(node.data + " ");
node = node.next;
}
System.out.println();
}
public void insertAfter(Node current, int data)
{
if (current == null) {
System.out.println("The node cannot be NULL ");
return;
}
Node new_node = new Node(data);
new_node.next = current.next;
current.next = new_node;
new_node.prev = current;
if (new_node.next != null)
new_node.next.prev = new_node;
size++;
}
public int deleteAt(Node current)
{
if (head == null || current == null) {
throw new RuntimeException("Empty Node");
}
int temp = current.data;
if (head == current) {
head = current.next;
}
if (current.next != null) {
current.next.prev = current.prev;
}
if (current.prev != null) {
current.prev.next = current.next;
}
size--;
return temp;
}
public static void main(String[] args)throws Exception
{
DoublyLinkedList dll = new DoublyLinkedList();
FileInputStream fis=new FileInputStream("/home/prasanna/Downloads/str.txt");
Scanner sc=new Scanner(fis); //file to be scanned
int count = Integer.parseInt(sc.nextLine()); //
for (int i=0; i<count; i++) {
String[] line = sc.nextLine().trim().split(" "); /*split the rows with space and storing*/
if(line.length>1){
int data = Integer.parseInt(line[1]);
if(line[0].equals("INSF")){
dll.insertFirst(data);
}
if(line[0].equals("INSL")){
dll.insertLast(data);
}
}
if(line[0].equals("DELF")){
dll.deleteFirst();
}
if(line[0].equals("DELL")){
dll.deleteLast();
}
if(line[0].equals("PRIN")){
dll.printList();
}
}
sc.close(); //closes the scanner
}
}