简体   繁体   中英

Java Double Linked List

Alright, my professor (Data Structures class) assigned this: Your task is to write a program that can update character access frequencies in a doubly-Link list. The program should read one character at a time from a text file that contain many characters. To make it easier, do not count spaces. Every time a character is accessed, increment its access frequency by one in the node of the list. If the frequency of the current node is higher than of its previous node, the two nodes need to be swapped in the list. Continue doing so for all the previous nodes until no more previous node has lower access frequency. Eventually, the character with the highest frequency will appear at the beginning of the list, the next highest will be in the next node, etc. Your program also need to print out the characters in the list according to the order of the list.

Here is the program I have made so far. It's just a doubly linked list as of right now. My main question is how should I go about the "Every time a character is accessed, increment its access frequency by one in the node of the list. If the frequency of the current node is higher than of its previous node, the two nodes need to be swapped in the list."?

I know there aren't any lines getting the info from a file. I'm going to add that later. Any help is appreciated!

public class DoublyLinkedList {
private class Node {
    String value;
    Node next,prev;

    public Node(String val, Node n, Node p) {
        value = val;
        next = n;
        prev=p;
    }

    Node(String val) {
        this(val, null, null);
    }
}
private Node first;
private Node last;

public DoublyLinkedList() {
    first = null;
    last = null;
}
public boolean isEmpty(){
    return first==null;
}
public int size(){
    int count=0;
    Node p=first;
    while(p!=null){
        count++;
        p=p.next;
    }
    return count;
}
public void add(String e) {

    if(isEmpty()){
        last=new Node(e);
        first=last;
    }
    else{
        last.next=new Node(e, null, last);
        last=last.next;
    }
}
public void add(int index, String e){
    if(index<0||index>size()){
        String message=String.valueOf(index);
        throw new IndexOutOfBoundsException(message);
    }
    if(index==0){
        Node p=first;
        first=new Node(e,p,null);
        if(p!=null)
            p.prev=first;
        if(last==null)
            last=first;
        return;
    }
    Node pred=first;
    for(int k=1; k<=index-1;k++){
        pred=pred.next;
    }
    Node succ=pred.next;
    Node middle=new Node(e,succ,pred);
    pred.next=middle;
    if(succ==null)
        last=middle;
    else
        succ.prev=middle;
}
public String toString(){
    StringBuilder strBuilder=new StringBuilder();
    Node p=first;
    while(p!=null){
        strBuilder.append(p.value+"\n");
        p=p.next;
    }
    return strBuilder.toString();
}
public String remove(int index){
    if(index<0||index>=size()){
        String message=String.valueOf(index);
        throw new IndexOutOfBoundsException(message);
    }
    Node target=first;
    for(int k=1; k<=index;k++){
        target=target.next;
    }
    String element=target.value;
    Node pred=target.prev;
    Node succ=target.next;
    if(pred==null)
        first=succ;
    else
        pred.next=succ;
    if(succ==null)
        last=pred;
    else
        succ.prev=pred;
    return element;
}
public boolean remove(String element){
    if(isEmpty())
        return false;
    Node target=first;
    while(target!=null&&!element.equals(target.value))
        target=target.next;
    if(target==null)
        return false;
    Node pred=target.prev;
    Node succ=target.next;
    if(pred==null)
        first=succ;
    else
        pred.next=succ;
    if(succ==null)
        last=pred;
    else
        succ.prev=pred;
    return true;
}
public static void main(String[] args){
    DoublyLinkedList list1=new DoublyLinkedList();
    String[] array={"a","c","e","f"};
    for(int i=0; i<array.length; i++){
        list1.add(array[i]);
    }
    list1.add(1,"b");
    list1.add(3,"d");
    System.out.println(list1);

}


}

Since this is a homework assigment, I'll only give hints:

  • Your Node class needs an extra field for a counter.

  • You need to iterate through the list to find the accessed character and increment its counter value.

  • You need a temporary Node object to swap nodes. Try it yourself first, then google it. It's an essential process every programmer must know.

Advice:

"I know there aren't any lines getting the info from a file." . You would be better off writing that code now, so that you can test what you have already written.

The other problem is that what you have written so far is a generic linked list, ignoring the requirement which say how the list is to be used. As a result, you have:

  • implemented a bunch of methods that appear to be unnecessary, and
  • not implemented the Node class correctly for the requirements.

Go back and look at the requirements, and work out what methods you actually need, and then implement them. (What you have done so far is a "bottom up" design that is largely ignoring what the top level needs. You would have been better of with a "top down" approach.)

The problem you are asked to solve is collating characters, not creating a "general purpose" linked list data structure.

I would recommend breaking down the procedure into the component parts. You know you need to keep and update a count, as Sebastian says above. You also know you need to be able to compare a node's count with the count of the node above it in the rankings. You know you need to be able to swap two nodes. You should have methods for those things. Think through what needs to happen in each broken-down method.

I always recommend a physical approach for these kinds of problems to get a feel for them: Try doing this with a set of note cards or post-it notes. On each one, write an object name and the fields for the Node object. Write field values in pencil. Jot down other fields (like the reference to the first element) on a sheet of paper. Then step through your algorithm and see what needs to change on each update. (note: Because this is a doubly-linked list, your changes should survive shuffling your stack of cards. Try that and see)

Good luck with the assignment!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM