Question

#1. Single linked list - find... Extra info/hint? It's free For this problem, I have a...

#1. Single linked list - find...

Extra info/hint? It's free

For this problem, I have a complete linked list program which supports the following commands from the user:
insert, display, delete, average, find, insertBefore, insertAfter. 
insert allows them to insert a number into the current list,
display will display the numbers in the current list
etc.
The program is complete except that I have removed the body of the method for find.

Given the following class for the nodes in a linked list:
public class Node {
   ...
   public Node getNext() {...}         // return next field
   public int getData() {...}         // returns data
}

Assuming that the variable head points to (i.e. contains the address of) the first node of a linked list, 
write the statements to find the first occurrence of the data value x in the linked list. For example, find(4)
would return something like "Found at node position 20" or "Not found" Assume that the nodes in the linked
list are counted starting from 1. Your statements will be inserted inside a method like the following:
   public String find(int x) {
      // Whatever statements you provide in response to this question will 
      // be inserted here BY THE SYSTEM and then compiled and tested as part of
      // a larger program which does many other things with the linked list
   }
Sample console I/O execution sequence
Linked list code sample. Options are:
(insert, display, delete, average, find, insertBefore, insertAfter)
What would you like: Find 4
Not found
What would you like: insert 10
inserted 10
What would you like: insert 30
inserted 30
What would you like: Find 30
Found at node position 1
What would you like: Find 10
Found at node position 2
What would you like: Exit
Bye
2. Single linked list - average...

Extra info/hint? It's free

For this problem, I have a complete linked list program which supports the following commands from the user:
insert, display, delete, average, find, insertBefore, insertAfter. 
insert allows them to insert a number into the current list,
display will display the numbers in the current list
etc.
The program is complete except that I have removed the body of the method for averaging the numbers in the linked list..
Given the following class for the nodes in a linked list:
public class Node {
   ...
   public Node getNext() {...}         // return next field
   public int getData() {...}         // returns data
}

Assuming that the variable head points to (i.e. contains the address of) the first node of a linked list, write the
statements to find the average of all data values in the linked list. For example, average() will return something like
"avg: 2.67" or "Empty". The average should be returned as a string with exactly two digits of decimal accuracy, or
Empty if the list is empty. Your statements will be inserted inside a method like the following:
   public String average() {
      // Whatever statements you provide in response to this question will 
      // be inserted here BY THE SYSTEM and then compiled and tested as part of
      // a larger program which does many other things with the linked list
   }
Sample console I/O execution sequence
Linked list code sample. Options are:
(insert, display, delete, average, find, insertBefore, insertAfter)
What would you like: insert 4
inserted 4
What would you like: insert 1
inserted 1
What would you like: insert 3
inserted 3
What would you like: average
avg: 2.67
What would you like: delete 3
deleted 3
What would you like: delete 4
deleted 4
What would you like: delete 1
deleted 1
What would you like: average
Empty
What would you like: Exit
Bye
0 0
Add a comment Improve this question Transcribed image text
Answer #1
public String find(int x) 
        { 
      String s1 = "Not found";
      String s2 ="found at Node position ";
      int pos=1;  //It is current position of visiting node.
                Node current = head; //Initialize current node where head refers first node of linked list
                while (current != null) 
                { 
                        if (current.data == x) {  //data found 
                          s2 += pos;
                          return s2; 
                        }
                        pos++;
                        current = current.next; 
                } 
       
                return s1; //data not found 
        } 

1. Make class object for accessing above function-

LinkedList list = new LinkedList();

int number = 10; //data which u want to find in the linked list.

2. For successfully returned output we will have to call above function inside the main function and store return value of find() function into new string variable like below-

String result = list.find(number);

System.out.println(result); //It shows the result according to node availability inside your linked list.

