Solved

from linked list to recursion

Posted on 2004-04-13
15
315 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: 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 250 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

Industry Leaders: 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!

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This video teaches viewers about errors in exception handling.
Suggested Courses

732 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