Solved

from linked list to recursion

Posted on 2004-04-13
15
297 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
firstswap challenge 20 77
simple java question 3 56
servlet requestdispatcher include and forward differences 1 31
configure dependency in POM for new database 3 25
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

778 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