Add a comment
Know the answer?
Add Answer to:
#1. Single linked list - find... Extra info/hint? It's free For this problem, I have a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create a linked list of at least 15 different values that are prompted for and entered...

    Create a linked list of at least 15 different values that are prompted for and entered while the program is running. Appending a node attaches that node to the end of the list while inserting a node places it in order maintaining a sorted list. Create a menu driven program where you have the options to appended, insert, display, and delete a node. Display the list after first appending data showing it in no specific order, delete all nodes and...

  • Programming in C: I am trying to modify this linked list to be doubly linked list....

    Programming in C: I am trying to modify this linked list to be doubly linked list. I’m also trying to add a print in reverse function. I’m really struggling with how to change the insert function to doubly link the nodes without effecting the alphabetical sorting mechanism. Example of desired output: Enter your choice: 1 to insert an element into the list. 2 to delete an element from the list. 3 to end. ? 1 Enter a character: a The...

  • Write a function to implement linked list consisting of five nodes. Store the data in the...

    Write a function to implement linked list consisting of five nodes. Store the data in the nodes in the order given below. Also, write a function to display the data stored in the implemented linked list. in C++ programming George, Paul, Ross, Joe, Elaine, Robert1 Insert three nodes in between second node and third node in the linked list that you implemented in problem 1. Store the following data in the new (inserted) nodes in the following order. (You have...

  • BELOW IS THE CODE I ALREADY HAVE Linked List - Delete Modify Lab 5 and include...

    BELOW IS THE CODE I ALREADY HAVE Linked List - Delete Modify Lab 5 and include the method to delete a node from the Linked List. In summary: 1) Add the method Delete 2) Method call to delete, with a number that IS in the list 3) Method call to delete, with a number that is NOT in the list - Be sure to include comments - Use meaningful identifier names (constants where appropriate) import java.io.*; 1/ Java program to...

  • PLEASE USE C++ Source Code Attached is a linked list with 2 nodes. You can use...

    PLEASE USE C++ Source Code Attached is a linked list with 2 nodes. You can use this or write a similar one. The assignment is to write 2 functions. One function will add another node at the end of the list. The other function will delete a node. Don't forget - No dangling pointers ! Example linked source code attached below #include<iostream> using namespace std; class Node { int data; Node *next; public: void setdata(int d) {data = d;} void...

  • Using the following definition (List.h file) for a list, implement the member functions (methods) for the...

    Using the following definition (List.h file) for a list, implement the member functions (methods) for the List class and store the implementation in a List.cpp file. Use a doubly linked list to implement the list. Write the client code (the main method and other non-class methods) and put in file driver.cpp. file: List.h typedef int ElementType; class node{ ​ElementType data; ​node * next; node* prev; }; class List { public: List(); //Create an empty list bool Empty(); // return true...

  • 10.16 LAB: Grocery shopping list (linked list: inserting at the end of a list)

    10.16 LAB: Grocery shopping list (linked list: inserting at the end of a list)Hello, I searched for similar problems, but their InsertAtEnd() have two parameters, while my question asks for one.Given main(), define an InsertAtEnd() member function in the ItemNode class that adds an element to the end of a linked list.DO NOT print the dummy head node.Ex. if the input is:4 Kale  Lettuce  Carrots  Peanutswhere 4 is the number of items to be inserted; Kale, Lettuce, Carrots, Peanuts are...

  • Q) Modify the class Linked List below to make it a Doubly Linked List. Name your...

    Q) Modify the class Linked List below to make it a Doubly Linked List. Name your class DoublyLinkedList. Add a method addEnd to add an integer at the end of the list and a method displayInReverse to print the list backwards. void addEnd(int x): create this method to add x to the end of the list. void displayInReverse(): create this method to display the list elements from the last item to the first one. Create a main() function to test...

  • In C++ - Learn how to implement linked lists Part 1 Node and Linked List Class...

    In C++ - Learn how to implement linked lists Part 1 Node and Linked List Class (50 pts): Create node with public properties: Block type block and block ptr next. Create a linked list class that uses the node you generated without an add or delete method with a head and optional tail and counter. Make a driver that generates a node to test your implementation. Part 2 Add Method (30 pts): Create an add method in your linked list...

  • C++ Assignment Project 1 - NodeList Building upon the the ListNode/List code I would like you...

    C++ Assignment Project 1 - NodeList Building upon the the ListNode/List code I would like you to extend the interface of a list to have these member functions as well. struct ListNode { int element; ListNode *next; } Write a function to concatenate two linked lists. Given lists l1 = (2, 3, 1)and l2 = (4, 5), after return from l1.concatenate(l2)the list l1should be changed to be l1 = (2, 3, 1, 4, 5). Your function should not change l2and...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT