Computer Networking: A Top-Down Approach (7th Edition)
Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN: 9780133594140
Author: James Kurose, Keith Ross
Publisher: PEARSON
Bartleby Related Questions Icon

Related questions

Question
100%

Code with comments and output screenshot is must. Thank you!

### Printing a Linked List in Java

Given a standard linked list of integers (`int`), `L`, we want to write two methods to print the elements of `L` in order:
1. Using a loop.
2. Using recursion.

We will start printing from `head.next`.

#### Using a Loop
Below is the Java code to print the elements of a linked list using a loop:

```java
class Node {
    int data;
    Node next;
    
    Node(int d) {
        data = d;
        next = null;
    }
}

public class LinkedList {
    Node head;  // head of the list

    /* Function to print linked list using loop starting from head.next */
    void printListUsingLoop() {
        Node temp = head.next; // Start from the next node
        while (temp != null) {
            System.out.print(temp.data + " "); 
            temp = temp.next;
        }
    }
    
    public static void main(String[] args) {
        LinkedList list = new LinkedList();
        
        // Creating a simple linked list
        list.head = new Node(1);
        Node second = new Node(2);
        Node third = new Node(3);
        
        list.head.next = second; // Link first node with the second node
        second.next = third;     // Link second node with the third node
        
        list.printListUsingLoop();
    }
}
```

#### Using Recursion
Below is the Java code to print the elements of a linked list using recursion:

```java
class Node {
    int data;
    Node next;
    
    Node(int d) {
        data = d;
        next = null;
    }
}

public class LinkedList {
    Node head;  // head of the list

    /* Function to print linked list using recursion starting from head.next */
    void printListUsingRecursion(Node node) {
        if (node == null) {
            return;  // Base case: end of the list
        }
        System.out.print(node.data + " ");
        printListUsingRecursion(node.next);
    }
    
    public static void main(String[] args) {
        LinkedList list = new LinkedList();
        
        // Creating a simple linked list
        list.head = new Node(1);
        Node second = new Node(2);
        Node third = new Node(3);
        
        list.head.next = second; //
expand button
Transcribed Image Text:### Printing a Linked List in Java Given a standard linked list of integers (`int`), `L`, we want to write two methods to print the elements of `L` in order: 1. Using a loop. 2. Using recursion. We will start printing from `head.next`. #### Using a Loop Below is the Java code to print the elements of a linked list using a loop: ```java class Node { int data; Node next; Node(int d) { data = d; next = null; } } public class LinkedList { Node head; // head of the list /* Function to print linked list using loop starting from head.next */ void printListUsingLoop() { Node temp = head.next; // Start from the next node while (temp != null) { System.out.print(temp.data + " "); temp = temp.next; } } public static void main(String[] args) { LinkedList list = new LinkedList(); // Creating a simple linked list list.head = new Node(1); Node second = new Node(2); Node third = new Node(3); list.head.next = second; // Link first node with the second node second.next = third; // Link second node with the third node list.printListUsingLoop(); } } ``` #### Using Recursion Below is the Java code to print the elements of a linked list using recursion: ```java class Node { int data; Node next; Node(int d) { data = d; next = null; } } public class LinkedList { Node head; // head of the list /* Function to print linked list using recursion starting from head.next */ void printListUsingRecursion(Node node) { if (node == null) { return; // Base case: end of the list } System.out.print(node.data + " "); printListUsingRecursion(node.next); } public static void main(String[] args) { LinkedList list = new LinkedList(); // Creating a simple linked list list.head = new Node(1); Node second = new Node(2); Node third = new Node(3); list.head.next = second; //
Expert Solution
Check Mark
Knowledge Booster
Background pattern image
Similar questions
Recommended textbooks for you
Text book image
Computer Networking: A Top-Down Approach (7th Edi...
Computer Engineering
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:PEARSON
Text book image
Computer Organization and Design MIPS Edition, Fi...
Computer Engineering
ISBN:9780124077263
Author:David A. Patterson, John L. Hennessy
Publisher:Elsevier Science
Text book image
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:9781337569330
Author:Jill West, Tamara Dean, Jean Andrews
Publisher:Cengage Learning
Text book image
Concepts of Database Management
Computer Engineering
ISBN:9781337093422
Author:Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:Cengage Learning
Text book image
Prelude to Programming
Computer Engineering
ISBN:9780133750423
Author:VENIT, Stewart
Publisher:Pearson Education
Text book image
Sc Business Data Communications and Networking, T...
Computer Engineering
ISBN:9781119368830
Author:FITZGERALD
Publisher:WILEY