{"id":"3e9d7391-8deb-4c14-86c7-0670de58bb6f","name":"Display A Linkedlist","description":"1. You are given a partially written LinkedList class.\r\n2. Here is a list of existing functions:\r\n 2.1 addLast - adds a new element with given value to the end of Linked List\r\n3. You are required to complete the body of display function and size function\r\n 3.1. display - Should print the elements of linked list from front to end in a single line. Elements should be separated by space.\r\n 3.2. size - Should return the number of elements in the linked list.\r\n4. Input and Output is managed for you.","inputFormat":"Input is managed for you","outputFormat":"Output is managed for you","constraints":"None","sampleCode":{"cpp":{"code":"#include <iostream>\n#include<string>\nusing namespace std;\n\nclass node\n{\npublic :\n int data;\n node* next;\n};\n\nclass linked_list\n{\npublic:\n node* head, *tail;\n int size = 0;\n\npublic:\n linked_list()\n {\n head = NULL;\n tail = NULL;\n }\n\n void addLast(int n)\n {\n node* tmp = new node;\n tmp->data = n;\n tmp->next = NULL;\n\n if (head == NULL)\n {\n head = tmp;\n tail = tmp;\n }\n else\n {\n tail->next = tmp;\n tail = tail->next;\n }\n size++;\n\n\n }\n void display() {\n // write your code here\n\n }\n\n\n void testList() {\n for (node* temp = head; temp != NULL; temp = temp->next) {\n cout << temp->data << endl;\n }\n cout << size << endl;\n\n if (size > 0) {\n cout << tail->data << endl;\n }\n }\n};\n\n\nint main() {\n\n\n string str;\n linked_list l;\n while (true) {\n getline(cin, str);\n if (str[0] == 'q') {\n break;\n }\n else if (str[0] == 'a') {\n string ss = str.substr(8, 2);\n int n = stoi(ss);\n l.addLast( n);\n\n }\n else if (str[0] == 's') {\n cout << l.size << endl;\n }\n else if (str[0] == 'd') {\n l.display();\n }\n\n\n }\n\n\n return 0;\n\n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n public static class Node {\r\n int data;\r\n Node next;\r\n }\r\n\r\n public static class LinkedList {\r\n Node head;\r\n Node tail;\r\n int size;\r\n\r\n void addLast(int val) {\r\n Node temp = new Node();\r\n temp.data = val;\r\n temp.next = null;\r\n\r\n if (size == 0) {\r\n head = tail = temp;\r\n } else {\r\n tail.next = temp;\r\n tail = temp;\r\n }\r\n\r\n size++;\r\n }\r\n\r\n public int size(){\r\n // write code here\r\n }\r\n\r\n public void display(){\r\n // write 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 LinkedList list = new LinkedList();\r\n\r\n String str = br.readLine();\r\n while(str.equals(\"quit\") == false){\r\n if(str.startsWith(\"addLast\")){\r\n int val = Integer.parseInt(str.split(\" \")[1]);\r\n list.addLast(val);\r\n } else if(str.startsWith(\"size\")){\r\n System.out.println(list.size());\r\n } else if(str.startsWith(\"display\")){\r\n list.display();\r\n }\r\n str = br.readLine();\r\n }\r\n }\r\n}"},"python":{"code":"class Node:\n def __init__(self, data=None):\n self.data = data\n self.next = None\nclass SLinkedList:\n def __init__(self):\n self.head = None\n self.size=0\n self.tail=None\n\n def AddLast(self, newdata):\n NewNode = Node(newdata)\n if self.head is None:\n self.head = NewNode\n self.tail = NewNode\n self.size+=1\n return\n laste = self.head\n while(laste.next):\n laste = laste.next\n laste.next=NewNode\n self.tail=laste.next \n self.size+=1\n\n def display(self):\n #write your code here\n def testList(self) :\n temp = self.head\n while temp is not None: \n print(temp.data)\n temp = temp.next\n \n print(self.size)\n\n if self.size > 0 :\n \n print(self.tail.data)\n \n \n\n\n\n\nl1 = SLinkedList()\nwhile 1>0 :\n str=input()\n\n if str[0]=='q':\n \n break;\n\n elif str[0]=='a':\n \n val=int(str[-3]+str[-2])\n \n l1.AddLast(val)\n \n elif str[0]=='d':\n \n\n l1.display() \n \n elif str[0]=='s':\n \n\n print(l1.size)"}},"points":10,"difficulty":"easy","sampleInput":"addLast 10\r\naddLast 20\r\naddLast 30\r\ndisplay\r\nsize\r\naddLast 40\r\naddLast 50\r\ndisplay\r\nsize\r\nquit","sampleOutput":"10 20 30 \r\n3\r\n10 20 30 40 50 \r\n5","questionVideo":"https://www.youtube.com/embed/6gR9_lA8uW8","hints":[],"associated":[{"id":"0dd3a513-932f-4999-908d-9f0e9e22adb0","name":"What will be the output for the same problem if \"head\" is null ?(Q- Display a linked list)","slug":"what-will-be-the-output-for-the-same-problem-if-head-is-null-q-display-a-linked-list","type":4},{"id":"1787c0b2-7031-4a43-b147-35c82b695ceb","name":"What will be the time complexity ?(Q- Display a linked list)","slug":"what-will-be-the-time-complexity-q-display-a-linked-list","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":"5d30fa24-f90b-4ff1-9b20-65343078071b","name":"Display A Linkedlist","slug":"display-a-linkedlist","type":1}],"next":{"id":"8a25a8e6-6fb7-42f1-9217-7d0f578dee2b","name":"Display a Linked List","type":3,"slug":"display-a-linked-list"},"prev":{"id":"8230d09f-bbaf-46d6-af08-9c6be2c87510","name":"Add Last in Linked List","type":3,"slug":"add-last-in-linked-list"}}}

Display A Linkedlist

1. You are given a partially written LinkedList class. 2. Here is a list of existing functions: 2.1 addLast - adds a new element with given value to the end of Linked List 3. You are required to complete the body of display function and size function 3.1. display - Should print the elements of linked list from front to end in a single line. Elements should be separated by space. 3.2. size - Should return the number of elements in the linked list. 4. Input and Output is managed for you.

{"id":"3e9d7391-8deb-4c14-86c7-0670de58bb6f","name":"Display A Linkedlist","description":"1. You are given a partially written LinkedList class.\r\n2. Here is a list of existing functions:\r\n 2.1 addLast - adds a new element with given value to the end of Linked List\r\n3. You are required to complete the body of display function and size function\r\n 3.1. display - Should print the elements of linked list from front to end in a single line. Elements should be separated by space.\r\n 3.2. size - Should return the number of elements in the linked list.\r\n4. Input and Output is managed for you.","inputFormat":"Input is managed for you","outputFormat":"Output is managed for you","constraints":"None","sampleCode":{"cpp":{"code":"#include <iostream>\n#include<string>\nusing namespace std;\n\nclass node\n{\npublic :\n int data;\n node* next;\n};\n\nclass linked_list\n{\npublic:\n node* head, *tail;\n int size = 0;\n\npublic:\n linked_list()\n {\n head = NULL;\n tail = NULL;\n }\n\n void addLast(int n)\n {\n node* tmp = new node;\n tmp->data = n;\n tmp->next = NULL;\n\n if (head == NULL)\n {\n head = tmp;\n tail = tmp;\n }\n else\n {\n tail->next = tmp;\n tail = tail->next;\n }\n size++;\n\n\n }\n void display() {\n // write your code here\n\n }\n\n\n void testList() {\n for (node* temp = head; temp != NULL; temp = temp->next) {\n cout << temp->data << endl;\n }\n cout << size << endl;\n\n if (size > 0) {\n cout << tail->data << endl;\n }\n }\n};\n\n\nint main() {\n\n\n string str;\n linked_list l;\n while (true) {\n getline(cin, str);\n if (str[0] == 'q') {\n break;\n }\n else if (str[0] == 'a') {\n string ss = str.substr(8, 2);\n int n = stoi(ss);\n l.addLast( n);\n\n }\n else if (str[0] == 's') {\n cout << l.size << endl;\n }\n else if (str[0] == 'd') {\n l.display();\n }\n\n\n }\n\n\n return 0;\n\n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n public static class Node {\r\n int data;\r\n Node next;\r\n }\r\n\r\n public static class LinkedList {\r\n Node head;\r\n Node tail;\r\n int size;\r\n\r\n void addLast(int val) {\r\n Node temp = new Node();\r\n temp.data = val;\r\n temp.next = null;\r\n\r\n if (size == 0) {\r\n head = tail = temp;\r\n } else {\r\n tail.next = temp;\r\n tail = temp;\r\n }\r\n\r\n size++;\r\n }\r\n\r\n public int size(){\r\n // write code here\r\n }\r\n\r\n public void display(){\r\n // write 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 LinkedList list = new LinkedList();\r\n\r\n String str = br.readLine();\r\n while(str.equals(\"quit\") == false){\r\n if(str.startsWith(\"addLast\")){\r\n int val = Integer.parseInt(str.split(\" \")[1]);\r\n list.addLast(val);\r\n } else if(str.startsWith(\"size\")){\r\n System.out.println(list.size());\r\n } else if(str.startsWith(\"display\")){\r\n list.display();\r\n }\r\n str = br.readLine();\r\n }\r\n }\r\n}"},"python":{"code":"class Node:\n def __init__(self, data=None):\n self.data = data\n self.next = None\nclass SLinkedList:\n def __init__(self):\n self.head = None\n self.size=0\n self.tail=None\n\n def AddLast(self, newdata):\n NewNode = Node(newdata)\n if self.head is None:\n self.head = NewNode\n self.tail = NewNode\n self.size+=1\n return\n laste = self.head\n while(laste.next):\n laste = laste.next\n laste.next=NewNode\n self.tail=laste.next \n self.size+=1\n\n def display(self):\n #write your code here\n def testList(self) :\n temp = self.head\n while temp is not None: \n print(temp.data)\n temp = temp.next\n \n print(self.size)\n\n if self.size > 0 :\n \n print(self.tail.data)\n \n \n\n\n\n\nl1 = SLinkedList()\nwhile 1>0 :\n str=input()\n\n if str[0]=='q':\n \n break;\n\n elif str[0]=='a':\n \n val=int(str[-3]+str[-2])\n \n l1.AddLast(val)\n \n elif str[0]=='d':\n \n\n l1.display() \n \n elif str[0]=='s':\n \n\n print(l1.size)"}},"points":10,"difficulty":"easy","sampleInput":"addLast 10\r\naddLast 20\r\naddLast 30\r\ndisplay\r\nsize\r\naddLast 40\r\naddLast 50\r\ndisplay\r\nsize\r\nquit","sampleOutput":"10 20 30 \r\n3\r\n10 20 30 40 50 \r\n5","questionVideo":"https://www.youtube.com/embed/6gR9_lA8uW8","hints":[],"associated":[{"id":"0dd3a513-932f-4999-908d-9f0e9e22adb0","name":"What will be the output for the same problem if \"head\" is null ?(Q- Display a linked list)","slug":"what-will-be-the-output-for-the-same-problem-if-head-is-null-q-display-a-linked-list","type":4},{"id":"1787c0b2-7031-4a43-b147-35c82b695ceb","name":"What will be the time complexity ?(Q- Display a linked list)","slug":"what-will-be-the-time-complexity-q-display-a-linked-list","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":"5d30fa24-f90b-4ff1-9b20-65343078071b","name":"Display A Linkedlist","slug":"display-a-linkedlist","type":1}],"next":{"id":"8a25a8e6-6fb7-42f1-9217-7d0f578dee2b","name":"Display a Linked List","type":3,"slug":"display-a-linked-list"},"prev":{"id":"8230d09f-bbaf-46d6-af08-9c6be2c87510","name":"Add Last in Linked List","type":3,"slug":"add-last-in-linked-list"}}}
plane

Editor


Loading...

Display A Linkedlist

easy

1. You are given a partially written LinkedList class. 2. Here is a list of existing functions: 2.1 addLast - adds a new element with given value to the end of Linked List 3. You are required to complete the body of display function and size function 3.1. display - Should print the elements of linked list from front to end in a single line. Elements should be separated by space. 3.2. size - Should return the number of elements in the linked list. 4. Input and Output is managed for you.

Constraints

None

Format

Input

Input is managed for you

Output

Output is managed for you

Example

Sample Input

addLast 10 addLast 20 addLast 30 display size addLast 40 addLast 50 display size quit

Sample Output

10 20 30 3 10 20 30 40 50 5

Question Video

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode