0

What will be the output generated by the following program?

class LinkedListAddDelp { Node head; // Create a node class Node { int data; Node next; Node(int d) { data = d; next = null; } } // Insert at the beginning public void insertAtBeginning(int new_data) { // insert the data Node new_node = new Node(new_data); new_node.next = head; head = new_node; } // Delete a node void deleteNode() { if (head == null) return; Node temp = head; head = head.next; } // Print the linked list public void printList() { System.out.println(); Node tnode = head; while (tnode != null) { System.out.print(tnode.data + " "); tnode = tnode.next; } } public static void main(String[] args) { LinkedListAddDelp llist = new LinkedListAddDelp(); // llist.insertAtEnd(1); llist.insertAtBeginning(2); llist.insertAtBeginning(3); llist.insertAtBeginning(5); // System.out.println("Linked list: "); // llist.printList(); llist.deleteNode(); // llist.printList(); llist.insertAtBeginning(8); llist.printList(); } }

5th May 2023, 1:31 PM
sara
2 ответов
+ 8
sara You can run this code in code playground to get the output. If you are facing problem in code write clearly so that will help the community to guide you.
5th May 2023, 1:36 PM
R🍁🇮🇳
R🍁🇮🇳 - avatar
0
walking down main: * new empty list * commented out * push 2 at the head (list is 2) * push 3 at the head (list is 3, 2) * push 5 at the head (list is 5, 3, 2) * commented out * commented out * deleteNode cuts off the head (list is 3, 2) * commented out * push 8 at the head (list is 8, 3, 2) * printList prints a line break, then "8 3 2 " (space at the end)
5th May 2023, 6:48 PM
Orin Cook
Orin Cook - avatar