搜索功能双向链表

时间:2018-09-30 19:11:19

标签: java search linked-list doubly-linked-list

我正在尝试让我的搜索方法搜索我的双向链接列表,但是当我收到控制台输入时,它说列表中没有显示元素,尽管显示了显示和长度方法。我的教授说,我的insertStart方法中的逻辑有缺陷,但我看不到。请帮忙!

    package assignment_3_1;

import java.util.*;

//MyLinkedList class
public class MyLinkedList<T> {

//Default variables
private ListNode head;
private ListNode tail;
private int length;

//Inner class for listnodes
private class ListNode<T> {
    T data;
    private ListNode<T> next;
    private ListNode<T> previous;


    //Listnode constructor
    public ListNode(T data) {
    this.data = data;
    }

}

//MyLinkedList constructor
public MyLinkedList() {
    this.head = null;
    this.tail = null;
    this.length = 0;
}

//Method checking if list is empty
public boolean isEmpty() {
    return length == 0;
}

//Method to check size of list
public void listSize() {
    System.out.println("    List Size    ");
    System.out.println("=================");
    System.out.println("There are " + length + " elements in the list.");
    System.out.println();
}

//Method to display list fowards
public void displayFowards() {
    if (isEmpty()) {
        return;
    }

    ListNode temp = head;
    while (temp != null){
        System.out.print(temp.data + " ==> ");
        temp = temp.next;
    }
    System.out.println("null");
}

//Method to display list backwards
public void displayBackwards() {
    if (isEmpty()) {
        return;
    }

    ListNode temp = tail;
    while (temp != null){
        System.out.print(temp.data + " ==> ");
        temp = temp.previous;
    }
    System.out.println("null");
}

//Method for inserting NewNode in beginning
public void insertStart(T inserted) {
    ListNode newNode = new ListNode(inserted);
    if(isEmpty()) {
        tail = newNode;
    }
    else {
        head.previous = newNode;
        newNode.next = head;            
    }
    head = newNode;
    length++;
}

//Method to find middle node
public ListNode getMiddleNode() {
    if (head == null) {
        return null;
    }

    ListNode slowPointer = head;
    ListNode fastPointer = head.next;

    while (fastPointer != null && fastPointer.next != null) {
        slowPointer = slowPointer.next;
        fastPointer = fastPointer.next.next;
    }
    return slowPointer;
}

//Method for inserting NewNode in middle
public void insertMiddle(T inserted) {
    ListNode newNode = new ListNode(inserted);

    if (isEmpty()) {
        head = newNode;
    }
    else {
    ListNode middle = getMiddleNode();            
    ListNode newMiddle = middle.next;
    newNode.next = newMiddle;
    middle.next = newNode;
    newNode.previous = middle;
    newMiddle.previous = newNode;
    length++;
    }
}
//Method for inserting NewNode in end
public void insertEnd(T inserted) {
    ListNode newNode = new ListNode(inserted);
    if(isEmpty()) {
        head = newNode;
    }
    else {
        tail.next = newNode;
        newNode.previous = tail;
    }
    tail = newNode;
    length++;
}   

//Method for removing First node
public ListNode removeFirst() {
    if(isEmpty()) {
        throw new NoSuchElementException();
    }
    ListNode temp = head;
    if (head == tail) {
        tail = null;
    }
    else {
        head.next.previous = null;

    }
    head = head.next;
    temp.next = null;
    length--;
    return temp;
} 

//Method removing last node
public void removeLast() {
    if(isEmpty()) {
        throw new NoSuchElementException();
    }
    ListNode temp = tail;
    if (head == tail) {
        head = null;
    }
    else {
        tail.previous.next = null;

    }
    tail = tail.previous;
    temp.previous = null;
    length--;    
}

//Method removing middle node
public void removeMiddle() {
    if(isEmpty()) {
        throw new NoSuchElementException();
    }
    ListNode temp = getMiddleNode();
    if (head == tail) {
        head = null;
    }
    else {
        temp.previous.next = temp.next;
        temp.next.previous = temp.previous;

    }
    temp.next = null;
    temp.previous = null;
    length--;    
}

//Method for searching through list
public void searchList(T searchKey) {
    ListNode current = head;
    int count = 1;
    while (current != null) {
        if (current.data == searchKey) {
            System.out.println("This element is in position " + count
            + ".");  
            return;
        }
        else { 
            current = current.next;
            count++;
        }
    }
    System.out.println("This element is not in the list.");
}


}

我的主班

    package assignment_3_1;

import java.util.*;

//Main Class
public class Assignment_3_1 {

//Main Method
public static void main(String[] args) {

    //Create scanner instance
    Scanner input = new Scanner(System.in);

    //Create List
    MyLinkedList nameList = new MyLinkedList();

    //Insert names
    nameList.insertStart("James");
    nameList.insertStart("John");
    nameList.insertEnd("Michael");
    nameList.insertEnd("Peter");
    nameList.insertStart("Allison");
    nameList.insertStart("Daniel");
    nameList.insertMiddle("George");
    nameList.insertMiddle("Simon");
    nameList.insertMiddle("Jason");
    nameList.insertStart("Mark");
            //Console input for searching
    System.out.println("Enter a name to search in the list: ");
    String searchKey1 = input.next();

    //Search through list for key
    nameList.searchList(searchKey1);

    //Display list fowards and backwards
    System.out.println("    Original List    ");
    System.out.println("=====================");
    System.out.println();        
    nameList.displayFowards();
    System.out.println();
    nameList.displayBackwards();
    System.out.println();

    //Remove first name
    nameList.removeFirst();
    nameList.removeFirst();
    //Display list fowards and backwards
    System.out.println("    List after removal of the first two names   ");
    System.out.println("================================================");
    System.out.println();       
    nameList.displayFowards();
    System.out.println();
    nameList.displayBackwards(); 
    System.out.println();

    //Remove Last name
    nameList.removeLast();

    //Display list fowards and backwards
    System.out.println("    List after removal of last name    ");
    System.out.println("=======================================");
    System.out.println();       
    nameList.displayFowards();
    System.out.println();
    nameList.displayBackwards(); 
    System.out.println();

    //Remove middle name
    nameList.removeMiddle();

    //Display list fowards and backwards
    System.out.println("    List after removal of middle name    ");
    System.out.println("=======================================");
    System.out.println();       
    nameList.displayFowards();
    System.out.println();
    nameList.displayBackwards(); 
    System.out.println();

    //Display size of current list
    nameList.listSize();

    //Console input for searching
    System.out.println("Enter a name to search in the list: ");
    String searchKey2 = input.next();

    //Search through list for key
    nameList.searchList(searchKey2);

}


}

0 个答案:

没有答案