{"id":"a960defa-a63c-439e-b02e-755b8b84cc19","name":"Remove First In Doubly Linkedlist","description":"1. You are given a partially written DoublyLinkedList class.\r\n2. You are required to complete the body of removeFirst function. This function is supposed to add an element to the front of LinkedList. \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 removeFirst 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":"#include<iostream>\nusing namespace std;\n#include<vector>\n\n\nclass DoublyLinkedList {\n public : class Node {\n public:\n int data = 0;\n Node* prev = nullptr;\n Node* next = nullptr;\n\n Node(int data) {\n this->data = data;\n }\n\n };\n\n Node* head = nullptr;\n Node* tail = nullptr;\n int size = 0;\n\n string toString() {\n string sb;\n Node* curr = this->head;\n sb += \"[\";\n while (curr != nullptr) {\n sb += (to_string)(curr->data);\n if (curr->next != nullptr)\n sb+=\", \";\n curr = curr->next;\n }\n sb += \"]\";\n\n return sb;\n }\n\n // Exceptions========================================\n\n private:bool ListIsEmptyException() {\n if (this->size == 0) {\n cout << \"ListIsEmpty: \";\n return true;\n }\n return false;\n }\n\n // BasicFunctions======================================\n\n public:int Size() {\n // write your code here\n return size;\n }\n\n public:bool isEmpty() {\n // write your code here\n return size==0;\n }\n\n // AddFunctions======================================\n\n public:void addFirstNode(Node* node) {\n if (this->size == 0)\n this->head = this->tail = node;\n else {\n node->next = this->head;\n this->head->prev = node;\n this->head = node;\n }\n this->size++;\n }\n\n public: void addFirst(int val) {\n Node* node = new Node(val);\n addFirstNode(node);\n }\n\n public:void addLastNode(Node* node) {\n if (this->size == 0)\n this->head = this->tail = node;\n else {\n this->tail->next = node;\n node->prev = this->tail;\n this->tail = node;\n }\n this->size++;\n }\n\n public:void addLast(int val) {\n Node* node = new Node(val);\n addLastNode(node);\n }\n\n // RemoveFunctions======================================\n \n public:int removeFirst() {\n //Write your code here\n }\n\n };\n \n\n int main() {\n DoublyLinkedList *dll = new DoublyLinkedList();\n \n string str;\n cin >> str;\n \n while(str != \"stop\"){\n int data;\n if(str == (\"addFirst\")){\n cin >> data;\n dll->addFirst(data);\n }\n else if (str == (\"addLast\")){\n cin >> data;\n dll->addLast((data));\n }\n else if (str == (\"removeFirst\"))\n cout << (dll->removeFirst()) << endl;\n cin >> str;\n }\n cout << dll->toString();\n \n return 0; \n }"},"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 // 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 public int removeFirst() {\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\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\naddLast 5\r\naddFirst 7\r\naddLast 1\r\nremoveFirst\r\nremoveFirst\r\nremoveFirst\r\nremoveFirst\r\nremoveFirst\r\nremoveFirst\r\nstop","sampleOutput":"7\r\n4\r\n4\r\n5\r\n1\r\nListIsEmpty: -1\r\n[]\r\n","questionVideo":"https://www.youtube.com/embed/CWmo8t4h6a0","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":"7cd45138-81f5-40d3-8d7a-eda6c8222f71","name":"Remove First In Doubly Linkedlist","slug":"remove-first-in-doubly-linkedlist","type":1}],"next":{"id":"57f19c2e-b2b6-4e0d-874f-f70a077d0c37","name":"Remove First In Doubly Linked List","type":3,"slug":"remove-first-in-doubly-linked-list"},"prev":{"id":"d23ce15c-ccc1-4454-8409-d3aa2ca71f12","name":"Add Last In Doubly Linked List","type":3,"slug":"add-last-in-doubly-linked-list"}}}

Remove First In Doubly Linkedlist

