데이터 구조 과정 설계양 방향 순환 링크

4800 단어 데이터 구조
package chCircleTwoLinkedList;

import java.util.Scanner;

public class CircleTwoLinkedList{
	private int theSize=0;
	private Node beginMarker;
	private Node endMarker;
	static Scanner cin=new Scanner(System.in);
	public CircleTwoLinkedList(){
		beginMarker=new Node();
		endMarker=new Node();
		beginMarker.prev=endMarker;
		beginMarker.next=endMarker;
		endMarker.prev=beginMarker;
		endMarker.next=beginMarker;
		theSize=0;
	}
	public void setSize(int theSize){
		this.theSize=theSize;
	}
	public int getSize(){
		return theSize;
	}
	public boolean isEmpty(){
		if(theSize==0)
			return true;
		else
			return false;
	}
	public AnyType getData(int idx){
		return getNode(idx).data;
	}
	public Node getNode(int idx){
		Node p=new Node();
		if(idx<0||idx>getSize()){
			throw new IndexOutOfBoundsException("getNode idx:"+idx+";size:"+getSize());
		}
		if(idxidx;i--){
				p=p.prev;
			}
		}
		return p;
	}
	public boolean add(AnyType x){
		add(theSize,x);
		return true;
	}
	public void add(int idx,AnyType x){
		Node p=new Node(x);
		if(idx==0){
			/*p.next=beginMarker.next;
			beginMarker.prev.next=p;
			p.prev=beginMarker=endMarker;
			endMarker.prev=p;*/
			p.next=beginMarker.next;
    		beginMarker.next.prev=p;
    		beginMarker.next=p;
    		p.prev=beginMarker;
		}
		else if(idx==theSize){
			/*p.prev=endMarker.prev;
			endMarker.prev=p;
			p.next=endMarker;*/
			p.prev=endMarker.prev;
    		endMarker.prev.next=p;
    		endMarker.prev=p;
    		p.next=endMarker;
		}
		else{
			/*Node Q;
			Q=getNode(idx-1);
			Q.next=p;
			p.prev=Q;
			p.next=getNode(idx);
			getNode(idx).prev=p;*/
			Node q;
    		q=getNode(idx-1);
    		p.next=q.next;
    		q.next.prev=p;
    		q.next=p;
    		p.prev=q;
		}
		theSize++;
	}
	public boolean remove(int idx){
		Node p=new Node();
		if(idx<0||idx>getSize())
			return false;
		else{
			p=getNode(idx);//         p, p    ;
			p.prev.next=p.next;
			p.next.prev=p.prev;
			theSize--;
			return false;
		}
	}
	public CircleTwoLinkedList reverseLinkedList(CircleTwoLinkedList p){
		Node q;
		for(int i=0;i p){
		beginMarker.prev=p.next;
		beginMarker.prev.next=p.prev;
		theSize++;
	}
	public void reverse( CircleTwoLinkedList p){
		CircleTwoLinkedList q=p;
		System.out.println("       :");
		reverseLinkedList(q);
		System.out.println();
		
	}
	public void menu(CircleTwoLinkedList p){
		System.out.println("1:      ;");
		System.out.println("2:   i   ;");
		System.out.println("3:   i   ;");
		System.out.println("4:   1   ;");
		System.out.println("5:        ;");
		System.out.println("6:  ;");
		System.out.print("   :");
		Scanner cin=new Scanner(System.in);
		int a=cin.nextInt();	
		switch(a){
			case 1:JianLiNull();menu(p);break;
			case 2:addI(p);menu(p);break;
			case 3:removeI(p);menu(p);break;
			case 4:addFirst(p);menu(p);break;
			case 5:addLast(p);menu(p);break;
			case 6:reverse(p);menu(p);break;
			default:break;
		}
	}
	public static void main(String[] args) {
		CircleTwoLinkedList cl_0=new CircleTwoLinkedList();
		System.out.print("        :");
		int i=cin.nextInt();
		System.out.println("        :");
		for(int j=0;j p=new Node();
		p=cl_0.beginMarker;
		System.out.println("     :");
		for(int j=0;j {

	public AnyType data;
	public Node prev;
	public Node next;
	public Node(){
		data=null;
		prev=null;
		next=null;
	}
	public Node(AnyType d){
		data=d;
		prev=next=null;
	}
	public Node(AnyType d,Node p,Node n){
		data=d;
		prev=p;
		next=n;
	}
}

좋은 웹페이지 즐겨찾기