LinkedList Matters needing attention

Source: Internet
Author: User
Tags prev static class

The LinkedList bottom is realized by bidirectional linked list, which can be used to realize stack, queue, bidirectional queue

1. Structure of each node
private Static Class Node {
E item;
Node Next;
Node prev;

    Node (node<e> prev, E element, node<e> next) {
        This.item = element;
        This.next = Next;
        This.prev = prev;
    }

-Note that we can add null to the LinkedList, when only the item equals NULL, and Next and Prev are still non-empty and do not affect the use

The 2.LinkedList class maintains the node of the both-A and the last, easy to operate
-first's prev is Null,last's next is null

3. With the one and last we can easily implement the stack, queue, two-way queue function
Add: public void AddFirst (e e);
public void AddLast (e e);
Delete: Public E removefirst ();
Public E removelast ();
Query: Public E peekfirst ();
Public E peeklast ();
4. Implement stack operation:
public void push (E e) {
AddFirst (e);
}
Public E pop () {
return Removefirst ();
}
Public E Peek () {
Final Node f = A;
return (f = null)? Null:f.item;
}
Note: size () will change after each pop, so you cannot traverse for (int i=0;i

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.