1. You are given a partially written DoublyLinkedList class. 2. You are required to complete the body of removeFirst function. This function is supposed to add an element to the front of LinkedList. 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 removeFirst 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":"a960defa-a63c-439e-b02e-755b8b84cc19","name":"Remove First In Doubly Linkedlist","description":"1. You are given a partially written DoublyLinkedList class.\r\n2. You are required to complete the body of removeFirst function. This function is supposed to add an element to the front of LinkedList. \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 removeFirst 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":"#include<iostream>\nusing namespace std;\n#include<vector>\n\n\nclass DoublyLinkedList {\n public : class Node {\n public:\n int data = 0;\n Node* prev = nullptr;\n Node* next = nullptr;\n\n Node(int data) {\n this->data = data;\n }\n\n };\n\n Node* head = nullptr;\n Node* tail = nullptr;\n int size = 0;\n\n string toString() {\n string sb;\n Node* curr = this->head;\n sb += \"[\";\n while (curr != nullptr) {\n sb += (to_string)(curr->data);\n if (curr->next != nullptr)\n sb+=\", \";\n curr = curr->next;\n }\n sb += \"]\";\n\n return sb;\n }\n\n // Exceptions========================================\n\n private:bool ListIsEmptyException() {\n if (this->size == 0) {\n cout << \"ListIsEmpty: \";\n return true;\n }\n return false;\n }\n\n // BasicFunctions======================================\n\n public:int Size() {\n // write your code here\n return size;\n }\n\n public:bool isEmpty() {\n // write your code here\n return size==0;\n }\n\n // AddFunctions======================================\n\n public:void addFirstNode(Node* node) {\n if (this->size == 0)\n this->head = this->tail = node;\n else {\n node->next = this->head;\n this->head->prev = node;\n this->head = node;\n }\n this->size++;\n }\n\n public: void addFirst(int val) {\n Node* node = new Node(val);\n addFirstNode(node);\n }\n\n public:void addLastNode(Node* node) {\n if (this->size == 0)\n this->head = this->tail = node;\n else {\n this->tail->next = node;\n node->prev = this->tail;\n this->tail = node;\n }\n this->size++;\n }\n\n public:void addLast(int val) {\n Node* node = new Node(val);\n addLastNode(node);\n }\n\n // RemoveFunctions======================================\n \n public:int removeFirst() {\n //Write your code here\n }\n\n };\n \n\n int main() {\n DoublyLinkedList *dll = new DoublyLinkedList();\n \n string str;\n cin >> str;\n \n while(str != \"stop\"){\n int data;\n if(str == (\"addFirst\")){\n cin >> data;\n dll->addFirst(data);\n }\n else if (str == (\"addLast\")){\n cin >> data;\n dll->addLast((data));\n }\n else if (str == (\"removeFirst\"))\n cout << (dll->removeFirst()) << endl;\n cin >> str;\n }\n cout << dll->toString();\n \n return 0; \n }"},"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 // 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 public int removeFirst() {\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\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\naddLast 5\r\naddFirst 7\r\naddLast 1\r\nremoveFirst\r\nremoveFirst\r\nremoveFirst\r\nremoveFirst\r\nremoveFirst\r\nremoveFirst\r\nstop","sampleOutput":"7\r\n4\r\n4\r\n5\r\n1\r\nListIsEmpty: -1\r\n[]\r\n","questionVideo":"https://www.youtube.com/embed/CWmo8t4h6a0","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":"7cd45138-81f5-40d3-8d7a-eda6c8222f71","name":"Remove First In Doubly Linkedlist","slug":"remove-first-in-doubly-linkedlist","type":1}],"next":{"id":"57f19c2e-b2b6-4e0d-874f-f70a077d0c37","name":"Remove First In Doubly Linked List","type":3,"slug":"remove-first-in-doubly-linked-list"},"prev":{"id":"d23ce15c-ccc1-4454-8409-d3aa2ca71f12","name":"Add Last In Doubly Linked List","type":3,"slug":"add-last-in-doubly-linked-list"}}}
plane

Editor


Loading...

Remove First In Doubly Linkedlist

easy

1. You are given a partially written DoublyLinkedList class. 2. You are required to complete the body of removeFirst function. This function is supposed to add an element to the front of LinkedList. 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 removeFirst 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 addLast 5 addFirst 7 addLast 1 removeFirst removeFirst removeFirst removeFirst removeFirst removeFirst stop

Sample Output

7 4 4 5 1 ListIsEmpty: -1 []

Question Video

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode