为LinkListWithSize类增加:
1、实现就地逆置函数:
public void reverse()
2、public void removeRange(int from, int to)
删除位置[from, to]范围的数据
3、public int lastInsdex(AnyType x)
返回x最后一次出现的位置
public interface IList {
//public void clear();
//public boolean isEmpty();
//public int length();
//public Object get(int i);
public void insert(int i,Object x)throws Exception;
public void reverse();
public void removeRange(int from,int to)throws Exception;
//public void remove(int i);
//public int indexOf(Object x);
public void display();
}
/*
2:线性表的链式实现
为LinkListWithSize类增加:
1、实现就地逆置函数:
public void reverse()
2、public void removeRange(int from, int to)
删除位置[from, to]范围的数据
3、public int lastInsdex(AnyType x)
返回x最后一次出现的位置
*/
import java.util.Scanner;
public class LinkList implements IList{
public Node head;
public LinkList(){
head=new Node();
}
public void create(int n)throws Exception {
Scanner sc=new Scanner(System.in);
for(int j=0;j<n;j++){
insert(length(),sc.next());
}
}
public void insert(int i,Object x) throws Exception{
Node p=head;
int j=-1;
while (p != null && j < i - 1) {
p=p.next;
++j;
}
if(j>i-1 || p==null){
throw new Exception("插入位置不合法");
}
Node s=new Node(x);
s.next=p.next;
p.next=s;
}
public void display(){
Node node=head.next;
while (node!=null){
System.out.print(node.data+" ");
node=node.next;
}
System.out.println();
}
public int length(){
Node p=head.next;
int length=0;
while (p!=null){
p=p.next;
++length;
}
return length;
}
public void reverse(){
if(head.next==null||head.next.next==null)
return;
Node p,q;
p=head.next;
head.next=null;
while(p!=null){
q=p.next;
p.next=head.next;
head.next=p;
p=q;
}
}
public void removeRange(int from,int to)throws Exception{
Node p=head;
Node q=head;
int j=-1;
int k=-1;
while(p.next!=null&&j<from-1){
p=p.next;
++j;
}
while(q.next!=null&&k<to-1){
q=q.next;
++k;
}
if(j>from||k>to-1||p.next==null||q.next==null||from>to)
throw new Exception("删除位置不合法");
p.next=q.next.next;
}
public int lastInsdex(Object x){
Node p=head;
int i=0;
int j=-1;
while (p.next!=null){
if (x.equals(p.data))
i=j;
p=p.next;
j++;
}
return i;
}
/*
public int lastInsdex(Object x){
Node p=head.next;
int i=0;
int j=-1;
while (p!=null){
if (x.equals(p.data))
i=j;
p=p.next;
j++;
}
return i;
}
*/
}
public class Node{
public Object data;
public Node next;
public Node(){
this(null,null);
}
public Node (Object data){
this(data,null);
}
public Node(Object data,Node next){
this.data=data;
this.next=next;
}
}
import java.util.Scanner;
public class Test {
public static void main(String[] args){
LinkList l1=new LinkList();
System.out.println("结点个数");
Scanner sc=new Scanner(System.in);
int num=sc.nextInt();
System.out.println("输入链表中的各元素值");
try {
l1.create(num);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("链表中的各元素值为");
l1.display();
System.out.println("就地逆置后链表各元素为");
l1.reverse();
l1.display();
System.out.println("输入删除区间起始位置from和to");
Scanner scanner1=new Scanner(System.in);
Scanner scanner2=new Scanner(System.in);
int from=scanner1.nextInt();
int to=scanner2.nextInt();
try {
l1.removeRange(from,to);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("删除from到to后各元素为");
l1.display();
System.out.println("输入需要查找的元素");
Scanner scanner3=new Scanner(System.in);
Object x=scanner3.next();
int n=l1.lastInsdex(x);
System.out.println("该元素在链表第"+n+"个位置上");
}
}