链表,可以进行首尾添加元素,删除

package none005链表;
//链表,可以进行首尾添加元素,删除
public class FirLasLinkApp {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		FirstLastList ff=new FirstLastList();
		ff.insertFirst(22);
		ff.insertFirst(44);
		ff.insertFirst(66);
		ff.insertLast(11);
		ff.insertLast(33);
		ff.insertLast(55);
		ff.displayList();
		System.out.println();
		ff.deleteFirst();
		ff.deleteFirst();
		ff.displayList();
	}

}
class LinkX{
	public long dData;
	public LinkX next;
	public LinkX(long d){
		dData=d;
	}
	public void displayLink(){
		System.out.print(dData+" ");
	}
	
}
class FirstLastList{
	private LinkX first;
	private LinkX last;
	public FirstLastList(){
		first=null;
		last=null;
	}
	public void insertFirst(long value){
		LinkX newLink=new LinkX(value);
		if(last==null)
			last=newLink;
		newLink.next=first;
		first=newLink;
	}
	public void insertLast(long value){
		LinkX newLink=new LinkX(value);
		if(first==null)
			first=newLink;
		last.next=newLink;
		last=newLink;
	}
	public LinkX deleteFirst(){
		if(first==null){
			return null;
		}
		LinkX deleteLink=first;
		first=first.next;
		return deleteLink;
	}
	public void displayList(){
		LinkX current=first;
		while(current!=null){
			current.displayLink();
			current=current.next;
		}
			
	}
}

你可能感兴趣的:(数据结构及算法)