{"id":"8a163ab7-13e3-4535-95fb-4cafba149a29","name":"Linked List To Stack Adapter","description":"1. You are required to complete the code of our LLToStackAdapter 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. push -> Should accept new data in LIFO manner\r\n 3.2. pop -> Should remove and return data in LIFO manner. If not \r\n available, print \"Stack underflow\" and return -1.\r\n 3.3. top -> Should return data in LIFO manner. If not available, print \r\n \"Stack underflow\" and return -1.\r\n 3.4. size -> Should return the number of elements available in the \r\n stack\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 stack. 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}\nvoid deletion_at_head(){\n if(head==NULL) return;\n node *temp=head;\n head=head->next;\n delete temp;\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}\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 LLToStackAdapter{\npublic:\nLinkedList l1;\n\nint size1(){\n // write your code here\n}\nvoid push(int val){\n // write your code here\n \n}\nint pop(){\n // write your code here\n}\nint top(){\n // write your code here\n}\n};\nint main() {\n LLToStackAdapter l1;\n string s;\n cin >> s;\n while(s!=\"quit\"){\n if(s == \"push\"){\n int val;\n cin >> val;\n l1.push(val);\n }\n else if(s == \"pop\"){\n int val= l1.pop();\n if (val != -1){\n cout << val << endl;\n }\n }\n else if(s== \"top\"){\n int val= l1.top();\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 LLToStackAdapter {\r\n LinkedList<Integer> list;\r\n\r\n public LLToStackAdapter() {\r\n list = new LinkedList<>();\r\n }\r\n\r\n\r\n int size() {\r\n // write your code here\r\n }\r\n\r\n void push(int val) {\r\n // write your code here\r\n }\r\n\r\n int pop() {\r\n // write your code here\r\n }\r\n\r\n int top() {\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 LLToStackAdapter st = new LLToStackAdapter();\r\n\r\n String str = br.readLine();\r\n while(str.equals(\"quit\") == false){\r\n if(str.startsWith(\"push\")){\r\n int val = Integer.parseInt(str.split(\" \")[1]);\r\n st.push(val);\r\n } else if(str.startsWith(\"pop\")){\r\n int val = st.pop();\r\n if(val != -1){\r\n System.out.println(val);\r\n }\r\n } else if(str.startsWith(\"top\")){\r\n int val = st.top();\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(st.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":"push 10\r\npush 20\r\npush 5\r\npush 8\r\npush 2\r\npush 4\r\npush 11\r\ntop\r\nsize\r\npop\r\ntop\r\nsize\r\npop\r\ntop\r\nsize\r\npop\r\ntop\r\nsize\r\npop\r\ntop\r\nsize\r\npop\r\ntop\r\nsize\r\npop\r\ntop\r\nsize\r\npop\r\nquit","sampleOutput":"11\r\n7\r\n11\r\n4\r\n6\r\n4\r\n2\r\n5\r\n2\r\n8\r\n4\r\n8\r\n5\r\n3\r\n5\r\n20\r\n2\r\n20\r\n10\r\n1\r\n10","questionVideo":"https://www.youtube.com/embed/cOspdnSyOms","hints":[],"associated":[{"id":"1773106d-a232-466b-888f-7e96e1c1a7e3","name":"Which function should be used by linked list to remove the top of stack?(Q- Linked List to Stack Adapter)","slug":"which-function-should-be-used-by-linked-list-to-remove-the-top-of-stack-q-linked-list-to-stack-adapter","type":4},{"id":"98a315dc-655c-4a43-881f-f297a16ba135","name":"Which function should be used by linked list inside the push function of stack?(Q- Linked List to Stack Adapter)","slug":"which-function-should-be-used-by-linked-list-inside-the-push-function-of-stack-q-linked-list-to-stack-adapter","type":4},{"id":"a5434147-6a73-4505-98a9-56e60a0fe8d7","name":"What type of linked list is used in this question?(Q- Linked List to Stack Adapter)","slug":"what-type-of-linked-list-is-used-in-this-question-q-linked-list-to-stack-adapter","type":4},{"id":"d485fcf9-69ed-4dfb-b327-c5dddabccd3b","name":"What should be the time complixity of push, pop and size function of the stack made in this question?(Q- LinkedList to Stack adapter)","slug":"what-should-be-the-time-complixity-of-push-pop-and-size-function-of-the-stack-made-in-this-question-q-linkedlist-to-stack-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":"0a01dae2-7d35-4883-a947-d36db386a292","name":"Linked List To Stack Adapter","slug":"linked-list-to-stack-adapter","type":1}],"next":{"id":"d3717add-f860-4be3-bbd6-9a116b51dc3f","name":"Linked List To Stack Adapter","type":3,"slug":"linked-list-to-stack-adapter"},"prev":{"id":"eaf6c6b6-5167-4d95-b183-08c2c5deb5bd","name":"Reverse Linked List (pointer Iterative)","type":3,"slug":"reverse-linked-list-pointer-iterative"}}}

Linked List To Stack Adapter

1. You are required to complete the code of our LLToStackAdapter 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. push -> Should accept new data in LIFO manner 3.2. pop -> Should remove and return data in LIFO manner. If not available, print "Stack underflow" and return -1. 3.3. top -> Should return data in LIFO manner. If not available, print "Stack underflow" and return -1. 3.4. size -> Should return the number of elements available in the stack 4. Input and Output is managed for you. Note -> The intention is to use linked list functions to achieve the purpose of a stack. All the functions should work in constant time.

{"id":"8a163ab7-13e3-4535-95fb-4cafba149a29","name":"Linked List To Stack Adapter","description":"1. You are required to complete the code of our LLToStackAdapter 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. push -> Should accept new data in LIFO manner\r\n 3.2. pop -> Should remove and return data in LIFO manner. If not \r\n available, print \"Stack underflow\" and return -1.\r\n 3.3. top -> Should return data in LIFO manner. If not available, print \r\n \"Stack underflow\" and return -1.\r\n 3.4. size -> Should return the number of elements available in the \r\n stack\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 stack. 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}\nvoid deletion_at_head(){\n if(head==NULL) return;\n node *temp=head;\n head=head->next;\n delete temp;\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}\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 LLToStackAdapter{\npublic:\nLinkedList l1;\n\nint size1(){\n // write your code here\n}\nvoid push(int val){\n // write your code here\n \n}\nint pop(){\n // write your code here\n}\nint top(){\n // write your code here\n}\n};\nint main() {\n LLToStackAdapter l1;\n string s;\n cin >> s;\n while(s!=\"quit\"){\n if(s == \"push\"){\n int val;\n cin >> val;\n l1.push(val);\n }\n else if(s == \"pop\"){\n int val= l1.pop();\n if (val != -1){\n cout << val << endl;\n }\n }\n else if(s== \"top\"){\n int val= l1.top();\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 LLToStackAdapter {\r\n LinkedList<Integer> list;\r\n\r\n public LLToStackAdapter() {\r\n list = new LinkedList<>();\r\n }\r\n\r\n\r\n int size() {\r\n // write your code here\r\n }\r\n\r\n void push(int val) {\r\n // write your code here\r\n }\r\n\r\n int pop() {\r\n // write your code here\r\n }\r\n\r\n int top() {\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 LLToStackAdapter st = new LLToStackAdapter();\r\n\r\n String str = br.readLine();\r\n while(str.equals(\"quit\") == false){\r\n if(str.startsWith(\"push\")){\r\n int val = Integer.parseInt(str.split(\" \")[1]);\r\n st.push(val);\r\n } else if(str.startsWith(\"pop\")){\r\n int val = st.pop();\r\n if(val != -1){\r\n System.out.println(val);\r\n }\r\n } else if(str.startsWith(\"top\")){\r\n int val = st.top();\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(st.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":"push 10\r\npush 20\r\npush 5\r\npush 8\r\npush 2\r\npush 4\r\npush 11\r\ntop\r\nsize\r\npop\r\ntop\r\nsize\r\npop\r\ntop\r\nsize\r\npop\r\ntop\r\nsize\r\npop\r\ntop\r\nsize\r\npop\r\ntop\r\nsize\r\npop\r\ntop\r\nsize\r\npop\r\nquit","sampleOutput":"11\r\n7\r\n11\r\n4\r\n6\r\n4\r\n2\r\n5\r\n2\r\n8\r\n4\r\n8\r\n5\r\n3\r\n5\r\n20\r\n2\r\n20\r\n10\r\n1\r\n10","questionVideo":"https://www.youtube.com/embed/cOspdnSyOms","hints":[],"associated":[{"id":"1773106d-a232-466b-888f-7e96e1c1a7e3","name":"Which function should be used by linked list to remove the top of stack?(Q- Linked List to Stack Adapter)","slug":"which-function-should-be-used-by-linked-list-to-remove-the-top-of-stack-q-linked-list-to-stack-adapter","type":4},{"id":"98a315dc-655c-4a43-881f-f297a16ba135","name":"Which function should be used by linked list inside the push function of stack?(Q- Linked List to Stack Adapter)","slug":"which-function-should-be-used-by-linked-list-inside-the-push-function-of-stack-q-linked-list-to-stack-adapter","type":4},{"id":"a5434147-6a73-4505-98a9-56e60a0fe8d7","name":"What type of linked list is used in this question?(Q- Linked List to Stack Adapter)","slug":"what-type-of-linked-list-is-used-in-this-question-q-linked-list-to-stack-adapter","type":4},{"id":"d485fcf9-69ed-4dfb-b327-c5dddabccd3b","name":"What should be the time complixity of push, pop and size function of the stack made in this question?(Q- LinkedList to Stack adapter)","slug":"what-should-be-the-time-complixity-of-push-pop-and-size-function-of-the-stack-made-in-this-question-q-linkedlist-to-stack-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":"0a01dae2-7d35-4883-a947-d36db386a292","name":"Linked List To Stack Adapter","slug":"linked-list-to-stack-adapter","type":1}],"next":{"id":"d3717add-f860-4be3-bbd6-9a116b51dc3f","name":"Linked List To Stack Adapter","type":3,"slug":"linked-list-to-stack-adapter"},"prev":{"id":"eaf6c6b6-5167-4d95-b183-08c2c5deb5bd","name":"Reverse Linked List (pointer Iterative)","type":3,"slug":"reverse-linked-list-pointer-iterative"}}}
plane

Editor


Loading...

Linked List To Stack Adapter

easy

1. You are required to complete the code of our LLToStackAdapter 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. push -> Should accept new data in LIFO manner 3.2. pop -> Should remove and return data in LIFO manner. If not available, print "Stack underflow" and return -1. 3.3. top -> Should return data in LIFO manner. If not available, print "Stack underflow" and return -1. 3.4. size -> Should return the number of elements available in the stack 4. Input and Output is managed for you. Note -> The intention is to use linked list functions to achieve the purpose of a stack. 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

push 10 push 20 push 5 push 8 push 2 push 4 push 11 top size pop top size pop top size pop top size pop top size pop top size pop top size pop quit

Sample Output

11 7 11 4 6 4 2 5 2 8 4 8 5 3 5 20 2 20 10 1 10

Question Video

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode