1、 建立一个空表。
2、 插入第i个结点。
3、 删除第i个结点。
4、 插入第1个结点。
5、 插入最后一个结点。
6、 就地逆置
public class TwoWayLinkedListNode<AnyType> {
AnyType data;
TwoWayLinkedListNode<AnyType> pre;
TwoWayLinkedListNode<AnyType> next;
public TwoWayLinkedListNode(AnyType data,TwoWayLinkedListNode<AnyType> pre,TwoWayLinkedListNode<AnyType> next){
this.data=data;
this.pre=pre;
this.next=next;
}
}
import java.util.Scanner;
public class TwoWayLinkedList <AnyType>{
public TwoWayLinkedListNode<AnyType> firstNode;
public TwoWayLinkedList(){
firstNode=null;
}
public boolean insertINode(int i,AnyType data){
if(i==1){
insertFirstNode(data);
return true;
}
else {
TwoWayLinkedListNode<AnyType> p=firstNode;
int j=0;
for(j=0;j<i-1&&p!=null;j++){
p=p.next;
}
if(j==i-1){
TwoWayLinkedListNode<AnyType> q=p.pre;
TwoWayLinkedListNode<AnyType> tmp=new TwoWayLinkedListNode(data,null,null);
q.next=tmp;
tmp.pre=q;
tmp.next=p;
p.pre=tmp;
return true;
}
else {
System.out.println("插入超过范围");
return false;
}
}
}
public boolean deleteINode(int i){
if(i==1){
TwoWayLinkedListNode<AnyType> p=firstNode;
p.pre.next=p.next;
p.next.pre=p.pre;
firstNode=p.next;
return true;
}
else {
TwoWayLinkedListNode<AnyType> p=firstNode;
int j=0;
for(j=0;j<i-1&&p!=null;j++){
p=p.next;
}
if(j==i-1){
TwoWayLinkedListNode<AnyType> q=p.pre;
q.next=p.next;
p.next.pre=q;
return true;
}
else {
System.out.println("删除的元素不存在");
return false;
}
}
}
public void insertFirstNode(AnyType data){
TwoWayLinkedListNode<AnyType> p=firstNode;
TwoWayLinkedListNode<AnyType> tmp=new TwoWayLinkedListNode<AnyType>(data,p.pre,p);
p.pre.next=tmp;
p.pre=tmp;
firstNode=tmp;
}
public void insertLastNode(AnyType data){
TwoWayLinkedListNode<AnyType> p=firstNode;
TwoWayLinkedListNode<AnyType> q=p.pre;
TwoWayLinkedListNode<AnyType> tmp=new TwoWayLinkedListNode(data,q,p);
q.next=tmp;
p.pre=tmp;
}
public void add(AnyType data){
TwoWayLinkedListNode<AnyType> p=firstNode;
if(p==null){
TwoWayLinkedListNode<AnyType> tmp=new TwoWayLinkedListNode<AnyType>(data,null,null);
tmp.pre=tmp;
tmp.next=tmp;
firstNode=tmp;
}
else{
insertLastNode(data);
}
}
public void reverse(){
TwoWayLinkedListNode<AnyType> p=firstNode;
TwoWayLinkedListNode<AnyType> q=p.pre;
TwoWayLinkedList<AnyType> first=new TwoWayLinkedList();
while(q!=firstNode){
first.add(q.data);
q=q.pre;
}
first.add(q.data);
this.firstNode=first.firstNode;
}
public void print(){
TwoWayLinkedListNode<AnyType> p=firstNode;
System.out.print(p.data+" ");
p=p.next;
while(p!=firstNode){
System.out.print(p.data+" ");
p=p.next;
}
System.out.println();
}
public static void main(String args[]){
TwoWayLinkedList<Integer> my=new TwoWayLinkedList();
int len,a;
System.out.println("请输入链表的长度:");
Scanner cin=new Scanner(System.in);
len=cin.nextInt();
System.out.println("请输入链表的各个点:");
for(int i=0;i<len;i++){
a=cin.nextInt();
my.add(a);
}
int pos,val;
System.out.println("请输入要插入的点的位置和值:");
pos=cin.nextInt();
val=cin.nextInt();
my.insertINode(pos+1,val);
System.out.println("链表的顺序是:");
my.print();
System.out.println("请输入要删除的位置:");
pos=cin.nextInt();
System.out.println("链表的顺序是:");
my.deleteINode(pos+1);
my.print();
System.out.println("请输入要插入第一个顶点的值:");
val=cin.nextInt();
my.insertFirstNode(val);
System.out.println("链表的顺序是:");
my.print();
System.out.println("请输入要插入最后一个顶点的值:");
val=cin.nextInt();
my.insertLastNode(val);
System.out.println("链表的顺序是:");
my.print();
my.reverse();
System.out.println("就地逆转之后的链表的顺序是:");
my.print();
}
}
package GraphDemo;
import java.util.Scanner;
public class TwoWayLinkedList <AnyType>{
private int theSize;
public TwoWayLinkedListNode<AnyType> firstNode;
public TwoWayLinkedListNode<AnyType> endNode;
public TwoWayLinkedList(){
firstNode=new TwoWayLinkedListNode<AnyType>(null,endNode,endNode);
endNode=new TwoWayLinkedListNode<AnyType>(null,firstNode,firstNode);
theSize=0;
}
public int size(){
return theSize;
}
public TwoWayLinkedListNode<AnyType> getNode(int idx){ //返回idx对应的结点
TwoWayLinkedListNode<AnyType> p;
if(idx<0||idx>size())
throw new IndexOutOfBoundsException( );
if(idx<size()/2){
p=firstNode.next;
for(int i=0;i<idx;i++)
p=p.next;
}
else{
p=endNode;
for(int i=size();i>idx;i--)
p=p.pre;
}
return p;
}
public AnyType deleteNode(int idx){ //删除元素
return deleteNode(getNode(idx));
}
public AnyType deleteNode(TwoWayLinkedListNode<AnyType> p){
p.next.pre=p.pre;
p.pre.next=p.next;
theSize--;
return p.data;
}
public void insertFirstNode(AnyType data){
add(0,data);
}
public void insertLastNode(AnyType data){
add(size(),data);
}
public boolean add(AnyType data){
add(size(),data);
return true;
}
public void add(int idx,AnyType x){
TwoWayLinkedListNode<AnyType> p;
p=getNode(idx);
TwoWayLinkedListNode<AnyType>newNode=new TwoWayLinkedListNode<AnyType>(x,p.pre,p);
newNode.pre.next=newNode;
p.pre=newNode;
theSize++;
}
public void reverse(){
TwoWayLinkedList<AnyType> newList=new TwoWayLinkedList();
AnyType s[]=(AnyType[])new Object[size()];
int num=size();
System.out.print(num);
for(int i=0;i<num;i++){
s[i]=deleteNode(0);
}
for(int i=num-1;i>=0;i--)
add(s[i]);
}
public void print(){
int i=0;
for(TwoWayLinkedListNode x=firstNode.next;x.next!=firstNode;x=x.next){
System.out.print(x.data+" ");
i++;
}
i=0;
System.out.println();
}
public static void main(String args[]){
TwoWayLinkedList<Integer> my=new TwoWayLinkedList();
int a;
System.out.println("请输入链表的长度:");
Scanner cin=new Scanner(System.in);
int len=cin.nextInt();
System.out.println("请输入链表的各个点:");
for(int i=0;i<len;i++){
a=cin.nextInt();
my.add(a);
}
int pos,val;
System.out.println("请输入要插入的点的位置和值:");
pos=cin.nextInt();
val=cin.nextInt();
my.add(pos,val);
System.out.println("链表的顺序是:");
my.print();
System.out.println("请输入要删除的位置:");
pos=cin.nextInt();
System.out.println("链表的顺序是:");
my.deleteNode(pos+1);
my.print();
System.out.println("请输入要插入第一个顶点的值:");
val=cin.nextInt();
my.insertFirstNode(val);
System.out.println("链表的顺序是:");
my.print();
System.out.println("请输入要插入最后一个顶点的值:");
val=cin.nextInt();
my.insertLastNode(val);
System.out.println("链表的顺序是:");
my.print();
my.reverse();
System.out.println("就地逆转之后的链表的顺序是:");
my.print();
}
}