{"id":"49f044e4-5297-48f8-9e36-9c86036aa6ff","name":"Linked List To Queue Adapter","description":"1. You are required to complete the code of our LLToQueueAdapter class. \r\n2. As data members, you've a linkedlist available in the class.\r\n3. Here is the list of functions that you are supposed to complete\r\n 3.1. add -> Should accept new data in FIFO manner\r\n 3.2. remove -> Should remove and return data in FIFO manner. If not available, \r\n print \"Queue underflow\" and return -1.\r\n 3.3. peek -> Should return data in FIFO manner. If not available, print \"Queue \r\n underflow\" and return -1.\r\n 3.4. size -> Should return the number of elements available in the queue\r\n4. Input and Output is managed for you.\r\n\r\nNote -> The intention is to use linked list functions to achieve the purpose of a queue. All the functions should work in constant time.","inputFormat":"Input is managed for you","outputFormat":"Output is managed for you","constraints":"None","sampleCode":{"cpp":{"code":"#include <iostream>\nusing namespace std;\nclass node{\n public:\n int val;\n node* next;\n \n\n};\n\nclass LinkedList {\n public: \n node* head=nullptr;\n node* tail=nullptr;\n int size_=0;\n \n\nvoid insert_at_tail(int val){\n if(head==NULL){\n node *newnode = new node;\n newnode->val=val;\n newnode->next=NULL;\n head=newnode;\n\n }\n else{\n node *newnode = new node;\n newnode->val=val;\n newnode->next=NULL;\n node *temp = head;\n while(temp->next!=NULL){\n temp=temp->next;\n }\n temp->next = newnode;\n }\n\n}\nvoid insertion_at_head(int val){\n node *newnode = new node;\n newnode->val=val;\n newnode->next=NULL;\n if(head==NULL){\n head=newnode;\n }\n else{\n newnode ->next =head;\n head = newnode;\n }\n\n}\nvoid print (){\n node *temp =head;\n if(head==NULL){\n cout << \"0\" << endl;\n return;\n }\n while(temp!=NULL){\n cout<<temp->val<<\" \";\n temp=temp->next;\n }\n cout << endl;\n}\nint deletion_at_head(){\n if(head==NULL) return -1;\n int tr=head->val;\n node *temp=head;\n head=head->next;\n \n delete temp;\n return tr;\n\n}\nvoid deletion_at_tail(){\n if(head==NULL) return;\n node* previous=NULL;\n node* temp=head;\n while(temp->next!=NULL){\n previous=temp;\n temp=temp->next;\n }\n previous->next = NULL;\n delete temp; \n}\n\n\nvoid last(){\n node* temp=head;\n while(temp->next!=NULL){\n temp=temp->next;\n }\n cout << temp->val << endl;\n}\nint size(){\n int cnt=0;\n node* temp=head;\n while(temp!=NULL){\n temp=temp->next;\n cnt++;\n }\n return cnt;\n}\nint first(){\n return head->val ;\n}\n\nnode* getAt(int p){\n int cnt=0;\n node* temp=head;\n while(cnt < p){\n cnt++;\n temp=temp->next;\n }\n cout << temp->val << endl;\n return temp;\n}\n\nvoid addAt(int pos,int data){\n if(pos==0){\n insertion_at_head(data);\n return;\n }\n node* newnode=new node;\n newnode->val=data;\n int cnt=0;\n node* temp=head;\n while(cnt<pos-1){\n cnt++;\n temp=temp->next;\n }\n newnode->next=temp->next;\n temp->next=newnode;\n \n}\n\nvoid removeAt(int pos){ \n if(pos==0){\n deletion_at_head();\n return;\n }\n int cnt=0;\n node* temp=head;\n while(cnt<pos-1){\n cnt++;\n temp=temp->next;\n }\n temp->next = temp->next->next;\n}\n\nvoid reverse_di(){\n int left = 0;\n int right = size() - 1;\n while(left < right){\n node* templ = getAt( left);\n node* tempr = getAt( right);\n \n int temp = templ->val;\n templ->val = tempr->val;\n tempr->val = temp;\n left++;\n right--;\n }\n}\nvoid reverse_pi(){\n if(size()<=1){\n return;\n }\n \n node* t=head;\n while(t->next!=NULL){\n t=t->next;\n \n }\n tail=t;\n \n node* prev=nullptr;\n node* curr=head;\n while(curr !=nullptr){\n node* next= curr->next;\n curr->next=prev;\n prev=curr;\n curr=next;\n }\n node* temp=head;\n \n \n \n head =tail;\n tail=temp;\n }\n};\n\nclass LLToQueueAdapter{\npublic:\nLinkedList l1;\n\nint size1(){\n // write your code here\n}\nvoid add(int val){\n // write your code here\n \n}\nint Remove(){\n // write your code here\n}\nint peek(){\n // write your code here\n}\n};\nint main() {\n LLToQueueAdapter l1;\n string s;\n cin >> s;\n while(s!=\"quit\"){\n if(s == \"add\"){\n int val;\n cin >> val;\n l1.add(val);\n }\n else if(s == \"remove\"){\n int val= l1.Remove();\n if (val != -1){\n cout << val << endl;\n }\n }\n else if(s== \"peek\"){\n int val= l1.peek();\n if(val != -1){\n cout << val << endl;\n }\n }else if(s== \"size\"){\n cout << l1.size1() << endl;\n }\n cin >> s;\n }\n \n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static class LLToQueueAdapter {\r\n LinkedList<Integer> list;\r\n\r\n public LLToQueueAdapter() {\r\n list = new LinkedList<>();\r\n }\r\n\r\n int size() {\r\n // write your code here\r\n }\r\n\r\n void add(int val) {\r\n // write your code here\r\n }\r\n\r\n int remove() {\r\n // write your code here\r\n }\r\n\r\n int peek() {\r\n // write your code here\r\n }\r\n }\r\n\r\n public static void main(String[] args) throws Exception {\r\n BufferedReader br = new BufferedReader(new InputStreamReader(System.in));\r\n LLToQueueAdapter qu = new LLToQueueAdapter();\r\n\r\n String str = br.readLine();\r\n while (str.equals(\"quit\") == false) {\r\n if (str.startsWith(\"add\")) {\r\n int val = Integer.parseInt(str.split(\" \")[1]);\r\n qu.add(val);\r\n } else if (str.startsWith(\"remove\")) {\r\n int val = qu.remove();\r\n if (val != -1) {\r\n System.out.println(val);\r\n }\r\n } else if (str.startsWith(\"peek\")) {\r\n int val = qu.peek();\r\n if (val != -1) {\r\n System.out.println(val);\r\n }\r\n } else if (str.startsWith(\"size\")) {\r\n System.out.println(qu.size());\r\n }\r\n str = br.readLine();\r\n }\r\n }\r\n}"},"python":{"code":"class Node:\n\n def __init__(self):\n self.data = 0\n self.next = None\n\n\nclass LinkedList:\n\n def __init__(self):\n self.head = None\n self.tail = None\n self.size = 0\n\n def addLast(self, val):\n temp = Node()\n temp.data = val\n temp.next = None\n\n if self.size == 0:\n self.head = self.tail = temp\n else:\n self.tail.next = temp\n self.tail = temp\n self.size += 1\n \n def getNodeAt(self, idx):\n temp = self.head\n i = 0\n while i < idx:\n temp = temp.next\n i += 1\n return temp\n \n def display(self):\n temp = self.head\n while temp is not None:\n print(temp.data, end = \" \")\n temp = temp.next\n print()\n \n def removeFirst(self):\n if self.size == 0:\n print(\"List is empty\")\n elif self.size == 1:\n self.head = self.tail = None\n self.size = 0\n else:\n self.head = self.head.next\n self.size -= 1\n \n def getFirst(self):\n if self.size == 0:\n print(\"List is empty\")\n return -1\n else:\n return self.head.data\n\n def getLast(self):\n if self.size == 0:\n print(\"List is empty\")\n return -1\n else:\n return self.tail.data\n \n def getAt(self, idx):\n if self.size == 0:\n print(\"List is empty\")\n return -1\n elif idx < 0 or idx >= self.size:\n print(\"Invalid arguments\")\n return -1\n else:\n temp = self.head\n i = 0\n while i < idx:\n temp = temp.next\n i += 1\n return temp.data\n \n def addFirst(self, val):\n temp = Node()\n temp.data = val\n temp.next = self.head\n self.head = temp\n\n if self.size == 0:\n self.tail = temp\n\n self.size += 1\n \n def removeAt(self, idx):\n if idx < 0 or idx >= self.size:\n print(\"Invalid arguments\")\n elif idx == 0:\n self.removeFirst()\n elif idx == self.size - 1:\n self.removeLast()\n else:\n temp = self.head\n i = 0\n while i < idx - 1:\n temp = temp.next\n i += 1\n\n temp.next = temp.next.next\n self.size -= 1\n\n def addAt(self, idx, val):\n if idx < 0 or idx > self.size:\n print(\"Invalid arguments\")\n elif idx == 0:\n self.addFirst(val)\n elif idx == self.size:\n self.addLast(val)\n else:\n node = Node()\n node.data = val\n\n temp = self.head\n i = 0\n while i < idx - 1:\n temp = temp.next\n i += 1\n node.next = temp.next\n\n temp.next = node\n self.size += 1\n \n def removeLast(self):\n if self.size == 0:\n print(\"List is empty\")\n elif self.size == 1:\n self.head = self.tail = None\n self.size = 0\n else:\n temp = self.head\n i = 0\n while i < self.size - 2:\n temp = temp.next\n i += 1\n\n self.tail = temp\n self.tail.next = None\n self.size -= 1\n \n def reverseDI(self):\n left=0\n right=self.size-1\n while(left<right):\n temp1 = self.getNodeAt(left)\n temp2 = self.getNodeAt(right)\n temp = temp1.data\n temp1.data = temp2.data\n temp2.data = temp\n left+=1\n right-=1\n \n def reversePI(self):\n if self.size <= 1:\n return\n\n prev = None\n curr = self.head\n while curr is not None:\n next = curr.next\n\n curr.next = prev\n prev = curr\n curr = next\n\n temp = self.head\n self.head = self.tail\n self.tail = temp\n\n\nclass LLToStackAdapter:\n def __init__(self):\n self.lst = LinkedList()\n \n def size(self):\n #write your code here\n \n def push(self,val):\n #write your code here\n \n def pop(self):\n #write your code here\n \n def top(self):\n #write your code here\n \ns=LLToStackAdapter()\nwhile True:\n cmd=input().split(\" \")\n if \"push\" in cmd[0]:\n val=int(cmd[1])\n s.push(val)\n \n elif \"pop\" in cmd[0]:\n ans=s.pop()\n if(ans!=-1):\n print(ans)\n \n elif \"top\" in cmd[0]:\n ans=s.top()\n if(ans!=-1):\n print(ans)\n \n elif \"size\" in cmd[0]:\n print(s.size())\n \n elif \"quit\" in cmd[0]:\n break"}},"points":10,"difficulty":"easy","sampleInput":"add 10\r\nadd 20\r\nadd 30\r\nadd 40\r\nadd 50\r\nadd 60\r\npeek\r\nremove\r\npeek\r\nremove\r\npeek\r\nremove\r\npeek\r\nremove\r\npeek\r\nremove\r\npeek\r\nremove\r\nquit","sampleOutput":"10\r\n10\r\n20\r\n20\r\n30\r\n30\r\n40\r\n40\r\n50\r\n50\r\n60\r\n60","questionVideo":"https://www.youtube.com/embed/BzeEZZTsNKA","hints":[],"associated":[{"id":"196a12a7-449d-4185-b894-365b3e8b4534","name":"In the linked list implementation of a queue, front and rear pointers are tracked. Which of these pointers will change during an insertion into EMPTY queue?(Q- Linkedlist to Queue adapter)","slug":"in-the-linked-list-implementation-of-a-queue-front-and-rear-pointers-are-tracked-which-of-these-pointers-will-change-during-an-insertion-into-empty-queue-q-linkedlist-to-queue-adapter","type":4},{"id":"a40afcce-1e58-4c0f-bc01-5a4ed0207e36","name":"In the linked list implementation of a queue, where does a new element be inserted?(Q- Linkedlist to Queue adapter)","slug":"in-the-linked-list-implementation-of-a-queue-where-does-a-new-element-be-inserted-q-linkedlist-to-queue-adapter","type":4},{"id":"c185b361-1ad7-4fb2-97dc-cdda8fec5abe","name":"In linked list implementation of queue, if only front pointer is maintained, which of the following operation take worst case linear time?(Q- Linkedlist to Queue adapter)","slug":"in-linked-list-implementation-of-queue-if-only-front-pointer-is-maintained-which-of-the-following-operation-take-worst-case-linear-time-q-linkedlist-to-queue-adapter","type":4},{"id":"f3bc6500-e757-4565-9703-983a8ef53361","name":"The front and rear pointers of a queue are tracked in a linked list implementation. During an insertion into a NONEMPTY queue, which of these pointers will change?(Q- Linkedlist to Queue adapter)","slug":"the-front-and-rear-pointers-of-a-queue-are-tracked-in-a-linked-list-implementation-during-an-insertion-into-a-nonempty-queue-which-of-these-pointers-will-change-q-linkedlist-to-queue-adapter","type":4}],"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":"91027ef1-2784-45bf-8143-cc6af4560105","name":"Linked Lists For Beginners","slug":"linked-lists-for-beginners","type":0},{"id":"c9aaec9b-7705-45d9-9d6e-ca2c27f63c79","name":"Linked List To Queue Adapter","slug":"linked-list-to-queue-adapter","type":1}],"next":{"id":"b7a717aa-3c8e-45d6-bbe7-7e818fabcb54","name":"Linked List to Queue Adapter","type":3,"slug":"linked-list-to-queue-adapter"},"prev":{"id":"d3717add-f860-4be3-bbd6-9a116b51dc3f","name":"Linked List To Stack Adapter","type":3,"slug":"linked-list-to-stack-adapter"}}}

