Home IT Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Java implementation of stacks and queues     - Android official recommendation: DialogFragment create dialog (Programming)

- Matters Oracle 11.2 single instance when connecting ASM need to pay attention and deal with the problem (Database)

- Oracle database, some basic grammatical structures (Database)

- Linux + Apache + PHP + Oracle based environment to build (Server)

- Linux Routine Task Scheduler (Linux)

- Linux Getting Started tutorial: Ubuntu 14.04 in the installation Sogou Pinyin (Linux)

- Getting CentOS Learning Notes (Linux)

- Fedora 22 installation and configuration optimization (Linux)

- Linux text processing tool of sed (Linux)

- Configure shared library PCRE (Linux)

- Linux character device - automatically creates the device nodes and devices (Linux)

- IBM Data Studio to use ---- window displays all rows (Database)

- How to use Git to upload code to GitHub project (Linux)

- Present Situation and Development Trend of firewall products (Linux)

- Ubuntu ADSL dial-up Internet access (Linux)

- Depth understanding of DB2 table space (Tablespace) (Database)

- Use Oracle 11g show spparameter command (Database)

- CentOS6.5 installation Docker (Linux)

- Linux process management related content (Linux)

- To see the Linux device tree (Linux)

 
         
  Java implementation of stacks and queues
     
  Add Date : 2017-08-31      
         
       
         
  Stack: LIFO (Last In First Out)

Queue: FIFO (First In First Out)

Sequential storage structure stack implementation:

/ **
 * Order of the array-based implementation Stack
 * @param < E>
 * /
public class Stack < E> {
    private Object [] data = null;
    private int maxSize = 0; // stack capacity
    private int top = -1; // stack pointer
    
    / **
    * Constructor: initialize the given stack size
    * /
    Stack () {
        this (10); // default stack size is 10
    }
    
    Stack (int initialSize) {
        if (initialSize> = 0) {
            this.maxSize = initialSize;
            data = new Object [initialSize];
            top = -1;
        } Else {
            throw new RuntimeException ( "the initial size of not less than 0:" + initialSize);
        }
    }
    
    // Empty sentence
    public boolean empty () {
        ? Return top == - 1 true: false;
    }
    
    // Into the stack, the first element of top = 0;
    public boolean push (E e) {
        if (top == maxSize -1) {
            throw new RuntimeException ( "stack is full, you can not stack the elements!");
        } Else {
            data [++ top] = e;
            return true;
        }
    }
    
    // Check the top element but does not remove
    public E peek () {
        if (top == -1) {
            throw new RuntimeException ( "stack is empty!");
        } Else {
            return (E) data [top];
        }
    }
    
    // Pop the top element
    public E pop () {
        if (top == -1) {
            throw new RuntimeException ( "stack is empty!");
        } Else {
            return (E) data [top--];
        }
    }
    
    // Return the object position in the stack, with 1 as the base
    public int search (E e) {
        int i = top;
        while (top! = -1) {
            if (peek ()! = e) {
                top -;
            } Else {
                break;
            }
        }
        int result = top + 1;
        top = i;
        return result;
    }
}

Storage Structure stack implementation:

public class LinkStack < E> {
    // Link stack node
    private class Node < E> {
        E e;
        Node < E> next;
        
        public Node () {}
        public Node (E e, Node next) {
            this.e = e;
            this.next = next;
        }
    }
    
    private Node < E> top; // the top element
    private int size; // current stack size
    
    public LinkStack () {
        top = null;
    }
    
    // Current stack size
    public int length () {
        return size;
    }
    
    // Empty sentence
    public boolean empty () {
        return size == 0;
    }
    
    // Stack: Let the top point to the newly created element, next element of the new reference point to the original top element
    public boolean push (E e) {
        top = new Node (e, top);
        size ++;
        return true;
    }
    
    // Check the top element but not deleted
    public Node < E> peek () {
        if (empty ()) {
            throw new RuntimeException ( "empty stack exception!");
        } Else {
            return top;
        }
    }
    
