Home IT Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Java implementation of stacks and queues     - Linux environment RabbitMQ installation and monitoring of plug-in installation (Linux)

- Linux System Getting Started Tutorial: Installing Git on Linux (Linux)

- Help you make Git Bisect (Linux)

- Android working with Volley Comments (Programming)

- OpenJDK7 source compiler installation on CentOS 6.5 (Linux)

- Linux Variable content removal and replacement (Linux)

- Troubleshooting Linux virtual machine device id and the letter of inconsistencies (Linux)

- Installation and use of Linux Sniffer tool Tcpdump (Linux)

- Spring WebSocket Comments (Programming)

- Android in the coordinate system and a method to obtain the coordinates (Programming)

- Java implementation file encryption and decryption (Programming)

- tar command to extract a file error: stdin has more than one entry (Linux)

- Acting on JavaScript events (Programming)

- Linux usage in echo (Linux)

- Openfire Hazelcast cluster Detailed (Server)

- Using PPA to install the lightweight theme software HotShots 2.1.0 under Ubuntu (Linux)

- Python virtual environment: Virtualenv (Linux)

- With screenfetch linux logo and basic hardware information display with cool Linux logo (Linux)

- Shell Scripting Basics (Linux)

- Linux Nginx FastDFS integration module is installed Nginx and FastDFS (Server)

 
         
  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:      
 
- How to make Linux a non-root user uses less than 1024 ports (Linux)
- Install JDK 1.7 + Eclipse in CentOS 6.4 in (Linux)
- Java memory area (Programming)
- Ubuntu download install and configure Plank Dock (Linux)
- Linux script to copy the folder to all folders with the same name (Linux)
- Installation on the way to the root source Ubuntu Server 14.04 LTS version Odoo8.0 (Server)
- Difference Docker mirror and containers (Server)
- How to add any of a Linux distribution beautiful fonts (Linux)
- Related to optimize the use of Btrfs file system on SSD (Linux)
- Ubuntu FAQ solutions (Linux)
- Python Django direct implementation of sql statement (Programming)
- Monitoring network traffic with Iptraf in Linux environment (Linux)
- Ubuntu 15.04 / CentOS 7.0 to set custom boot (Linux)
- Prevent security threats caused Rootkit (Linux)
- KVM virtualization of nested virtualization (Linux)
- Storm how to assign tasks and load balancing (Programming)
- Ubuntu 14.04 compile, install, configure, the latest development version GoldenDict (Linux)
- Elementary OS Freya global menu (Linux)
- Upgrading Oracle 11.2.0.1.0 to 11.2.0.3.0 (Database)
- Mac OS X systems create Ubuntu USB boot disk for the Mac (Linux)
     
           
     
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.