Linked List To Queue Adapter

1. You are required to complete the code of our LLToQueueAdapter class. 2. As data members, you've a linkedlist available in the class. 3. Here is the list of functions that you are supposed to complete 3.1. add -> Should accept new data in FIFO manner 3.2. remove -> Should remove and return data in FIFO manner. If not available, print "Queue underflow" and return -1. 3.3. peek -> Should return data in FIFO manner. If not available, print "Queue underflow" and return -1. 3.4. size -> Should return the number of elements available in the queue 4. Input and Output is managed for you. Note -> The intention is to use linked list functions to achieve the purpose of a queue. All the functions should work in constant time.

{"id":"49f044e4-5297-48f8-9e36-9c86036aa6ff","name":"Linked List To Queue Adapter","description":"1. You are required to complete the code of our LLToQueueAdapter class. \r\n2. As data members, you've a linkedlist available in the class.\r\n3. Here is the list of functions that you are supposed to complete\r\n 3.1. add -> Should accept new data in FIFO manner\r\n 3.2. remove -> Should remove and return data in FIFO manner. If not available, \r\n print \"Queue underflow\" and return -1.\r\n 3.3. peek -> Should return data in FIFO manner. If not available, print \"Queue \r\n underflow\" and return -1.\r\n 3.4. size -> Should return the number of elements available in the queue\r\n4. Input and Output is managed for you.\r\n\r\nNote -> The intention is to use linked list functions to achieve the purpose of a queue. All the functions should work in constant time.","inputFormat":"Input is managed for you","outputFormat":"Output is managed for you","constraints":"None","sampleCode":{"cpp":{"code":"#include <iostream>\nusing namespace std;\nclass node{\n public:\n int val;\n node* next;\n \n\n};\n\nclass LinkedList {\n public: \n node* head=nullptr;\n node* tail=nullptr;\n int size_=0;\n \n\nvoid insert_at_tail(int val){\n if(head==NULL){\n node *newnode = new node;\n newnode->val=val;\n newnode->next=NULL;\n head=newnode;\n\n }\n else{\n node *newnode = new node;\n newnode->val=val;\n newnode->next=NULL;\n node *temp = head;\n while(temp->next!=NULL){\n temp=temp->next;\n }\n temp->next = newnode;\n }\n\n}\nvoid insertion_at_head(int val){\n node *newnode = new node;\n newnode->val=val;\n newnode->next=NULL;\n if(head==NULL){\n head=newnode;\n }\n else{\n newnode ->next =head;\n head = newnode;\n }\n\n}\nvoid print (){\n node *temp =head;\n if(head==NULL){\n cout << \"0\" << endl;\n return;\n }\n while(temp!=NULL){\n cout<<temp->val<<\" \";\n temp=temp->next;\n }\n cout << endl;\n}\nint deletion_at_head(){\n if(head==NULL) return -1;\n int tr=head->val;\n node *temp=head;\n head=head->next;\n \n delete temp;\n return tr;\n\n}\nvoid deletion_at_tail(){\n if(head==NULL) return;\n node* previous=NULL;\n node* temp=head;\n while(temp->next!=NULL){\n previous=temp;\n temp=temp->next;\n }\n previous->next = NULL;\n delete temp; \n}\n\n\nvoid last(){\n node* temp=head;\n while(temp->next!=NULL){\n temp=temp->next;\n }\n cout << temp->val << endl;\n}\nint size(){\n int cnt=0;\n node* temp=head;\n while(temp!=NULL){\n temp=temp->next;\n cnt++;\n }\n return cnt;\n}\nint first(){\n return head->val ;\n}\n\nnode* getAt(int p){\n int cnt=0;\n node* temp=head;\n while(cnt < p){\n cnt++;\n temp=temp->next;\n }\n cout << temp->val << endl;\n return temp;\n}\n\nvoid addAt(int pos,int data){\n if(pos==0){\n insertion_at_head(data);\n return;\n }\n node* newnode=new node;\n newnode->val=data;\n int cnt=0;\n node* temp=head;\n while(cnt<pos-1){\n cnt++;\n temp=temp->next;\n }\n newnode->next=temp->next;\n temp->next=newnode;\n \n}\n\nvoid removeAt(int pos){ \n if(pos==0){\n deletion_at_head();\n return;\n }\n int cnt=0;\n node* temp=head;\n while(cnt<pos-1){\n cnt++;\n temp=temp->next;\n }\n temp->next = temp->next->next;\n}\n\nvoid reverse_di(){\n int left = 0;\n int right = size() - 1;\n while(left < right){\n node* templ = getAt( left);\n node* tempr = getAt( right);\n \n int temp = templ->val;\n templ->val = tempr->val;\n tempr->val = temp;\n left++;\n right--;\n }\n}\nvoid reverse_pi(){\n if(size()<=1){\n return;\n }\n \n node* t=head;\n while(t->next!=NULL){\n t=t->next;\n \n }\n tail=t;\n \n node* prev=nullptr;\n node* curr=head;\n while(curr !=nullptr){\n node* next= curr->next;\n curr->next=prev;\n prev=curr;\n curr=next;\n }\n node* temp=head;\n \n \n \n head =tail;\n tail=temp;\n }\n};\n\nclass LLToQueueAdapter{\npublic:\nLinkedList l1;\n\nint size1(){\n // write your code here\n}\nvoid add(int val){\n // write your code here\n \n}\nint Remove(){\n // write your code here\n}\nint peek(){\n // write your code here\n}\n};\nint main() {\n LLToQueueAdapter l1;\n string s;\n cin >> s;\n while(s!=\"quit\"){\n if(s == \"add\"){\n int val;\n cin >> val;\n l1.add(val);\n }\n else if(s == \"remove\"){\n int val= l1.Remove();\n if (val != -1){\n cout << val << endl;\n }\n }\n else if(s== \"peek\"){\n int val= l1.peek();\n if(val != -1){\n cout << val << endl;\n }\n }else if(s== \"size\"){\n cout << l1.size1() << endl;\n }\n cin >> s;\n }\n \n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static class LLToQueueAdapter {\r\n LinkedList<Integer> list;\r\n\r\n public LLToQueueAdapter() {\r\n list = new LinkedList<>();\r\n }\r\n\r\n int size() {\r\n // write your code here\r\n }\r\n\r\n void add(int val) {\r\n // write your code here\r\n }\r\n\r\n int remove() {\r\n // write your code here\r\n }\r\n\r\n int peek() {\r\n // write your code here\r\n }\r\n }\r\n\r\n public static void main(String[] args) throws Exception {\r\n BufferedReader br = new BufferedReader(new InputStreamReader(System.in));\r\n LLToQueueAdapter qu = new LLToQueueAdapter();\r\n\r\n String str = br.readLine();\r\n while (str.equals(\"quit\") == false) {\r\n if (str.startsWith(\"add\")) {\r\n int val = Integer.parseInt(str.split(\" \")[1]);\r\n qu.add(val);\r\n } else if (str.startsWith(\"remove\")) {\r\n int val = qu.remove();\r\n if (val != -1) {\r\n System.out.println(val);\r\n }\r\n } else if (str.startsWith(\"peek\")) {\r\n int val = qu.peek();\r\n if (val != -1) {\r\n System.out.println(val);\r\n }\r\n } else if (str.startsWith(\"size\")) {\r\n System.out.println(qu.size());\r\n }\r\n str = br.readLine();\r\n }\r\n }\r\n}"},"python":{"code":"class Node:\n\n def __init__(self):\n self.data = 0\n self.next = None\n\n\nclass LinkedList:\n\n def __init__(self):\n self.head = None\n self.tail = None\n self.size = 0\n\n def addLast(self, val):\n temp = Node()\n temp.data = val\n temp.next = None\n\n if self.size == 0:\n self.head = self.tail = temp\n else:\n self.tail.next = temp\n self.tail = temp\n self.size += 1\n \n def getNodeAt(self, idx):\n temp = self.head\n i = 0\n while i < idx:\n temp = temp.next\n i += 1\n return temp\n \n def display(self):\n temp = self.head\n while temp is not None:\n print(temp.data, end = \" \")\n temp = temp.next\n print()\n \n def removeFirst(self):\n if self.size == 0:\n print(\"List is empty\")\n elif self.size == 1:\n self.head = self.tail = None\n self.size = 0\n else:\n self.head = self.head.next\n self.size -= 1\n \n def getFirst(self):\n if self.size == 0:\n print(\"List is empty\")\n return -1\n else:\n return self.head.data\n\n def getLast(self):\n if self.size == 0:\n print(\"List is empty\")\n return -1\n else:\n return self.tail.data\n \n def getAt(self, idx):\n if self.size == 0:\n print(\"List is empty\")\n return -1\n elif idx < 0 or idx >= self.size:\n print(\"Invalid arguments\")\n return -1\n else:\n temp = self.head\n i = 0\n while i < idx:\n temp = temp.next\n i += 1\n return temp.data\n \n def addFirst(self, val):\n temp = Node()\n temp.data = val\n temp.next = self.head\n self.head = temp\n\n if self.size == 0:\n self.tail = temp\n\n self.size += 1\n \n def removeAt(self, idx):\n if idx < 0 or idx >= self.size:\n print(\"Invalid arguments\")\n elif idx == 0:\n self.removeFirst()\n elif idx == self.size - 1:\n self.removeLast()\n else:\n temp = self.head\n i = 0\n while i < idx - 1:\n temp = temp.next\n i += 1\n\n temp.next = temp.next.next\n self.size -= 1\n\n def addAt(self, idx, val):\n if idx < 0 or idx > self.size:\n print(\"Invalid arguments\")\n elif idx == 0:\n self.addFirst(val)\n elif idx == self.size:\n self.addLast(val)\n else:\n node = Node()\n node.data = val\n\n temp = self.head\n i = 0\n while i < idx - 1:\n temp = temp.next\n i += 1\n node.next = temp.next\n\n temp.next = node\n self.size += 1\n \n def removeLast(self):\n if self.size == 0:\n print(\"List is empty\")\n elif self.size == 1:\n self.head = self.tail = None\n self.size = 0\n else:\n temp = self.head\n i = 0\n while i < self.size - 2:\n temp = temp.next\n i += 1\n\n self.tail = temp\n self.tail.next = None\n self.size -= 1\n \n def reverseDI(self):\n left=0\n right=self.size-1\n while(left<right):\n temp1 = self.getNodeAt(left)\n temp2 = self.getNodeAt(right)\n temp = temp1.data\n temp1.data = temp2.data\n temp2.data = temp\n left+=1\n right-=1\n \n def reversePI(self):\n if self.size <= 1:\n return\n\n prev = None\n curr = self.head\n while curr is not None:\n next = curr.next\n\n curr.next = prev\n prev = curr\n curr = next\n\n temp = self.head\n self.head = self.tail\n self.tail = temp\n\n\nclass LLToStackAdapter:\n def __init__(self):\n self.lst = LinkedList()\n \n def size(self):\n #write your code here\n \n def push(self,val):\n #write your code here\n \n def pop(self):\n #write your code here\n \n def top(self):\n #write your code here\n \ns=LLToStackAdapter()\nwhile True:\n cmd=input().split(\" \")\n if \"push\" in cmd[0]:\n val=int(cmd[1])\n s.push(val)\n \n elif \"pop\" in cmd[0]:\n ans=s.pop()\n if(ans!=-1):\n print(ans)\n \n elif \"top\" in cmd[0]:\n ans=s.top()\n if(ans!=-1):\n print(ans)\n \n elif \"size\" in cmd[0]:\n print(s.size())\n \n elif \"quit\" in cmd[0]:\n break"}},"points":10,"difficulty":"easy","sampleInput":"add 10\r\nadd 20\r\nadd 30\r\nadd 40\r\nadd 50\r\nadd 60\r\npeek\r\nremove\r\npeek\r\nremove\r\npeek\r\nremove\r\npeek\r\nremove\r\npeek\r\nremove\r\npeek\r\nremove\r\nquit","sampleOutput":"10\r\n10\r\n20\r\n20\r\n30\r\n30\r\n40\r\n40\r\n50\r\n50\r\n60\r\n60","questionVideo":"https://www.youtube.com/embed/BzeEZZTsNKA","hints":[],"associated":[{"id":"196a12a7-449d-4185-b894-365b3e8b4534","name":"In the linked list implementation of a queue, front and rear pointers are tracked. Which of these pointers will change during an insertion into EMPTY queue?(Q- Linkedlist to Queue adapter)","slug":"in-the-linked-list-implementation-of-a-queue-front-and-rear-pointers-are-tracked-which-of-these-pointers-will-change-during-an-insertion-into-empty-queue-q-linkedlist-to-queue-adapter","type":4},{"id":"a40afcce-1e58-4c0f-bc01-5a4ed0207e36","name":"In the linked list implementation of a queue, where does a new element be inserted?(Q- Linkedlist to Queue adapter)","slug":"in-the-linked-list-implementation-of-a-queue-where-does-a-new-element-be-inserted-q-linkedlist-to-queue-adapter","type":4},{"id":"c185b361-1ad7-4fb2-97dc-cdda8fec5abe","name":"In linked list implementation of queue, if only front pointer is maintained, which of the following operation take worst case linear time?(Q- Linkedlist to Queue adapter)","slug":"in-linked-list-implementation-of-queue-if-only-front-pointer-is-maintained-which-of-the-following-operation-take-worst-case-linear-time-q-linkedlist-to-queue-adapter","type":4},{"id":"f3bc6500-e757-4565-9703-983a8ef53361","name":"The front and rear pointers of a queue are tracked in a linked list implementation. During an insertion into a NONEMPTY queue, which of these pointers will change?(Q- Linkedlist to Queue adapter)","slug":"the-front-and-rear-pointers-of-a-queue-are-tracked-in-a-linked-list-implementation-during-an-insertion-into-a-nonempty-queue-which-of-these-pointers-will-change-q-linkedlist-to-queue-adapter","type":4}],"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":"91027ef1-2784-45bf-8143-cc6af4560105","name":"Linked Lists For Beginners","slug":"linked-lists-for-beginners","type":0},{"id":"c9aaec9b-7705-45d9-9d6e-ca2c27f63c79","name":"Linked List To Queue Adapter","slug":"linked-list-to-queue-adapter","type":1}],"next":{"id":"b7a717aa-3c8e-45d6-bbe7-7e818fabcb54","name":"Linked List to Queue Adapter","type":3,"slug":"linked-list-to-queue-adapter"},"prev":{"id":"d3717add-f860-4be3-bbd6-9a116b51dc3f","name":"Linked List To Stack Adapter","type":3,"slug":"linked-list-to-stack-adapter"}}}
plane

Editor


Loading...

Linked List To Queue Adapter

easy

1. You are required to complete the code of our LLToQueueAdapter class. 2. As data members, you've a linkedlist available in the class. 3. Here is the list of functions that you are supposed to complete 3.1. add -> Should accept new data in FIFO manner 3.2. remove -> Should remove and return data in FIFO manner. If not available, print "Queue underflow" and return -1. 3.3. peek -> Should return data in FIFO manner. If not available, print "Queue underflow" and return -1. 3.4. size -> Should return the number of elements available in the queue 4. Input and Output is managed for you. Note -> The intention is to use linked list functions to achieve the purpose of a queue. All the functions should work in constant time.

Constraints

None

Format

Input

Input is managed for you

Output

Output is managed for you

Example

Sample Input

add 10 add 20 add 30 add 40 add 50 add 60 peek remove peek remove peek remove peek remove peek remove peek remove quit

Sample Output

10 10 20 20 30 30 40 40 50 50 60 60

Question Video

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode