双向循环链表(插入,删除,就地逆置)

package design;

import java.util.Scanner;

//循环双链表
public class LinkedList<AnyType>  {
    private int theSize;
    private Node<AnyType> beginMarker;
    private Node<AnyType> endMarker;
    
	    class Node<AnyType>{          //定义匿名类Node
		public AnyType data;
		public Node<AnyType> prev;
		public Node<AnyType> next;
		public Node(AnyType d,Node<AnyType> p,Node<AnyType> n){
			this.data=d;
			this.prev=p;
			this.next=n;
		}
		
	}
	
	public LinkedList(){                         //构造方法
		beginMarker=new Node<AnyType>(null,endMarker,endMarker);
		endMarker=new Node<AnyType>(null,beginMarker,beginMarker);
		beginMarker.next=endMarker;
		theSize=0;
	}
	public int size(){                     
		return theSize;
	}
	public Node<AnyType> getNode(int idx){             //返回idx对应的结点
		Node<AnyType> p=null;
		if(idx<0||idx>size())
            System.out.println("无法找到该点");
		
		if(idx<size()/2){
			p=beginMarker.next;
			for(int i=0;i<idx;i++)
				p=p.next;
		}
		else{
			p=endMarker;
			for(int i=size();i>idx;i--)
				p=p.prev;
		}
	
		 return p;	    
	}
	
	public boolean add(AnyType x){     //插入元素
		add(size(),x);
		return true;
	}
	private void add(int idx,AnyType x){
		if(idx>size()||idx<0){
			System.out.println("输入错误");
		}else{
		Node<AnyType> p;
		p=getNode(idx);
		Node<AnyType> newNode=new Node<AnyType>(x,p.prev,p);
		newNode.prev.next=newNode;
		p.prev=newNode;
		theSize++;
		}
	}
	public AnyType remove(int idx){              //删除元素
		if(idx>size()||idx<0){
			System.out.println("无法找到该节点");
			return null;
		}else
		return remove(getNode(idx));
	}
	public AnyType remove(Node<AnyType> p){
		p.next.prev=p.prev;
		p.prev.next=p.next;
		theSize--;
		return p.data;
	}
	public void addFirst(AnyType data){
		add(0,data);
	}
	public void addLast(AnyType data){
		add(size(),data);
	}
	public void trs(){
	     Node p=beginMarker;
	     while(p.next!=beginMarker){
	    	 Node q=p;
	    	 Node r=p.next;
	    	 q.next=q.prev;
	    	 q.prev=r;
	    	 p=p.prev;
	     }
	     Node q=p;
	     Node r=p.next;
	     q.next=q.prev;
	     q.prev=r;
	     Node s=beginMarker;
	     beginMarker=endMarker;
	     endMarker=s;
	     
	     int i=0;
	     for(Node x=beginMarker.next;x!=endMarker;x=x.next){
	    	 System.out.println(i+": "+x.data);
	    	 i++;
	     }
	     i=0;
	}
	public void print(){
		int i=0;
		for(Node x=beginMarker.next;x.next!=beginMarker;x=x.next){
			System.out.println(i+": "+x.data);
			i++;
		}
		i=0;
	}
	public static void main(String[] args) {
	      //验证部分
		LinkedList<String> La=new LinkedList<String>();
		System.out.println("1:创建链表");
		System.out.println("2:添加结点");
		System.out.println("3:删除结点");
		System.out.println("4:添加第一个");
		System.out.println("5:添加最后一个");
		System.out.println("6 :转置");
		System.out.println("请输入要进行的操作");
		Scanner sc=new Scanner(System.in);
		boolean flag=true;
		while(flag){
			  System.out.println("请输入您的要进行的操作:");
			int c=sc.nextInt();
			switch(c){
			   case 1: System.out.println("请输入数据个数");
			           int n=sc.nextInt();
			           System.out.println("输入数据");
			           for(int i=0;i<n;i++){
			        	   String data=sc.next();
			        	   La.add(data);
			           }
			           La.print();
			           break;
			   case 2: System.out.println("输入数据的插入位置和数据");
			           int w=sc.nextInt();
			           String data=sc.next();
			           La.add(w,data);
			           La.print();
			           break;
			   case 3: System.out.println("输入要删除结点的位置");
			           int q=sc.nextInt();
			           La.remove(q);
			           La.print();break;
			   case 4: System.out.println("输入要添加的数据");
			           String d=sc.next();
			           La.addFirst(d);
			           La.print();break;
			   case 5: System.out.println("输入要添加的数据");
	                   String da=sc.next();
	                   La.addLast(da); 
	                   La.print();break;
			   case 6: La.trs();
				       break;
			   default :System.out.println("您输错了,请重新输入");break;
			}	
		}
		
	}

}

版权声明:本文为博主原创文章,未经博主允许不得转载。

posted @ 2014-01-02 20:13  dingxiaoyue  阅读(958)  评论(0编辑  收藏  举报