0

Hello I implemented a multiset using a linkedlist and I want to implement the multiset using sorted linkedlist. This is multiset abstract class.

import java.io.PrintStream;

public abstract class Multiset<T> {
    /**
     * Delimiter string for print operation.
     */
    protected static final String printDelim = " | ";

    public abstract void add(T item);

    public abstract int search(T item);

    public abstract void removeOne(T item);

    public abstract void removeAll(T item);

    public abstract void print(PrintStream out);
} 

This is my implementation of linkedlist.

import java.io.PrintStream;

public class LinkedListMultiset<T> extends Multiset<T> {
    protected Node mHead;
    protected int mLength;

    public LinkedListMultiset() {
        // Implement me!
        mHead = null;
        mLength = 0;
    }

    public void add(T item) {
        Node newNode = new Node((String) item);
        if (mHead == null)
            mHead = newNode;
        else {
            Node currNode = mHead;
            Node parentNode = null;
            while (currNode != null) {
                if (currNode.getValue().
                        equals(newNode.getValue())) {
                    currNode.addCounter();
                    return;
                }
                parentNode = currNode;
                currNode = currNode.getNext();
            }
            parentNode.setNext(newNode);
        }
        mLength++;
    }

    public int search(T item) {
        Node currNode = mHead;

        while (currNode != null) {
            if (currNode.getValue().equals((String) item)) {
                return currNode.getCounter();
            }
            currNode = currNode.getNext();
        }

        return 0;
    }

    public void removeOne(T item) {
        Node currNode = mHead;
        Node lastNode = null;

        while (currNode != null) {
            if (currNode.getValue().equals((String) item)) {
                currNode.minusCounter();
                if (currNode.getCounter() == 0) {
                    if (currNode == mHead)
                        mHead = currNode.getNext();
                    else
                        lastNode.setNext
                                (currNode.getNext());
                    mLength--;
                }
                return;
            }
            lastNode = currNode;
            currNode = currNode.getNext();
        }
    }

    public void removeAll(T item) {
        Node currNode = mHead;
        Node lastNode = null;

        while (currNode != null) {
            if (currNode.getValue().equals((String) item)) {
                if (currNode == mHead)
                    mHead = currNode.getNext();
                else
                    lastNode.setNext(currNode.getNext());
                mLength--;
                return;
            }
            lastNode = currNode;
            currNode = currNode.getNext();
        }
    }

    public void print(PrintStream out) {
        Node currNode = mHead;

        while (currNode != null) {
            out.printf("%s | %d\n", currNode.getValue()
                    , currNode.getCounter());
            currNode = currNode.getNext();
        }
    }

    private class Node {
        protected String mValue;
        protected Node mNext;

        int counter;

        public Node(String value) {
            mValue = value;
            mNext = null;
            counter = 1;
        }

        public void addCounter() {
            counter++;
        }

        public void minusCounter() {
            counter--;
        }

        public int getCounter() {
            return counter;
        }

        public String getValue() {
            return mValue;
        }

        public Node getNext() {
            return mNext;
        }

        public void setValue(String value) {
            mValue = value;
        }

        public void setNext(Node next) {
            mNext = next;
        }
    }
}

I want to implement sorted linkedlist but I want to change my code as minimum as possible.

beresfordt
  • 5,088
  • 10
  • 35
  • 43
John Sick
  • 427
  • 6
  • 15
  • What should be sorted? The lists or the values in the lists (although I'd assume they are basically equal)? Why not just use a `TreeSet>` with an appropriate `Comparator>`? – Thomas Mar 30 '16 at 09:15
  • @Thomas The values will be sorted. No i donot want to use treeSet. I have to implement it using Sorted linkedList – John Sick Mar 30 '16 at 09:23
  • I'm not quite sure I understand your requirement correctly: from your code it looks like you're just implementing a linked list but name it "set". So it's basically a sorted linked list you're after? In that case you could just implement the search method so that it returns a node that equals the item passed or the next higher/lower node if no such node exists. When adding a new item you then do a search first and add the new item right before/after the node returned. If search returns null (no higher/lower node) you add the item at the end/front. – Thomas Mar 30 '16 at 09:29
  • @Thomas so i just need to change add method and rest of the code worked with the sortedLinkedList – John Sick Mar 30 '16 at 09:53
  • Basically yes, you change your add method to insert nodes in a sorted way. Btw, I guess your search method should not return 0 but probably the index of the item if found. You could create an internal search method to support both `search()` and `add()` since it's basically the same approach. – Thomas Mar 30 '16 at 10:03

0 Answers0