    // Stack
    public Node < E> pop () {
        if (empty ()) {
            throw new RuntimeException ( "empty stack exception!");
        } Else {
            Node < E> value = top; // get the top element
            top = top.next; // make reference to the next element points to top the top element of the original
            value.next = null; // the top element of the next release of the original reference
            size -;
            return value;
        }
    }
}

Based LinkedList implement stack structure:

import java.util.LinkedList;

/ **
 * Based on LinkedList implement stack
 * In the LinkedList strength to select only part of the interface stack-based implementation
 * /
public class StackList < E> {
    private LinkedList < E> ll = new LinkedList < E> ();
    
    // Stack
    public void push (E e) {
        ll.addFirst (e);
    }
    
    // Check the top element but does not remove
    public E peek () {
        return ll.getFirst ();
    }
    
    // Stack
    public E pop () {
        return ll.removeFirst ();
    }
    
    // Empty sentence
    public boolean empty () {
        return ll.isEmpty ();
    }
    
    // Print stack elements
    public String toString () {
        return ll.toString ();
    }
}

Sequential storage structure queue implementation

public class Queue < E> {
    private Object [] data = null;
    private int maxSize; // queue capacity
    private int front; // head of the queue, allowing deleted
    private int rear; // end of the queue, allowing the insertion

    //Constructor
    public Queue () {
        this (10);
    }
    
    public Queue (int initialSize) {
        if (initialSize> = 0) {
            this.maxSize = initialSize;
            data = new Object [initialSize];
            front = rear = 0;
        } Else {
            throw new RuntimeException ( "the initial size of not less than 0:" + initialSize);
        }
    }
    
    // Empty sentence
    public boolean empty () {
        return rear == front true:? false;
    }
    
    //insert
    public boolean add (E e) {
        if (rear == maxSize) {
            throw new RuntimeException ( "queue is full, you can not insert a new element!");
        } Else {
            data [rear ++] = e;
            return true;
        }
    }
    
    // Returns the first element of the team, but does not delete
    public E peek () {
        if (empty ()) {
            throw new RuntimeException ( "empty queue exception!");
        } Else {
            return (E) data [front];
        }
    }
    
    // Dequeue
    public E poll () {
        if (empty ()) {
            throw new RuntimeException ( "empty queue exception!");
        } Else {
            E value = (E) data [front]; // value retention queue front end of the element
            data [front ++] = null; // release the front end of the queue element
            return value;
        }
    }
    
    // Queue length
    public int length () {
        return rear-front;
    }
}

Sequential storage structure circular queue implementations

import java.util.Arrays;

public class LoopQueue < E> {
    public Object [] data = null;
    private int maxSize; // queue capacity
    private int rear; // end of the queue, allowing the insertion
    private int front; // head of the queue, allowing deleted
    private int size = 0; // current length of the queue

    public LoopQueue () {
        this (10);
    }

    public LoopQueue (int initialSize) {
        if (initialSize> = 0) {
            this.maxSize = initialSize;
            data = new Object [initialSize];
            front = rear = 0;
        } Else {
            throw new RuntimeException ( "the initial size of not less than 0:" + initialSize);
        }
    }

    // Empty sentence
    public boolean empty () {
        return size == 0;
    }

    // Insert
    public boolean add (E e) {
        if (size == maxSize) {
            throw new RuntimeException ( "queue is full, you can not insert a new element!");
        } Else {
            data [rear] = e;
            rear = (rear + 1)% maxSize;
            size ++;
            return true;
        }
    }

    // Returns the first element of the team, but does not delete
    public E peek () {
        if (empty ()) {
            throw new RuntimeException ( "empty queue exception!");
        } Else {
            return (E) data [front];
        }
    }

    // Dequeue
    public E poll () {
        if (empty ()) {
            throw new RuntimeException ( "empty queue exception!");
        } Else {
            E value = (E) data [front]; // value retention queue front end of the element
            data [front] = null; // release the front end of the queue element
            front = (front + 1)% maxSize; // pointer to the first team plus 1
            size--;
            return value;
        }
    }

    // Queue length
    public int length () {
        return size;
    }

    // Empty circular queue
    public void clear () {
        Arrays.fill (data, null);
        size = 0;
        front = 0;
        rear = 0;
    }
}

