Queue-linked list queue

Source: Internet
Author: User

Queues represented by linked lists are more efficient in insertion and deletion.

/** Node class used by linked structures. <br/> * this class and its data members are <br/> * visible only within the package datastructures. */</P> <p> package datastructures; </P> <p> class chainnode <br/> {<br/> // package visible data members <br/> object element; <br/> chainnode next; </P> <p> // package visible constructors <br/> chainnode () {}</P> <p> chainnode (object element) <br/> {This. element = element ;}</P> <p> chainnode (object element, chainnode next) <br/> {This. element = element; <br/> This. next = next ;}< br/>}< br/>

 

/** A linked queue class */</P> <p> package datastructures; </P> <p> public class incluqueue implements queue <br/> {<br/> // data members <br/> protected chainnode front; <br/> protected chainnode rear; </P> <p> // constructors <br/>/** create an empty queue */<br/> Public incluqueue (INT initialcapacity) <br/>{< br/> // The default initial value of front is null <br/>}</P> <p> Public writable Queue () <B R/> {This (0 );} </P> <p> // methods <br/>/** @ return true iff queue is empty */<br/> Public Boolean isempty () <br/> {return front = NULL ;} </P> <p>/** @ return the element at the front of the queue <br/> * @ return NULL if the queue is empty */<br/> Public object getfrontelement () <br/>{< br/> If (isempty () <br/> return NULL; <br/> else <br/> return front. element; <br/>}</P> <p>/** @ return the E LEMENT at the rear of the queue <br/> * @ return NULL if the queue is empty */<br/> Public object getrearelement () <br/>{< br/> If (isempty () <br/> return NULL; <br/> else <br/> return rear. element; <br/>}</P> <p>/** insert theelement at the rear of the queue */<br/> Public void put (Object theelement) <br/>{< br/> // create a node for theelement <br/> chainnode P = new chainnode (theelement, n Ull); </P> <p> // append P to the chain <br/> If (front = NULL) <br/> front = P; // empty queue <br/> else <br/> rear. next = P; // nonempty queue <br/> rear = P; <br/>}</P> <p>/** remove an element from the front of the queue <br/> * @ return removed element <br/> * @ return null if the queue is empty */<br/> Public object remove () <br/>{< br/> If (isempty () <br/> return NULL; <br/> Object fronte LEMENT = front. element; <br/> front = front. next; <br/> If (isempty () <br/> rear = NULL; // enable garbage collection <br/> return frontelement; <br/>}</P> <p>/** Test Program */<br/> Public static void main (string [] ARGs) <br/>{< br/> int X; <br/> queue q = new queue Queue (3 ); <br/> // Add a few elements <br/> q. put (New INTEGER (1); <br/> q. put (New INTEGER (2); <br/> q. put (New INTEGER (3); <br/> q. Put (New INTEGER (4); </P> <p> // delete all elements <br/> while (! Q. isempty () <br/>{< br/> system. out. println ("rear element is" + q. getrearelement (); <br/> system. out. println ("front element is" + q. getfrontelement (); <br/> system. out. println ("removed the element" + q. remove (); <br/>}< br/>

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.