Java: Implementing Stacks and queues based on LinkedList

Source: Internet
Author: User

1. Provides a set of stacks of interfaces that are associated with a linkedlist (two-terminal queue) instance. It can provide a secure stack implementation because it exposes only some of the interfaces that are based on the stack implementation.

Import java.util.LinkedList;

public class Stack<T> {
  private LinkedList<T> storage = new LinkedList<T>();
   /** 入栈 */
  public void push(T v) {
    storage.addFirst(v);
  }
   /** 出栈,但不删除 */
  public T peek() {
    return storage.getFirst();
  }
   /** 出栈 */
  public T pop() {
    return storage.removeFirst();
  }
   /** 栈是否为空 */
  public boolean empty() {
    return storage.isEmpty();
  }
   /** 打印栈元素 */
  public String toString() {
    return storage.toString();
  }
}

2. Using the Java.util.Queue interface, the bottom layer is associated with a linkedlist (two-terminal queue) instance. A secure queue implementation can be provided by exposing only some of the queued implementation-based interfaces.

Import java.util.LinkedList;

Import java.util.Queue
public class Myqueue<t> {
Private queue<t> storage = new LinkedList <T> ();   
/** inserts the specified element into the team tail */
public void offer (T v) {
Storage.offer (v);
  
/** retrieves, but does not remove the header of the queue, and returns NULL */
Public T Peek () {
return Storage.peek () If this queue is empty;

/** retrieves, but does not remove the header */
/** This method is the only difference from the Peek method is that if the queue is empty, it throws an exception */
Public T Element () {
Return St Orage.element ();   
}
/** retrieves and removes the header of this queue and returns NULL */
Public T poll () {
return Storage.poll () if the queue is empty;

/** Retrieves and removes the header */
of this queue/** this method differs from the poll method in that if the queue is empty, it throws an exception */
Public T Remove () {
Return storage . remove ();   
}
/** queue is empty */
Public boolean empty () {
return storage.isempty ();
  
/** Print Queue element */
Public String toString () {
return storage.tostring ();
}
}

This article is from the "Zi Postman" blog, please be sure to keep this source http://zhangjunhd.blog.51cto.com/113473/69912

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.