?
Solved

from linked list to recursion

Posted on 2004-04-13
15
Medium Priority
?
325 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
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month10 days, 23 hours left to enroll

770 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