如何在Java中为单链表实现按索引删除方法?
我是一名编程课的学生,我需要一些关于我所写代码的帮助。到目前为止,我已经编写了一个完整的链表类(如下所示),但由于某种原因,“removeByIndex”方法无法工作。我似乎不明白为什么,这个逻辑对我来说似乎很合理。有什么我不知道的问题吗如何在Java中为单链表实现按索引删除方法?,java,linked-list,Java,Linked List,我是一名编程课的学生,我需要一些关于我所写代码的帮助。到目前为止,我已经编写了一个完整的链表类(如下所示),但由于某种原因,“removeByIndex”方法无法工作。我似乎不明白为什么,这个逻辑对我来说似乎很合理。有什么我不知道的问题吗 public class List<T> { //private sub-class Link private class Link { private T value; private Link next; //construc
public class List<T> {
//private sub-class Link
private class Link {
private T value;
private Link next;
//constructors of Link:
public Link (T val) {
this.value = val;
this.next = null;
}
public Link (T val, Link next) {
this.value = val;
this.next = next;
}
@SuppressWarnings("unused")
public T getValue() {
return value;
}
}
private static final Exception NoSuchElementException = null;
private static final Exception IndexOutOfBoundsException = null;
private Link chain = null;
//constructors of List:
public List() {
this.chain = null;
}
//methods of List:
/**
* Preconditions: none
* Postconditions: returns true if list is empty
*/
public boolean isEmpty() {
return this.chain == null;
}
/**
* Preconditions: none
* Postconditions: A new Link is added via add-aux
* @param element
*/
public void add(T element) {
this.add_aux(element, this.chain);
}
/**
* Preconditions: none
* Postconditions: A new Link is added to the current chain
* @param element
* @param chain
*/
private void add_aux(T element, Link chain) {
if (chain == null) {
//if chain is null set chain to a new Link with a value of
//element
this.chain = new Link(element);
}
else if (chain.next != null) {
//if chain.next is not null, go to next item in chain and
//try
//to add element
add_aux(element, chain.next);
}
else {
//if chain.next is null, set chain.next equal to a new Link
//with value of element.
chain.next = new Link(element);
}
}
/**
* Preconditions: none
* Postconditions: returns the link at the defined index via nthlink_aux
* @param index
* @return
*/
private Link nthLink (int index) {
return nthLink_aux(index, this.chain);
}
/**
* Preconditions: none
* Postconditions: returns the link at the defined index in the specified
*chain
* @param i
* @param c
* @return
*/
private Link nthLink_aux (int i, Link c) {
if (i == 0) {
return c;
}
else return nthLink_aux(i-1, c.next);
}
/**
* Preconditions: the specified element is present in the list
* Postconditions: the specified element is removed from the list
* @param element
* @throws Exception
*/
public void removeElement(T element) throws Exception {
if (chain == null) {
throw NoSuchElementException;
}
//while chain's next is not null and the value of chain.next is not
//equal to element,
//set chain equal to chain.next
//use this iteration to go through the linked list.
else while ((chain.next != null) && !(chain.next.value.equals(element))){
Link testlink = chain.next;
if (testlink.next.value.equals(element)) {
//if chain.next is equal to element, bypass the
//element.
chain.next.next = chain.next.next.next;
}
else if (testlink.next == null) {
throw NoSuchElementException;
}
}
}
/**
* Preconditions: none
* Postsconditions: the Link at the specified index is removed
* @param index
* @throws Exception
*/
public void removeByIndex(int index) throws Exception {
if (index == 0) {
//if index is 0, set chain equal to chain.next
chain = chain.next;
}
else if (index > 0) {
Link target = nthLink(index);
while (target != null) {
if (target.next != null) {
target = target.next;
}
//if target.next is null, set target to null
else {
target = null;
}
}
return;
}
else throw IndexOutOfBoundsException;
}
/**
* Preconditions: none
* Postconditions: the specified link's value is printed
* @param link
*/
public void printLink (Link link) {
if(link != null) {
System.out.println(link.value.toString());
}
}
/**
* Preconditions: none
* Postconditions: all of the links' values in the list are printed.
*/
public void print() {
//copy chain to a new variable
Link head = this.chain;
//while head is not null
while (!(head == null)) {
//print the current link
this.printLink(head);
//set head equal to the next link
head = head.next;
}
}
/**
* Preconditions: none
* Postconditions: The chain is set to null
*/
public void clear() {
this.chain = null;
}
/**
* Preconditions: none
* Postconditions: Places the defined link at the defined index of the list
* @param index
* @param val
*/
public void splice(int index, T val) {
//create a new link with value equal to val
Link spliced = new Link(val);
if (index <= 0) {
//copy chain
Link copy = chain;
//set chain equal to spliced
chain = spliced;
//set chain.next equal to copy
chain.next = copy;
}
else if (index > 0) {
//create a target link equal to the link before the index
Link target = nthLink(index - 1);
//set the target's next equal to a new link with a next
//equal to the target's old next
target.next = new Link(val, target.next);
}
}
/**
* Preconditions: none
* Postconditions: Check to see if element is in the list, returns true
* if it is and false if it isn't
* @param element
* @return
*/
public boolean Search(T element) {
if (chain == null) {
//return false if chain is null
return false;
}
//while chain's next is not null and the value of chain.next is not
//equal to element,
//set chain equal to chain.next
//use this iteration to go through the linked list.
else while ((chain.next != null) && !(chain.next.value.equals(element))) {
Link testlink = chain.next;
if (testlink.next.value.equals(element)) {
//if chain.next is equal to element, return true
return true;
}
else if (testlink.next == null) {
return false;
}
}
return false;
}
/**
* Preconditions: none
* Postconditions: order of the links in the list is reversed.
*/
public void reverse() {
//copy chain
Link current = chain;
//set chain equal to null
chain = null;
while (current != null) {
Link save = current;
current = current.next;
save.next = chain;
chain = save;
}
}
}'
公共类列表{
//专用子类链接
私有类链接{
私人T值;
私有链接下一步;
//链接的构造函数:
公共链接(T val){
this.value=val;
this.next=null;
}
公共链接(T val,链接下一个){
this.value=val;
this.next=next;
}
@抑制警告(“未使用”)
公共T getValue(){
返回值;
}
}
私有静态最终异常NoSuchElementException=null;
私有静态最终异常IndexOutOfBoundsException=null;
私有链接链=空;
//列表的构造函数:
公开名单(){
this.chain=null;
}
//列表方法:
/**
*先决条件:无
*后置条件:如果列表为空,则返回true
*/
公共布尔值为空(){
返回this.chain==null;
}
/**
*先决条件:无
*后置条件:通过add aux添加新链接
*@param元素
*/
公共无效添加(T元素){
this.add_aux(元素,this.chain);
}
/**
*先决条件:无
*后置条件:将新链接添加到当前链
*@param元素
*@param链
*/
专用void add_aux(T元素,链接链){
如果(链==null){
//如果链为空,则将链设置为值为的新链接
//元素
this.chain=新链接(元素);
}
else if(chain.next!=null){
//如果chain.next不为空,请转到chain中的下一项并
//试一试
//添加元素
添加_aux(元素,链,下一步);
}
否则{
//如果chain.next为空,则将chain.next设置为等于新链接
//具有元素的值。
chain.next=新链接(元素);
}
}
/**
*先决条件:无
*后置条件:通过nthlink\u aux返回定义索引处的链接
*@param索引
*@返回
*/
专用链接(整数索引){
返回nthLink_aux(索引,this.chain);
}
/**
*先决条件:无
*Postconditions:返回指定索引中定义索引处的链接
*链子
*@param i
*@param c
*@返回
*/
专用链路nthLink_辅助(内部i,链路c){
如果(i==0){
返回c;
}
否则,返回第N个链接(i-1,c.next);
}
/**
*前提条件:列表中存在指定的元素
*后置条件:指定的元素将从列表中删除
*@param元素
*@抛出异常
*/
public void removeElement(T元素)引发异常{
如果(链==null){
抛出无接触异常;
}
//而chain的next不为null,chain.next的值不为null
//等于元素,
//将chain设置为chain.next
//使用此迭代遍历链接列表。
else while((chain.next!=null)和&!(chain.next.value.equals(element))){
Link testlink=chain.next;
if(testlink.next.value.equals(元素)){
//如果chain.next等于元素,则绕过
//元素。
chain.next.next=chain.next.next.next;
}
else if(testlink.next==null){
抛出无接触异常;
}
}
}
/**
*先决条件:无
*Postsconditions:删除指定索引处的链接
*@param索引
*@抛出异常
*/
public void removeByIndex(int-index)引发异常{
如果(索引==0){
//如果索引为0,则将chain设置为chain.next
chain=chain.next;
}
否则,如果(索引>0){
链接目标=第n个链接(索引);
while(目标!=null){
if(target.next!=null){
target=target.next;
}
//如果target.next为null,则将target设置为null
否则{
target=null;
}
}
返回;
}
否则抛出IndexOutOfBoundsException;
}
/**
*先决条件:无
*后置条件:打印指定链接的值
*@param-link
*/
公共无效打印链接(链接){
如果(链接!=null){
System.out.println(link.value.toString());
}
}
/**
*先决条件:无
*后置条件:打印列表中所有链接的值。
*/
公开作废印刷品(){
//将链复制到新变量
链节头=此链;
//而head不是空的
而(!(head==null)){
//打印当前链接
这是printLink(head);
//将头设置为与下一个链接相等
head=head.next;
}
}
/**
*先决条件:无
*后置条件:链设置为空
*/
公共空间清除(){
this.chain=null;
}
/**
*先决条件:无
*后置条件:将已定义的链接放置在列表的已定义索引处
*@param索引
*@param val
*/
公共无效拼接(int索引,T val){
//创建一个值等于val的新链接
连接拼接=新连接(val);
如果(索引0){
//创建与索引之前的链接相等的目标链接
链接目标=第n个链接(索引-1);
//将目标的next设置为等于具有next的新链接
//等于目标的旧next
target.next=新链接(val,target.next);
}
}
/**
*先决条件:无
*后置条件:检查元素是否在列表中,返回true
*如果是,如果不是,则为假
*@param元素
*@返回
*/
公共布尔搜索(T元素){
如果(链==null){
//如果链为null,则返回false
返回false;
}
//而chain的next不为null,chain.next的值不为null
//等于元素,
//将chain设置为chain.next
//使用此迭代遍历链接列表。
else while((chain.next!=null)和&!(chain.next.value.equals(element))){
Link testlink=chain.next;
if(testlink.next.value.equals(元素)){
//如果chain.next等于element,则返回true
返回true;
}
else if(testlink.n
if (index == 0) {
//if index is 0, set chain equal to chain.next
chain = chain.next;
}
//while head is not null
while (!(head == null)) {
Link target = nthLink(index - 1);
if (target.next != null)
target.next = target.next.next;
public void removeByIndex(int index) throws Exception {
if (index == 0) {
//if index is 0, set chain equal to chain.next
chain = chain.next;
}
else if (index > 0 && index < size()) {
Link priorToRemove = nthLink_aux(index - 1);
priorToRemove.next = priorToRemove.next.next;
}
else {
throw IndexOutOfBoundsException;
}
}
public void removeByIndex(int index) throws Exception {
if (index == 0) {
//if index is 0, set chain equal to chain.next
chain = chain.next;
}
else if (index > 0) {
Link target = nthLink(index - 1);
target.next = target.next.next;
}
return;
}
Link target = nthLink(index);
while (target != null) {
if (target.next != null) {
target = target.next;
}
//if target.next is null, set target to null
else {
target = null;
}