Question

In: Computer Science

1. (10 pts) Define the nodes in the LinkedList. Create the LinkedList using the ListNode class....

1. (10 pts) Define the nodes in the LinkedList. Create the LinkedList using the ListNode class. Create a method to find a node with given value in a LinkedList. Return the value is this value exists in the LinkedList. Return null if not exists. Use these two examples to test your method.

Example 1:

Input: 1 -> 2 -> 3, and target value = 3

Output: 3

Example 2:

Input: 1 -> 2 -> 3, and target value = 4

Output: null

Using Eclipse for Java

Solutions

Expert Solution

/*If you any query do comment in the comment section else like the solution*/

class ListNode
{
        Node head; 
        class Node
        {
                int val;
                Node link;
                Node(int d) { 
                        val = d; link = null; 
                }
        }                               
        public void push(int val)
        {       
                Node newNode = new Node(val);           
                newNode.link = head;
                head = newNode;
        }
        int searchByTarget(int target) {
                Node ptr = head;
                while(ptr != null) {
                        if(ptr.val == target) {
                                return target;
                        }
                        ptr = ptr.link;
                }
                return 0;
        }
        public static void main(String args[])
        {
                ListNode llist = new ListNode();
                llist.push(1);
                llist.push(2);
                llist.push(3);
                int target = 4;
                int res = llist.searchByTarget(target);
                if(res != 0)
                        System.out.println(res);
                else 
                        System.out.println("null");
        }
} 


Related Solutions

Find a node in a LinkedList In Java: 1. (2 pts) Define the nodes in the...
Find a node in a LinkedList In Java: 1. (2 pts) Define the nodes in the LinkedList. 2. (2 pts) Create the LinkedList using the ListNode class. 3. (4 pts) Create a method to find a node with given value in a LinkedList. Return the value is this value exists in the LinkedList. Return null if not exists. 4. (2 pts) Use these two examples to test your method. Example 1: Input: 1 -> 2 -> 3, and target value...
Define empty methods in Queue class using LinkedList class in Java ------------------------------------------------------------------------------- //Queue class public class...
Define empty methods in Queue class using LinkedList class in Java ------------------------------------------------------------------------------- //Queue class public class Queue{ public Queue(){ // use the linked list } public void enqueue(int item){ // add item to end of queue } public int dequeue(){ // remove & return item from the front of the queue } public int peek(){ // return item from front of queue without removing it } public boolean isEmpty(){ // return true if the Queue is empty, otherwise false }...
Define empty methods in Stack class using LinkedList class in Java ------------------------------------------------------------------------------- //Stack class public class...
Define empty methods in Stack class using LinkedList class in Java ------------------------------------------------------------------------------- //Stack class public class Stack{ public Stack(){ // use LinkedList class } public void push(int item){ // push item to stack } public int pop(){ // remove & return top item in Stack } public int peek(){ // return top item in Stack without removing it } public boolean isEmpty(){ // return true if the Stack is empty, otherwise false } public int getElementCount(){ // return current number...
Create a concrete LinkedList class that extends the provided ALinkedList class. You will need to override...
Create a concrete LinkedList class that extends the provided ALinkedList class. You will need to override the extract()method in your class. You can use your main() method for testing your method (although you do not need to provide a main method). Recall that a list is an ordered collection of data X_0, X_1, X_2, ..., X_n-1 The extract(int start, int end) method removes all elements X_start, X_start_1, ..., X_end-1 from the list. It also returns all removed elements as a...
using java Consider the following LinkedList that is composed of 4 nodes containing 13, 7, 24,...
using java Consider the following LinkedList that is composed of 4 nodes containing 13, 7, 24, 1. Assume that the Node.value is an int, and the reference to the first value is a Node called n. Write a method that computes the sum of all the values in the nodes of a linked list. For example, your method shall return the sum of all the nodes, in this example shall return 45
Create an ArrayListReview class with one data field of ArrayList and one with LinkedList with the...
Create an ArrayListReview class with one data field of ArrayList and one with LinkedList with the generic type passed to the class. (2 point) 2. Create a constructor that populate an array list and the LinkedList filled with the generic type through inserting new elements into the specified location index-i in the list. (2 points) 3. You have been given the job of creating a new order processing system for the Yummy Fruit CompanyTM. The system reads pricing information for...
PYTHON- create a queue class that uses a LINKEDLIST in order to store data. this queue...
PYTHON- create a queue class that uses a LINKEDLIST in order to store data. this queue will call on a linkedlist class class Queue: def __init__(self): self.items = LinkedList() #has to be O(1) def enqueue(self, item): #has to be O(1) def dequeue(self): def is_empty(self): def __len__(self):
C++ Using an appropriate definition of ListNode, design a simple linked list class with only two...
C++ Using an appropriate definition of ListNode, design a simple linked list class with only two member functions and a default constructor: void add(double x); boolean isMember(double x); LinkedList( ); The add function adds a new node containing x to the front (head) of the list, while the isMember function tests to see if the list contains a node with the value x. Test your linked list class by adding various numbers to the list and then testing for membership....
Using Linked List, create a Java program that does the following without using LinkedList from the...
Using Linked List, create a Java program that does the following without using LinkedList from the Java Library. and please include methods for each function. Create a menu that contains the following options : 1. Add new node at the end of LL. ( as a METHOD ) 2. Add new node at the beginning of LL. ( as a METHOD ) 3. Delete a node from the end of LL. ( as a METHOD ) 4. Delete a node...
Create a class library (.dll) in a particular namespace (using csc or VS2017) to define one...
Create a class library (.dll) in a particular namespace (using csc or VS2017) to define one or more classes, each contains fields, methods, constructors, and/or properties, including r/w properties, auto-implemented and/or expression-bodied properties.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT