java linkedlist查找方法

时间:2017-03-09 01:04:31

标签: java doubly-linked-list

我在Java实验室任务中遇到了困难。

以下是我的代码和说明:

如果钥匙不在那里,它应该    返回PREVIOUS节点。如果没有先前(key == Aaron),    return null。

 protected Node<K, V> find (K key, Node start) {
  for (Node<K, V> node = start == null ? head : (Node<K, V>) start;
  node != null; node = node.next){
    if(key.equals(node.key)){
        return node;
    }
    else if(!key.equals(node.key)){
        node=node.next;
    }
    else{
        return null;
    }
  }
  return tail;
}

C E G I K M O Q S U W Y

2 4 6 8 10 12 14 16 18 20 22 24

这是链接列表。

我的输出是 C E G I K M O Q S U W Y. 2 4 6 8 10 12 14 16 18 20 22 24

containsKey(A)= false

containsKey(C)= true

containsKey(L)= false

containsKey(M)= false

containsKey(Y)= true

containsKey(Z)= false

但是对于M,它也应该返回true 我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

您可以使用双链接列表或(这意味着您必须更改节点类)来解决此问题 另一个是当指向头部而另一个指向头部后面时必须节点,

myApp.controller('othersObjectiveListController', ['$scope', '$uibModal', 'EmployeeObjectiveService', '$routeParams', function ($scope, $uibModal, EmployeeObjectiveService, $routeParams) {

var items = [];
for (i = 0; i < 100; i++) {
    items[i] = i;
}
$scope.test = items;



$scope.StatusBy = [{ id: 1, value: true, label: 'Approve' }, { id: 2, value: false, label: 'Pending' }];


//Angular Code for Multiple column show and hide

$scope.dropConfig = {
    scrollable: true,
    scrollableHeight: '340px',
    showCheckAll: false,
    showUncheckAll: false
}

EmployeeObjectiveService.getOtherObjectiveColumnList().then(function (response) { $scope.AllColumn = response.data });
EmployeeObjectiveService.getOtherSelectedColumn().then(function (response) { $scope.SelectedColumn = response.data });
EmployeeObjectiveService.getOtherObjectiveSelected().then(function (response) { $scope.SelectCol = response.data });


function changeColumnViewShow(item) {
    if (item.id == 1) {
        $scope.SelectCol.EmployeeID = !$scope.SelectCol.EmployeeID;
    } else if (item.id == 2) {
        $scope.SelectCol.EmployeeName = !$scope.SelectCol.EmployeeName;
    } else if (item.id == 3) {
        $scope.SelectCol.Code = !$scope.SelectCol.Code;
    } else if (item.id == 4) {
        $scope.SelectCol.Title = !$scope.SelectCol.Title;
    } else if (item.id == 5) {
        $scope.SelectCol.KPI = !$scope.SelectCol.KPI;
    } else if (item.id == 6) {
        $scope.SelectCol.Target = !$scope.SelectCol.Target;
    } else if (item.id == 7) {
        $scope.SelectCol.Weight = !$scope.SelectCol.Weight;
    } else if (item.id == 8) {
        $scope.SelectCol.Note = !$scope.SelectCol.Note;
    } else if (item.id == 9) {
        $scope.SelectCol.Status = !$scope.SelectCol.Status;
    }
}

$scope.changeEvents = {
    onItemSelect: function (item) {
        changeColumnViewShow(item);
    },
    onItemDeselect: function (item) {
        changeColumnViewShow(item);
    }
};
//End Column hide show function


// This Section for Pagination for Pending List
$scope.ViewItems = [{ value: 10, id: 10 }, { value: 20, id: 20 }, { value: 50, id: 50 }, { value: 100, id: 100 }];
$scope.selectItem = $scope.ViewItems[0];
$scope.ViewPerPage = 10;
$scope.setitemsPerPage = function (num) {
    $scope.ViewPerPage = num.value;
}

//This Section for Modal
$scope.viewObjectives = function (data) {
    var modalInstance = $uibModal.open({
        templateUrl: '/View/Modal View/OtherObjective.html',
        scope: $scope,
        size: 'lg',
    });
}

function ViewObjective($uibModalInstance, code) {
    $scope.id = code;
}

$scope.initial = function () {
    var id = $routeParams.id;
    if(id!=null)
    $scope.viewObjectives(id);
}

$scope.initial();


//PDF Create

$scope.CreatePDF = function () {
    $('#objectivelist').tableExport({
        type: 'pdf',
        fileName: 'ObjectiveList',
        jspdf: {
            orientation: 'l',
            format: 'a4',
            margins: { left: 10, right: 10, top: 20, bottom: 20 },
            autotable: {
                styles: {
                    fontSize: 10,
                    fillColor: 'inherit',
                    textColor: 'inherit'
                },
                tableWidth: 'auto'
            }
        }
    });
}

每次循环 你会做的

node p = null ; 
node q = head ;

所以,这样,如果你找不到钥匙,你将返回p 如果你找到它,你将返回q。