{"id":"06f0231a-6096-4d74-a5df-dfd9b163180a","name":"Get At In Doubly Linkedlist","description":"1. You are given a partially written DoublyLinkedList class.\r\n2. You are required to complete the body of getAt function. This function is supposed to return value of node at given index. \r\n3. If size of list is zero then return \"ListIsEmpty: -1\".\r\n4. If Inddex is Invalid then return \"IndexIsInValid: -1\". \r\n5. You are required to update head, tail and size as required.\r\n6. Input and Output is managed for you. Just update the code in incomplete function.\r\n\r\nNote -> Use the code snippet and follow the algorithm discussed in question video. The judge can't \r\n force you but the intention is to teach a concept. Play in spirit of the question.\r\n","inputFormat":"input in managed for you.\r\n","outputFormat":"output in managed for you.\r\n","constraints":"0 &lt;= N &lt;= 10^6\r\n","sampleCode":{"cpp":{"code":""},"java":{"code":"import java.util.*;\r\n\r\nclass Main {\r\n\r\n public static class DoublyLinkedList {\r\n private class Node {\r\n int data = 0;\r\n Node prev = null;\r\n Node next = null;\r\n\r\n Node(int data) {\r\n this.data = data;\r\n }\r\n\r\n }\r\n\r\n private Node head = null;\r\n private Node tail = null;\r\n private int size = 0;\r\n\r\n public String toString() {\r\n StringBuilder sb = new StringBuilder();\r\n Node curr = this.head;\r\n sb.append(\"[\");\r\n while (curr != null) {\r\n sb.append(curr.data);\r\n if (curr.next != null)\r\n sb.append(\", \");\r\n curr = curr.next;\r\n }\r\n sb.append(\"]\");\r\n\r\n return sb.toString();\r\n }\r\n\r\n // Exceptions========================================\r\n\r\n private boolean ListIsEmptyException() {\r\n if (this.size == 0) {\r\n System.out.print(\"ListIsEmpty: \");\r\n return true;\r\n }\r\n return false;\r\n }\r\n\r\n private boolean indexIsInvalidException(int index, int leftRange, int rightRange) {\r\n if (index < leftRange || index > rightRange) {\r\n System.out.print(\"IndexIsInValid: \");\r\n return true;\r\n }\r\n return false;\r\n }\r\n\r\n // BasicFunctions======================================\r\n\r\n public int size() {\r\n return this.size;\r\n }\r\n\r\n public boolean isEmpty() {\r\n return this.size == 0;\r\n }\r\n\r\n // AddFunctions======================================\r\n\r\n private void addFirstNode(Node node) {\r\n if (this.size == 0)\r\n this.head = this.tail = node;\r\n else {\r\n node.next = this.head;\r\n this.head.prev = node;\r\n this.head = node;\r\n }\r\n this.size++;\r\n }\r\n\r\n public void addFirst(int val) {\r\n Node node = new Node(val);\r\n addFirstNode(node);\r\n }\r\n\r\n private void addLastNode(Node node) {\r\n if (this.size == 0)\r\n this.head = this.tail = node;\r\n else {\r\n this.tail.next = node;\r\n node.prev = this.tail;\r\n this.tail = node;\r\n }\r\n this.size++;\r\n }\r\n\r\n public void addLast(int val) {\r\n Node node = new Node(val);\r\n addLastNode(node);\r\n }\r\n\r\n // RemoveFunctions======================================\r\n\r\n private Node removeFirstNode() {\r\n Node node = this.head;\r\n if (this.size == 1)\r\n this.head = this.tail = null;\r\n else {\r\n Node nextNode = this.head.next;\r\n nextNode.prev = null;\r\n node.next = null;\r\n\r\n this.head = nextNode;\r\n }\r\n\r\n this.size--;\r\n return node;\r\n }\r\n\r\n public int removeFirst() {\r\n if (ListIsEmptyException())\r\n return -1;\r\n Node node = removeFirstNode();\r\n return node.data;\r\n }\r\n\r\n private Node removeLastNode() {\r\n Node node = this.tail;\r\n if (this.size == 1)\r\n this.head = this.tail = null;\r\n else {\r\n Node prevNode = this.tail.prev;\r\n prevNode.next = null;\r\n node.prev = null;\r\n\r\n this.tail = prevNode;\r\n }\r\n\r\n this.size--;\r\n return node;\r\n }\r\n\r\n public int removeLast() {\r\n if (ListIsEmptyException())\r\n return -1;\r\n Node node = removeLastNode();\r\n return node.data;\r\n }\r\n\r\n // getFunctions======================================\r\n\r\n public int getFirst() {\r\n if (ListIsEmptyException())\r\n return -1;\r\n\r\n return this.head.data;\r\n }\r\n\r\n public int getLast() {\r\n if (ListIsEmptyException())\r\n return -1;\r\n\r\n return this.tail.data;\r\n }\r\n\r\n public int getAt(int index) {\r\n\r\n }\r\n\r\n }\r\n\r\n public static void main(String[] args) {\r\n Scanner scn = new Scanner(System.in);\r\n DoublyLinkedList dll = new DoublyLinkedList();\r\n\r\n String str = scn.nextLine();\r\n while (!str.equals(\"stop\")) {\r\n String[] s = str.split(\" \");\r\n if (s[0].equals(\"addFirst\"))\r\n dll.addFirst(Integer.parseInt(s[1]));\r\n else if (s[0].equals(\"addLast\"))\r\n dll.addLast(Integer.parseInt(s[1]));\r\n else if (s[0].equals(\"removeFirst\"))\r\n System.out.println(dll.removeFirst());\r\n else if (s[0].equals(\"removeLast\"))\r\n System.out.println(dll.removeLast());\r\n else if (s[0].equals(\"getFirst\"))\r\n System.out.println(dll.getFirst());\r\n else if (s[0].equals(\"getLast\"))\r\n System.out.println(dll.getLast());\r\n else if (s[0].equals(\"size\"))\r\n System.out.println(dll.size());\r\n else if (s[0].equals(\"isEmpty\"))\r\n System.out.println(dll.isEmpty());\r\n else if (s[0].equals(\"getAt\"))\r\n System.out.println(dll.getAt(Integer.parseInt(s[1])));\r\n\r\n str = scn.nextLine();\r\n }\r\n\r\n System.out.println(dll);\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"addFirst 10\r\naddFirst 20\r\naddFirst 30\r\naddLast 40\r\naddLast 50\r\nremoveLast \r\nremoveFirst\r\naddFirst 90\r\ngetAt 2\r\nstop","sampleOutput":"50\r\n30\r\n10\r\n[90, 20, 10, 40]","questionVideo":"https://www.youtube.com/embed/QOHrdI41VsI","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"1e4c8949-5890-4d15-be5b-6601c7e2029a","name":"Linked List For Intermediate","slug":"linked-list-for-intermediate-637","type":0},{"id":"71bf4f4c-e6a1-4895-9b1c-2cd88a8c110e","name":"Get At In Doubly Linkedlist","slug":"get-at-in-doubly-linkedlist","type":1}],"next":{"id":"17e5186c-11e6-40e3-9568-0981acd30a6e","name":"Get At In Doubly Linkedlist MCQ","type":0,"slug":"get-at-in-doubly-linkedlist-mcq"},"prev":{"id":"392f59b2-c7c8-410c-bf9d-b7433cbfc55c","name":"Get First And Get Last In Doubly Linkedlist","type":3,"slug":"get-first-and-get-last-in-doubly-linkedlist"}}}

Get At In Doubly Linkedlist

1. You are given a partially written DoublyLinkedList class. 2. You are required to complete the body of getAt function. This function is supposed to return value of node at given index. 3. If size of list is zero then return "ListIsEmpty: -1". 4. If Inddex is Invalid then return "IndexIsInValid: -1". 5. You are required to update head, tail and size as required. 6. Input and Output is managed for you. Just update the code in incomplete function. Note -> Use the code snippet and follow the algorithm discussed in question video. The judge can't force you but the intention is to teach a concept. Play in spirit of the question.

{"id":"06f0231a-6096-4d74-a5df-dfd9b163180a","name":"Get At In Doubly Linkedlist","description":"1. You are given a partially written DoublyLinkedList class.\r\n2. You are required to complete the body of getAt function. This function is supposed to return value of node at given index. \r\n3. If size of list is zero then return \"ListIsEmpty: -1\".\r\n4. If Inddex is Invalid then return \"IndexIsInValid: -1\". \r\n5. You are required to update head, tail and size as required.\r\n6. Input and Output is managed for you. Just update the code in incomplete function.\r\n\r\nNote -> Use the code snippet and follow the algorithm discussed in question video. The judge can't \r\n force you but the intention is to teach a concept. Play in spirit of the question.\r\n","inputFormat":"input in managed for you.\r\n","outputFormat":"output in managed for you.\r\n","constraints":"0 &lt;= N &lt;= 10^6\r\n","sampleCode":{"cpp":{"code":""},"java":{"code":"import java.util.*;\r\n\r\nclass Main {\r\n\r\n public static class DoublyLinkedList {\r\n private class Node {\r\n int data = 0;\r\n Node prev = null;\r\n Node next = null;\r\n\r\n Node(int data) {\r\n this.data = data;\r\n }\r\n\r\n }\r\n\r\n private Node head = null;\r\n private Node tail = null;\r\n private int size = 0;\r\n\r\n public String toString() {\r\n StringBuilder sb = new StringBuilder();\r\n Node curr = this.head;\r\n sb.append(\"[\");\r\n while (curr != null) {\r\n sb.append(curr.data);\r\n if (curr.next != null)\r\n sb.append(\", \");\r\n curr = curr.next;\r\n }\r\n sb.append(\"]\");\r\n\r\n return sb.toString();\r\n }\r\n\r\n // Exceptions========================================\r\n\r\n private boolean ListIsEmptyException() {\r\n if (this.size == 0) {\r\n System.out.print(\"ListIsEmpty: \");\r\n return true;\r\n }\r\n return false;\r\n }\r\n\r\n private boolean indexIsInvalidException(int index, int leftRange, int rightRange) {\r\n if (index < leftRange || index > rightRange) {\r\n System.out.print(\"IndexIsInValid: \");\r\n return true;\r\n }\r\n return false;\r\n }\r\n\r\n // BasicFunctions======================================\r\n\r\n public int size() {\r\n return this.size;\r\n }\r\n\r\n public boolean isEmpty() {\r\n return this.size == 0;\r\n }\r\n\r\n // AddFunctions======================================\r\n\r\n private void addFirstNode(Node node) {\r\n if (this.size == 0)\r\n this.head = this.tail = node;\r\n else {\r\n node.next = this.head;\r\n this.head.prev = node;\r\n this.head = node;\r\n }\r\n this.size++;\r\n }\r\n\r\n public void addFirst(int val) {\r\n Node node = new Node(val);\r\n addFirstNode(node);\r\n }\r\n\r\n private void addLastNode(Node node) {\r\n if (this.size == 0)\r\n this.head = this.tail = node;\r\n else {\r\n this.tail.next = node;\r\n node.prev = this.tail;\r\n this.tail = node;\r\n }\r\n this.size++;\r\n }\r\n\r\n public void addLast(int val) {\r\n Node node = new Node(val);\r\n addLastNode(node);\r\n }\r\n\r\n // RemoveFunctions======================================\r\n\r\n private Node removeFirstNode() {\r\n Node node = this.head;\r\n if (this.size == 1)\r\n this.head = this.tail = null;\r\n else {\r\n Node nextNode = this.head.next;\r\n nextNode.prev = null;\r\n node.next = null;\r\n\r\n this.head = nextNode;\r\n }\r\n\r\n this.size--;\r\n return node;\r\n }\r\n\r\n public int removeFirst() {\r\n if (ListIsEmptyException())\r\n return -1;\r\n Node node = removeFirstNode();\r\n return node.data;\r\n }\r\n\r\n private Node removeLastNode() {\r\n Node node = this.tail;\r\n if (this.size == 1)\r\n this.head = this.tail = null;\r\n else {\r\n Node prevNode = this.tail.prev;\r\n prevNode.next = null;\r\n node.prev = null;\r\n\r\n this.tail = prevNode;\r\n }\r\n\r\n this.size--;\r\n return node;\r\n }\r\n\r\n public int removeLast() {\r\n if (ListIsEmptyException())\r\n return -1;\r\n Node node = removeLastNode();\r\n return node.data;\r\n }\r\n\r\n // getFunctions======================================\r\n\r\n public int getFirst() {\r\n if (ListIsEmptyException())\r\n return -1;\r\n\r\n return this.head.data;\r\n }\r\n\r\n public int getLast() {\r\n if (ListIsEmptyException())\r\n return -1;\r\n\r\n return this.tail.data;\r\n }\r\n\r\n public int getAt(int index) {\r\n\r\n }\r\n\r\n }\r\n\r\n public static void main(String[] args) {\r\n Scanner scn = new Scanner(System.in);\r\n DoublyLinkedList dll = new DoublyLinkedList();\r\n\r\n String str = scn.nextLine();\r\n while (!str.equals(\"stop\")) {\r\n String[] s = str.split(\" \");\r\n if (s[0].equals(\"addFirst\"))\r\n dll.addFirst(Integer.parseInt(s[1]));\r\n else if (s[0].equals(\"addLast\"))\r\n dll.addLast(Integer.parseInt(s[1]));\r\n else if (s[0].equals(\"removeFirst\"))\r\n System.out.println(dll.removeFirst());\r\n else if (s[0].equals(\"removeLast\"))\r\n System.out.println(dll.removeLast());\r\n else if (s[0].equals(\"getFirst\"))\r\n System.out.println(dll.getFirst());\r\n else if (s[0].equals(\"getLast\"))\r\n System.out.println(dll.getLast());\r\n else if (s[0].equals(\"size\"))\r\n System.out.println(dll.size());\r\n else if (s[0].equals(\"isEmpty\"))\r\n System.out.println(dll.isEmpty());\r\n else if (s[0].equals(\"getAt\"))\r\n System.out.println(dll.getAt(Integer.parseInt(s[1])));\r\n\r\n str = scn.nextLine();\r\n }\r\n\r\n System.out.println(dll);\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"addFirst 10\r\naddFirst 20\r\naddFirst 30\r\naddLast 40\r\naddLast 50\r\nremoveLast \r\nremoveFirst\r\naddFirst 90\r\ngetAt 2\r\nstop","sampleOutput":"50\r\n30\r\n10\r\n[90, 20, 10, 40]","questionVideo":"https://www.youtube.com/embed/QOHrdI41VsI","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"1e4c8949-5890-4d15-be5b-6601c7e2029a","name":"Linked List For Intermediate","slug":"linked-list-for-intermediate-637","type":0},{"id":"71bf4f4c-e6a1-4895-9b1c-2cd88a8c110e","name":"Get At In Doubly Linkedlist","slug":"get-at-in-doubly-linkedlist","type":1}],"next":{"id":"17e5186c-11e6-40e3-9568-0981acd30a6e","name":"Get At In Doubly Linkedlist MCQ","type":0,"slug":"get-at-in-doubly-linkedlist-mcq"},"prev":{"id":"392f59b2-c7c8-410c-bf9d-b7433cbfc55c","name":"Get First And Get Last In Doubly Linkedlist","type":3,"slug":"get-first-and-get-last-in-doubly-linkedlist"}}}
plane

Editor


Loading...

Get At In Doubly Linkedlist

easy

1. You are given a partially written DoublyLinkedList class. 2. You are required to complete the body of getAt function. This function is supposed to return value of node at given index. 3. If size of list is zero then return "ListIsEmpty: -1". 4. If Inddex is Invalid then return "IndexIsInValid: -1". 5. You are required to update head, tail and size as required. 6. Input and Output is managed for you. Just update the code in incomplete function. Note -> Use the code snippet and follow the algorithm discussed in question video. The judge can't force you but the intention is to teach a concept. Play in spirit of the question.

Constraints

0 <= N <= 10^6

Format

Input

input in managed for you.

Output

output in managed for you.

Example

Sample Input

addFirst 10 addFirst 20 addFirst 30 addLast 40 addLast 50 removeLast removeFirst addFirst 90 getAt 2 stop

Sample Output

50 30 10 [90, 20, 10, 40]

Question Video

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode