Solved

from linked list to recursion

Posted on 2004-04-13
15
281 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
 
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This video teaches viewers about errors in exception handling.

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now