Storage Structure queue implementation

public class LinkQueue < E> {
    // Link stack node
    private class Node < E> {
        E e;
        Node < E> next;

        public Node () {
        }

        public Node (E e, Node next) {
            this.e = e;
            this.next = next;
        }
    }
    
    private Node front; // head of the queue, allowing deleted
    private Node rear; // end of the queue, allowing the insertion
    // Current length of the queue; private int size
    
    public LinkQueue () {
        front = null;
        rear = null;
    }
    
    // Empty sentence
      public boolean empty () {
          return size == 0;
      }
      
      //insert
      public boolean add (E e) {
          if (empty ()) {// if the queue is empty
              front = new Node (e, null); // only one node, front, rear point of the node
              rear = front;
          } Else {
              Node < E> newNode = new Node < E> (e, null);
              rear.next = newNode; // let the tail node of next point to the new node
              rear = newNode; // new node as a new tail node
          }
          size ++;
          return true;
      }
      
      // Returns the first element of the team, but does not delete
      public Node < E> peek () {
          if (empty ()) {
              throw new RuntimeException ( "empty queue exception!");
          } Else {
              return front;
          }
      }
      
      // Dequeue
      public Node < E> poll () {
          if (empty ()) {
              throw new RuntimeException ( "empty queue exception!");
          } Else {
              Node < E> value = front; // get the head of the queue element
              front = front.next; // let the front reference point to the next element of the original head of the queue elements
              value.next = null; // release the former head of the queue elements next reference
              size -;
              return value;
          }
      }
      
      // Queue length
      public int length () {
          return size;
      }
}

Based LinkedList queue structure

/ **
 * Use java.util.Queue interface associated with the underlying to a LinkedList (deque) instance.
 * /
import java.util.LinkedList;
import java.util.Queue;

public class QueueList < E> {
    private Queue < E> queue = new LinkedList < E> ();
    
    // The specified element into this queue (if feasible immediately without violating capacity restrictions), returning on success true,
    // If no space is available, throw IllegalStateException.
    public boolean add (E e) {
        return queue.add (e);
    }
    
    // Get, but do not remove the head of the queue.
    public E element () {
        return queue.element ();
    }
    
    // The specified element into this queue (if feasible immediately without violating capacity restrictions), when using a capacity-restricted queue,
    // This method is generally preferable to add (E), which can fail to insert an element only by throwing an exception.
    public boolean offer (E e) {
        return queue.offer (e);
    }
    
    // Retrieves, but does not remove, the head of this queue; if this queue is empty, it returns null
    public E peek () {
        return queue.peek ();
    }
    
    // Retrieves and removes the head of this queue, if the queue is empty or null
    public E poll () {
        return queue.poll ();
    }
    
    // Retrieves and removes the head of this queue
    public E remove () {
        return queue.remove ();
    }
    
    // Empty sentence
    public boolean empty () {
        return queue.isEmpty ();
    }
}
     
         
       
         
  More:      
 
- Detailed Linux network security policies and protection measures (Linux)
- How to Disable Linux IPv6 (Linux)
- Linux kernel source tree to establish load module hello (Linux)
- CentOS permanently banned from running in the background PackageKit (Linux)
- Household use Linux Security (Linux)
- Improve the efficiency of Linux development tools 5 (Linux)
- JDK tools jstat (Linux)
- Shell scripts get a snapshot of the page and generates thumbnails (Linux)
- Linux System Administrator common interview questions and answers 30 (Linux)
- Extended use of the swap file swap space on Linux (Linux)
- Java rewrite equals method (Programming)
- CentOS yum source configuration (Linux)
- Some practical tips Linux (Linux)
- Oracle SDE and maintain common commands - Display space (Database)
- Linux Basics Tutorial: Linux Kickstart automated installation (Linux)
- CentOS 5.5 install ntop (Linux)
- Linux Detailed instructions alias settings (Linux)
- Linux Troubleshooting: How to save the status of the SSH session is closed (Linux)
- Linux batch copy data script (Linux)
- Android system source code and compile the kernel source code (Programming)
     
           
     
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.