In the JAVA language, write a linked list class (bidirectional linked list), implement INSERT, Delete, find operation __java

Source: Internet
Author: User
Tags int size
Define interface://deque.java package DSA; Depending on the location of your program public interface Deque {public int getsize ();//Returns the number of elements in the queue public Boolean isempty ();//Determine if the queue is empty public Obj  ECT first () throws exceptionqueueempty;//(but not deleted) the public Object, last () throws exceptionqueueempty;//the end element (but not deleted) public  void Insertfirst (object obj); Inserts the new element as the first element into the public void Insertlast (object obj);//Inserts the new element as the last element into the public Object Removefirst () Throws exceptionqueueempty;//Delete the first element public Object Removelast () throws exceptionqueueempty;//Delete the end element public void traversal (

)//traversal} bidirectional linked list implementation://deque_dlnode.java * * Double-end queue structure based on bidirectional link list * * Package DSA; public class Deque_dlnode implements Deque {protected Dlnode header;//pointing head node (Sentry) protected Dlnode pointing Tail node (Sentinel) p
		rotected int size;//The number of elements in the queue//constructor public Deque_dlnode () {header = new Dlnode ();
		Trailer = new Dlnode ();
		Header.setnext (trailer);
		Trailer.setprev (header);
	size = 0;
	
//Returns the number of elements in the queue public int getsize () {return size;} Determines whether the queue is empty public Boolean isempty () { Return (0 = size)? True:false; ///Take the first element (but not delete) public Object A () throws Exceptionqueueempty {if (IsEmpty ()) throw new Exceptionqueueempty ("unexpected
		: Two-end queues are empty ");
	Return Header.getnext (). Getelem (); }//Fetch the end element (but not delete) public Object last () throws Exceptionqueueempty {if (IsEmpty ()) throw new Exceptionqueueempty ("Unexpected:
		Two-terminal queue is empty ");
	Return Trailer.getprev (). Getelem ();
		///Insert new node in front of queue public void Insertfirst (Object obj) {Dlnode second = Header.getnext ();
		Dlnode-New Dlnode (obj, header, second);
		Second.setprev (a);
		Header.setnext (a); 
	size++;
		/////Insert new node at backend of queue public void Insertlast (Object obj) {Dlnode second = Trailer.getprev ();
		Dlnode-New Dlnode (obj, second, trailer);
		Second.setnext (a);
		Trailer.setprev (a); 
	size++; //Delete the first node public Object Removefirst () throws Exceptionqueueempty {if (IsEmpty ()) throw new Exceptionqueueempty ("unexpected
		: Two-end queues are empty ");
		Dlnode-Header.getnext ();
		Dlnode second = First.getnext ();	Objectobj = First.getelem ();
		Header.setnext (second);
		Second.setprev (header);
		size--;
	return (obj); ///Remove the distal point public Object removelast () throws Exceptionqueueempty {if (IsEmpty ()) throw new Exceptionqueueempty ("Unexpected:
		Two-terminal queue is empty ");
		Dlnode-Trailer.getprev ();
		Dlnode second = First.getprev ();
		Object obj = First.getelem ();
		Trailer.setprev (second);
		Second.setnext (trailer);
		size--;
	return (obj);
		}//Traverse public void Traversal () {Dlnode p = header.getnext ();
			while (P!= trailer) {System.out.print (P.getelem () + "");
		p = P.getnext ();
	} System.out.println (); }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.