Click here to Skip to main content
15,924,195 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm practicing programming in java with lists, because in a while this exam. i can't solve 3 methods

1st: Write a method, belonging to a hypothetical list of numbers, which concatenates the L2 list passed as an argument to the end of the current list.

2nd: Write a method, belonging to a hypothetical list of numbers, which searches for an element and returns a new list containing the numbers corresponding to the position in which the element appears in the given list. For example if L is 1 -> 15 -> 1 -> 9 -> 8 -> 1 and the element to search is 1, then the method returns the list of positions where 1 is present in the list L. The returned list, in this case is: 1 -> 3 -> 6 (I have separated 2 methods one, "search" and the other "modify") Here I wrote:

What I have tried:

1st method I wrote:
Java
public void concatenazione(Lista L,Lista L2) {
    Nodo p=head;
    while(p.getLink()!=null) {
        p=p.setLink(p);
        p=p.getLink();
    }
}


2nd method I wrote:
Java
public int ricerca(int ricerca) {
    Nodo p=head;
    int i=0;
    while(p.getLink()!=null) {      //Scorri la lista
        if(p.getElemento()==ricerca) {
            System.out.print(i++);
        }
        p=p.getLink();  //scorimento
        i++;
    }
    return i++;
}


public void modificare(Nodo L) {
    L=head;
    int disparo=0;
    while(L.getLink()!=null) {
        if(L.getElemento()/2==1) {
            L.setElemento(disparo);
            L.getLink().getLink();  //riferisce al prossimo nodo
            L.setElemento(disparo);
        }
        L=L.getLink();      //Scorimento
    }
}
Posted
Updated 23-Aug-22 0:33am

While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 
You are asked to write 2 methods. Why are you providing 3 ones?

Quote:
public void concatenazione(Lista L,Lista L2) {
Nodo p=head;
while(p.getLink()!=null) {
p=p.setLink(p);
p=p.getLink();
}
}


This should be more similar to
Java
public void concatenazione(Lista L2) 
{
    Nodo p = head;
    Nodo q = L2.getHead();

    while(p.getLink()!=null) 
        p = p.getLink();

    while (q != null)
    {
      // here you should create a new node (a duplicate of q) say 'qcopy'
      p.setLink(qcopy);
      p = qcopy;
      q = q.getLink();
    }
}



The second method should, in just one list traversing, produce the expected output. That is, while traversing the input list, if the item is equal to the passed one, than its postion should be added to the output list.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900