{"id":"bb212479-70e8-478f-aa96-758bde0c9088","name":"Get First And Get Last In Doubly Linkedlist","description":"1. You are given a partially written DoublyLinkedList class.\r\n2. You are required to complete the body of size, isEmpty, getFirst and getLast function. This function is supposed to return size, empty state of linkedlist, first element and last element respectively. \r\n3. If size of list is zero then return \"ListIsEmpty: -1\". \r\n4. You are required to update head, tail and size as required.\r\n5. 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 <= N <= 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 // BasicFunctions======================================\r\n\r\n public int size() {\r\n\r\n }\r\n\r\n public boolean isEmpty() {\r\n\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\r\n }\r\n\r\n public int getLast() {\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\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 4\r\naddFirst 4\r\naddFirst 5\r\naddFirst 7\r\naddFirst 1\r\nstop","sampleOutput":"[1, 7, 5, 4, 4]\r\n","questionVideo":"https://www.youtube.com/embed/HIJBH1iDX_U","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":"3e6c8b3a-56fc-4211-b727-9118e4b9832d","name":"Get First And Get Last In Doubly Linkedlist","slug":"get-first-and-get-last-in-doubly-linkedlist","type":1}],"next":{"id":"e2709a40-1c82-4dd2-8da7-f71dee03b0b5","name":"Get First And Get Last In Doubly Linkedlist MCQ","type":0,"slug":"get-first-and-get-last-in-doubly-linkedlist-mcq"},"prev":{"id":"e8a09549-5527-4377-bd4d-5c8502d8ff3d","name":"Remove Last In Doubly Linked List","type":3,"slug":"remove-last-in-doubly-linked-list"}}}

Get First And Get Last In Doubly Linkedlist

1. You are given a partially written DoublyLinkedList class. 2. You are required to complete the body of size, isEmpty, getFirst and getLast function. This function is supposed to return size, empty state of linkedlist, first element and last element respectively. 3. If size of list is zero then return "ListIsEmpty: -1". 4. You are required to update head, tail and size as required. 5. 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":"bb212479-70e8-478f-aa96-758bde0c9088","name":"Get First And Get Last In Doubly Linkedlist","description":"1. You are given a partially written DoublyLinkedList class.\r\n2. You are required to complete the body of size, isEmpty, getFirst and getLast function. This function is supposed to return size, empty state of linkedlist, first element and last element respectively. \r\n3. If size of list is zero then return \"ListIsEmpty: -1\". \r\n4. You are required to update head, tail and size as required.\r\n5. 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 <= N <= 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 // BasicFunctions======================================\r\n\r\n public int size() {\r\n\r\n }\r\n\r\n public boolean isEmpty() {\r\n\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\r\n }\r\n\r\n public int getLast() {\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\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 4\r\naddFirst 4\r\naddFirst 5\r\naddFirst 7\r\naddFirst 1\r\nstop","sampleOutput":"[1, 7, 5, 4, 4]\r\n","questionVideo":"https://www.youtube.com/embed/HIJBH1iDX_U","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":"3e6c8b3a-56fc-4211-b727-9118e4b9832d","name":"Get First And Get Last In Doubly Linkedlist","slug":"get-first-and-get-last-in-doubly-linkedlist","type":1}],"next":{"id":"e2709a40-1c82-4dd2-8da7-f71dee03b0b5","name":"Get First And Get Last In Doubly Linkedlist MCQ","type":0,"slug":"get-first-and-get-last-in-doubly-linkedlist-mcq"},"prev":{"id":"e8a09549-5527-4377-bd4d-5c8502d8ff3d","name":"Remove Last In Doubly Linked List","type":3,"slug":"remove-last-in-doubly-linked-list"}}}
plane

Editor


Loading...

Get First And Get Last In Doubly Linkedlist

easy

1. You are given a partially written DoublyLinkedList class. 2. You are required to complete the body of size, isEmpty, getFirst and getLast function. This function is supposed to return size, empty state of linkedlist, first element and last element respectively. 3. If size of list is zero then return "ListIsEmpty: -1". 4. You are required to update head, tail and size as required. 5. 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 4 addFirst 4 addFirst 5 addFirst 7 addFirst 1 stop

Sample Output

[1, 7, 5, 4, 4]

Question Video

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode