Home IT Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Java implementation of stacks and queues     - Unetbootin make use U disk loading Linux system (Linux)

- To install Scribus 1.4.4 under ubuntu (Linux)

- About enhanced Linux / Unix server system security program (Linux)

- Spring AOP (Programming)

- Linux, see picture not resolve the problem (Linux)

- Android project using the command to create and install the package (Programming)

- Ubuntu system grub repair method (Linux)

- Linux ban single-user mode to enhance system security (Linux)

- Share Java-based multithreading file case (Programming)

- An example of troubleshooting of embedded Linux OpenWRT (Linux)

- CentOS7 install NTFS-3G driver (Linux)

- How to set IonCube Loaders in Ubuntu (Linux)

- Ubuntu configuration SVN and http mode access (Server)

- Linux Network Programming - raw socket instance: MAC Address Scanner (Programming)

- Using Python and OpenCV detecting image barcode (Programming)

- Ora-1092: OPI colleague K aborting process --- killed by OO Well killer (Database)

- Oracle 11g on Linux system boot from the startup settings (Database)

- Ubuntu 12.04 commonly use shortcuts finishing Share (Linux)

- Oracle Execute to Parse perform analytical Ratio Analysis (Database)

- Why you can have JavaScript string method (Programming)

 
         
  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:      
 
- Understanding the type in C ++ bitset (Programming)
- How to install Eclipse Luna IDE on CentOS 7 / RHEL 7 (Linux)
- Git you do not know about some of the things (Linux)
- Ubuntu installed Komodo editor by PPA (Linux)
- Node.js v4.0.0 installation configuration on Ubuntu 14.04 / 15.04 (Linux)
- Linux CPU Monitoring Index (Linux)
- MySQL binlog automatic cleanup script (Database)
- CentOS7 yum install third-party source EPEL (Linux)
- How to manage KVM virtual environments with command-line tools in Linux (Server)
- Bash Automated Customization Linux belongs to its own CentOS system (Linux)
- Java string intern constant pool resolution Introduction (Programming)
- Arduino UNO simulation development environment set up and run simulation (Linux)
- Python 3.5 await / async (Programming)
- Ubuntu Server 14.04 installation Web server (Linux + Apache + MySQL + PHP) (Server)
- Python system default encoding (Programming)
- Ubuntu install image browser and manager Phototonic 1.6.17 (Linux)
- Wireless LAN security solutions (Linux)
- CentOS system Amoeba + MySQL Master-slave configuration (Database)
- Change CentOS 7 NIC name eno16777736 to eth0 (Linux)
- Use GNU / Linux broadcasting of television programs (Linux)
     
           
     
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.