Java chain list Implementation of algorithm _ stack and queue

Source: Internet
Author: User
Tags iterable

A linked list is a recursive data structure that is either null or a reference to a node that contains a generic element and a reference to another linked list. You can use an inner class to define the abstract data type of a node:

Private class /* Defining node Classes */ {        item item;        Node next;    }

According to the recursive definition, we only need a variable of node type to represent a linked list, as long as the value is guaranteed to be null or point to another node object, and the next field of the object points to another linked list. The list represents a list of elements, although you can also use arrays to represent a column of elements. However, it is convenient to insert elements into the list or remove elements from the sequence. In addition, each action that modifies a linked list needs to add code that checks whether the variable is being modified.

For the traversal of a linked list element, you can use the following methods:

 for (Node x=first;x!=null; x=x.next) {        // process X.item      }

For implementing queues and stacks, this iterative approach is the most basic way to implement iterators. Implementing queues and stacks by maintaining a linked list internally, the time required to achieve the operation is always independent of the size of the collection. The following is the implementation code:

ImportJava.util.Iterator;//the stack implemented by the linked list Public classStack<item>ImplementsIterable<item>{    PrivateNode first;//defining the top of the stack    Private intN//define the number of elements    Private classNode {Item item;    Node Next; }         Public BooleanIsEmpty () {returnfirst==NULL; }     Public intsize () {returnN; }     Public voidpush (item item) {//adding elements to the top of the stackNode oldfirst=First ; First=NewNode (); First.item=item; First.next=Oldfirst; N++; }     PublicItem Pop () {//eject element from top of stackItem item=First.item; First=First.next; N--; returnitem; } @Override PublicIterator<item>iterator () {return NewStackiterator (); }    Private classStackiteratorImplementsIterator<item> {                PrivateNode current=First ; @Override Public BooleanHasnext () {returncurrent!=NULL; } @Override PublicItem Next () {Item Item=Current.item; Current=Current.next; returnitem; }        }}
//List -implemented queuesImportJava.util.Iterator; Public classQueue<item>ImplementsIterable<item> {    PrivateNode first;//Defining a table header    PrivateNode last;//Define footer    Private intN//define the number of elements    Private classNode/*Defining node Classes*/{item item;    Node Next; }     Public BooleanIsEmpty () {returnfirst==NULL;}  Public intSize () {returnN;}  Public voidEnquene (item item) {//add an element to the end of a tableNode oldlast=Last ; Last=NewNode (); Last.item=item; Last.next=NULL; if(IsEmpty ()) First=last;//If the list is empty before adding, the table header and footer point to the same element        ElseOldlast.next=last;//points the next element of the previous trailing element to the new trailing elementn++;//increase the number of elements    }     PublicItem dequeue () {Item Item=First.item; First=First.next; if(IsEmpty ()) last=NULL;//If the list becomes empty, then the trailing element is set to nulln--;//reduce the number of elements.        returnitem; } @Override PublicIterator<item>iterator () {return NewQueueiterator (); }Private classQueueiteratorImplementsIterator<item>/*is exactly the same as the implementation of the stack iterator*/{                PrivateNode current=First ; @Override Public BooleanHasnext () {returncurrent!=NULL; } @Override PublicItem Next () {Item Item=Current.item; Current=Current.next; returnitem; }        }}

Java chain list Implementation of algorithm _ stack and queue

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.