473,396 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Editing add method

10
Now modify the add method of DoubleLinkedSet so that it properly links the nodes in both directions

I have to modify the add method of my doublelinked set so that it links nodes in both directions. here is part of the code.

Expand|Select|Wrap|Line Numbers
  1.  
  2. public void add (T element)
  3.     {
  4.        if (!(contains(element)))
  5.        {
  6.           DoubleNode<T> node = new DoubleNode<T> (element);
  7.           node.setNext(contents);
  8.           contents = node;
  9.           count++;
  10.        }
  11.     }
Now I tired to change it up and add a .setPrevious too but it did not work. Basically I have no idea what to do Any help would be greatful
Feb 18 '07 #1
8 1961
Will2k
10
bump...........
Feb 18 '07 #2
Ganon11
3,652 Expert 2GB
Your DoubleNode class should have a setPrevious function to have a link to the previous data...I think we need to see a little more code for this problem.
Feb 18 '07 #3
Will2k
10
Here is the rest of the whole code. I tried putting a setPrevious in there but whenever I tried it, it didnt put the numbers before and after the add.


Expand|Select|Wrap|Line Numbers
  1. package jss2;
  2. import jss2.exceptions.*;
  3. import java.util.*;
  4. public class DoubleLinkedSet<T> implements SetADT<T> {
  5.    private static Random rand = new Random();
  6.  
  7.     private int count;  // the current number of elements in the set
  8.  
  9.     private DoubleNode<T> contents;
  10.  
  11.     //-----------------------------------------------------------------
  12.     //  Creates an empty set.
  13.     //-----------------------------------------------------------------
  14.     public DoubleLinkedSet()
  15.     {
  16.        count = 0;
  17.        contents = null;
  18.     }
  19.  
  20.     //-----------------------------------------------------------------
  21.     //  Adds the specified element to the set if it's not already
  22.     //  present.
  23.     //-----------------------------------------------------------------
  24.     public void add (T element)
  25.     {
  26.        if (!(contains(element)))
  27.        {
  28.           DoubleNode<T> node = new DoubleNode<T> (element);
  29.           node.setNext(contents);
  30.           contents = node;
  31.           count++;
  32.        }
  33.     }
  34.  
  35.     //-----------------------------------------------------------------
  36.     //  Adds the contents of the parameter to this set.
  37.     //-----------------------------------------------------------------
  38.     public void addAll (SetADT<T> set)
  39.     {
  40.        Iterator<T> scan = set.iterator();
  41.  
  42.        while (scan.hasNext())
  43.           add (scan.next());
  44.     }
  45.  
  46.     //-----------------------------------------------------------------
  47.     //  Removes a random element from the set and returns it. Throws
  48.     //  an EmptySetException if the set is empty.
  49.     //-----------------------------------------------------------------
  50.     public T removeRandom() throws EmptySetException
  51.     {
  52.        DoubleNode<T> previous, current;
  53.        T result = null;
  54.  
  55.        if (isEmpty())
  56.           throw new EmptySetException();
  57.  
  58.        int choice = rand.nextInt(count) + 1;
  59.  
  60.        if (choice == 1)
  61.        {
  62.           result = contents.getElement();
  63.           contents = contents.getNext();
  64.        }
  65.        else
  66.        {
  67.           previous = contents;
  68.           for (int skip=2; skip < choice; skip++)
  69.              previous = previous.getNext();
  70.           current = previous.getNext();
  71.           result = current.getElement();
  72.           previous.setNext(current.getNext());
  73.        }
  74.  
  75.        count--;
  76.  
  77.        return result;
  78.     }
  79.  
  80.     //-----------------------------------------------------------------
  81.     //  Removes the specified element from the set and returns it.
  82.     //  Throws an EmptySetException if the set is empty and a
  83.     //  NoSuchElemetnException if the target is not in the set.
  84.     //-----------------------------------------------------------------
  85.     public T remove (T target) throws EmptySetException,
  86.                                       NoSuchElementException
  87.     {
  88.        boolean found = false;
  89.        DoubleNode<T> previous, current;
  90.        T result = null;
  91.  
  92.        if (isEmpty())
  93.           throw new EmptySetException();
  94.  
  95.        if (contents.getElement().equals(target))
  96.        {
  97.           result = contents.getElement();
  98.           contents = contents.getNext();
  99.        }
  100.        else
  101.        {
  102.           previous = contents;
  103.           current = contents.getNext();
  104.           for (int look=0; look < count && !found; look++)
  105.              if (current.getElement().equals(target))
  106.                 found = true;
  107.              else
  108.              {
  109.                 previous = current;
  110.                 current = current.getNext();
  111.              }
  112.  
  113.           if (!found)
  114.              throw new NoSuchElementException();
  115.  
  116.           result = current.getElement();
  117.           previous.setNext(current.getNext());
  118.        }
  119.  
  120.        count--;
  121.  
  122.        return result;
  123.     }
  124.  
  125.     //-----------------------------------------------------------------
  126.     //  Returns a new set that is the union of this set and the
  127.     //  parameter.
  128.     //-----------------------------------------------------------------
  129.     public SetADT<T> union (SetADT<T> set)
  130.     {
  131.        DoubleLinkedSet<T> both = new DoubleLinkedSet<T>();
  132.  
  133.        DoubleNode<T> current = contents;
  134.  
  135.        while (current != null)
  136.        {
  137.           both.add (current.getElement());
  138.           current = current.getNext();
  139.        }
  140.  
  141.        Iterator<T> scan = set.iterator();
  142.        while (scan.hasNext())
  143.           both.add (scan.next());
  144.  
  145.        return both;
  146.     }
  147.  
  148.     //-----------------------------------------------------------------
  149.     //  Returns true if this set contains the specified target
  150.     //  element.
  151.     //-----------------------------------------------------------------
  152.     public boolean contains (T target)
  153.     {
  154.        boolean found = false;
  155.  
  156.        DoubleNode<T> current = contents;
  157.  
  158.        for (int look=0; look < count && !found; look++)
  159.           if (current.getElement().equals(target))
  160.              found = true;
  161.           else
  162.              current = current.getNext();
  163.  
  164.        return found;
  165.     }
  166.  
  167.     //-----------------------------------------------------------------
  168.     //  Returns true if this set contains exactly the same elements
  169.     //  as the parameter.
  170.     //-----------------------------------------------------------------
  171.     public boolean equals (SetADT<T> set)
  172.     {
  173.        boolean result = false;
  174.        DoubleLinkedSet<T> temp1 = new DoubleLinkedSet<T>();
  175.        DoubleLinkedSet<T> temp2 = new DoubleLinkedSet<T>();
  176.        T obj;
  177.  
  178.        if (size() == set.size())
  179.        {
  180.           temp1.addAll(this);
  181.           temp2.addAll(set);
  182.  
  183.           Iterator<T> scan = set.iterator();
  184.  
  185.           while (scan.hasNext())
  186.           {
  187.              obj = scan.next();
  188.              if (temp1.contains(obj))
  189.              {
  190.                 temp1.remove(obj);
  191.                 temp2.remove(obj);
  192.              }
  193.  
  194.           }
  195.           result = (temp1.isEmpty() && temp2.isEmpty());
  196.        }
  197.  
  198.        return result;
  199.     }
  200.  
  201.     //-----------------------------------------------------------------
  202.     //  Returns true if this set is empty and false otherwise.
  203.     //-----------------------------------------------------------------
  204.     public boolean isEmpty()
  205.     {
  206.        return (size() == 0);
  207.     }
  208.  
  209.     //-----------------------------------------------------------------
  210.     //  Returns the number of elements currently in this set.
  211.     //-----------------------------------------------------------------
  212.     public int size()
  213.     {
  214.        return count;
  215.     }
  216.  
  217.     //-----------------------------------------------------------------
  218.     //  Returns an iterator for the elements currently in this set.
  219.     //-----------------------------------------------------------------
  220.     public Iterator<T> iterator()
  221.     {
  222.        return new DoubleLinkedIterator<T> (contents, count);
  223.     }
  224.  
  225.     //-----------------------------------------------------------------
  226.     //  Returns a string representation of this set.
  227.     //-----------------------------------------------------------------
  228.     public String toString()
  229.     {
  230.        String result = "";
  231.  
  232.        DoubleNode<T> current = contents;
  233.  
  234.        while (current != null)
  235.        {
  236.           result += current.getElement().toString() + "\n";
  237.           current = current.getNext();
  238.        }
  239.  
  240.        return result;
  241.     }
  242.  }
  243.  
  244.  
  245.  
