Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

from linked list to recursion

Posted on 2004-04-13
15
Medium Priority
?
330 Views
Last Modified: 2010-03-31
Can anyone help me to change my code to a recursive way?

public void delete( String key)
{
      boolean flag = false;
      Node last, now;

      if (first == null)
        {
          System.out.println("The flavor is not on the list");
        }
        else
        {
          if ( ( (Flavor) (first.getData())).getName().equals(key)) {
            first = first.getNext();
          }
          else {
            now = first.getNext();
            last = first;
            while (now != null && !flag) {
              if ( ( (Flavor) (now.getData())).getName().equals(key)) {
                flag = true;
                last.setNext(now.getNext());
              }

              last = now;
              now = now.getNext();
            }
            if (!flag)
              System.out.println("The flavor is not on the list");
          }
        }
}
//********************************
// Displays the contents of the data in the list
// Assumes:  the list starts off as a NULL-terminated linked list
public void display( )
{
   Node  temp;
   temp = first;
   while (temp != null)
   {
     ((Flavor)(temp.getData())).display();
     temp = temp.getNext();
   }
}



0
Comment
Question by:will_joy99
  • 6
  • 5
  • 4
15 Comments
 
LVL 92

Expert Comment

by:objects
ID: 10820418
it doesn't really make sense to do your delete recursively.
And your display method is already recursive.
0
 

Author Comment

by:will_joy99
ID: 10820481
but that's what i have to do.. is there a way to make it recursive?
0
 
LVL 92

Expert Comment

by:objects
ID: 10820508
you can't really, why do you want to make it recursive?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10820511


public void delete( String key)
{
     boolean flag = false;
     Node last, now;

     if (first == null)
        {
          System.out.println("The flavor is not on the list");
        }
        else
        {
          if ( ( (Flavor) (first.getData())).getName().equals(key)) {
            first = first.getNext();
          }
          else {
            now = first.getNext();
            last = first;

            if (now != null && !flag)
             {
              if ( ( (Flavor) (now.getData())).getName().equals(key)) {
                flag = true;
                last.setNext(now.getNext());
              }

              last = now;
              now = now.getNext();
              delete(key);
            }
            if (!flag)
              System.out.println("The flavor is not on the list");
          }
        }
}
0
 
LVL 92

Expert Comment

by:objects
ID: 10820540
:D Thats recursive, but it aint going to work.

0
 

Author Comment

by:will_joy99
ID: 10820605
can you almost help me with the sorting? have to change it to a recursive function

public void sortFlavors()
{
  Node min, minlast, last, now, lastwalker, walker, tmp1, tmp2 = null;

  if (first == null)
  {
    System.out.println("List is empty.");
  }
  else if (first.getNext() == null)
  {
    System.out.println("List contains only one item and thus does not need to be sorted.");
  }
  else
  {
      last = null;
      minlast = null;
    min = first;
    now = first;

    while (now != null) {
      min = now;

      lastwalker = now;
      walker = now.getNext();

      while (walker != null) {
       
        if ( ( (Flavor) walker.getData()).getStock() < ( ( (Flavor)
            min.getData()).getStock()))
        {
          minlast = lastwalker;
          min = walker;
        }

          lastwalker = walker;
        walker = walker.getNext();
      }

      if (min != now)
      {
        if (last == null)
        {
          tmp1 = first;
          tmp2 = min.getNext();

          first = min;
          minlast.setNext(tmp1);
          first.setNext(tmp1.getNext());
          tmp1.setNext(tmp2);
        }
        else
        {
            tmp1 = now;
            tmp2 = min.getNext();

            last.setNext(min);
            minlast.setNext(tmp1);
            min.setNext(now.getNext());
            tmp1.setNext(tmp2);

        }

        last = min;
        now = min.getNext();
      }
      else
      {
          last = now;
          now = now.getNext();
      }
    }
  }
}
0
 

Author Comment

by:will_joy99
ID: 10820675
the delete function doesn't work
0
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10820688
what is the problem???
0
 
LVL 92

Expert Comment

by:objects
ID: 10820695
> the delete function doesn't work

told u ;)

if this is an assignment can u post the q that requests the delete method be implemented recursively.
0
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10820704
Am not sure about the perfect execution.... this is like a starting point... u can go from here...
Since I don't have your classes here... I can't test anything.... I can only give a rough idea how to do it?


public void sortFlavors(Node start, Node compare)
{
  if (start == null)
        return;
   else if (start.getNext() == null)
        return;
   else
   {
      if ( ( (Flavor) start.getData()).getStock() > ( ( (Flavor)
            compare.getData()).getStock()))
        {
          Node temp = compare.getNext();
          compare.setNext(start);
          start.setNext(compare);
        }      

       sortFlavours(start.getNext(), compare.getNext());
   }
}
0
 

Author Comment

by:will_joy99
ID: 10820721
Re-do exercise 03-19-B (flavor link).  Write the display, insertInOrder, and delete routines RECURSIVELY
0
 

Author Comment

by:will_joy99
ID: 10820760
btw... can the sort method be written a way that it doesn't take in any parameters while writing it recursively?
0
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10820770
All the variables should be at class level then.... That is not a good practice in recursion
0
 
LVL 92

Accepted Solution

by:
objects earned 1000 total points
ID: 10820773
Strange request :)

On;y way I can think to achieve it would be to handle the locating of the node to remove recursively.

public void delete( String key)
{
   doDelete(first, key);
}

private void doDelete(Node n, String key)
{
     if (n == null)
     {
         System.out.println("The flavor is not on the list");
     }
     else if ( ( (Flavor) (n.getData())).getName().equals(key))
     {
         // handle removeing node
     }
     else
     {
         doDelete n.getNext(), key);
     }
}

     
0
 
LVL 92

Expert Comment

by:objects
ID: 10820776
> can the sort method be written a way that it doesn't take in any parameters while writing it recursively?

No that is not possible.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month21 days, 3 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question