Feb 18 '07 #4
Ganon11
3,652 Expert 2GB
From what I understand, your list should have 2 DoubleNodes - one for the front end of the list, and one for the back. Was this data structure given to you, or did you make it yourself?
Feb 18 '07 #5
Will2k
10
It was given. I tried this:


Expand|Select|Wrap|Line Numbers
  1. public void add (T element)
  2.     {
  3.        if (!(contains(element)))
  4.        {
  5.           DoubleNode<T> node = new DoubleNode<T> (element);
  6.           node.setNext(contents);
  7.           contents = node;
  8.           count++;
  9. node.setPrevious(contents)
  10. contents=node;
  11. count++;
  12.  
  13.  
  14.        }
  15.     }
Still no go for that.
Feb 18 '07 #6
Ganon11
3,652 Expert 2GB
Well, you wouldn't be incrementing count again - after all, you're only adding one node. What you'll have to do is use current's previous node to set the new node's previous.
Feb 18 '07 #7
How would you modify the remove method so that they take advantage of the doubly-linked structure. Why would you eliminate the variable previous?
Sep 23 '07 #8
Ganon11
3,652 Expert 2GB
I'm sorry, I don't quite understand your question...
Sep 23 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Dan Weeb | last post by:
Hi All, I have struggled through this far with help from many of you so thanks. I am stuck again. I am really new to this so don't be harsh :-) There are a few problems. You can run the script...
1
by: nospam | last post by:
Amazon wins patent for ordering forms, Collapsing and Maximizing Form Areas.... NAME OF PATENT Method and system for displaying and editing of information # 6,615,226 ...
1
by: Sam | last post by:
I'm having problem. I want to have a block of code run, after a user edits the label of a node, so I put the code in the event method of the afterLabelEdit event. The problem is that this code is...
0
by: Marc Scheuner [MVP ADSI] | last post by:
Folks, I'm trying to get a grip on customizing my WebBrowser control hosted on a WinForms form. I've come as far as finding out about IDocHostUIHandler, and I've implemented the...
12
by: Ron Weldy | last post by:
I have a test server runinng 2003/IIS 6 with a mixture of asp and asp.net files. On my workstation I have a share set up to the folder where the web files reside. I am just doing quick and dirty...
10
by: Nathan Sokalski | last post by:
I have a DataList control with an EditTemplate. Three of the controls in this template include a Calendar, a Button with CommandName="update", and a Button with CommandName="cancel". Whenever I...
0
by: Vanga Sasidhar | last post by:
Hi All, Please help me in the following problem. I am having some files with AVI extension. I want to make two programs in Visual Basic .NET which will work with these AVI Files. One program...
5
by: Ian Bicking | last post by:
I got a puzzler for y'all. I want to allow the editing of functions in-place. I won't go into the reason (it's for HTConsole -- http://blog.ianbicking.org/introducing-htconsole.html), except that...
2
by: ritesh | last post by:
Hi, I'm facing a problem in which I need to edit an already created file, and the editing needs to be done at the start of the file rather then appending to the file. OS - Linux,Solaris ...
3
by: cpnet | last post by:
I have a GridView which I'm populating from an ObjectDataSource (give the GridView a DataTable). The GridView will have about 20 rows, and only one editable column. The editable column consists...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.