Algorithm series (vi) Table of data structures queue and stack

Source: Internet
Author: User
Tags concurrentmodificationexception

in the http://blog.csdn.net/robertcpp/article/details/51559333 article, we talked about sorting, this chapter introduces basic data structures: tables, queues, stacks, and their simple implementationsTable ADT1, array implementation Order tableIt is more appropriate to choose this kind of implementation by using an array operation to directly add or remove the table to the tables, and this linear table takes an O (1) time to find the element in a location, but the time to insert the deleted element is O (n), and if the operation on the table is more of an access operation. here is a simple implementation
Package Com.algorithm.list;import java.util.iterator;import java.util.nosuchelementexception;/** * Linear array * * @author Chao * * @param <E> */public class Myarraylist<e> implements iterable<e> {private static final int Defau lt_capacity = 10;private int thesize;private e[] theitems;public myarraylist () {clear ();} public void Clear () {thesize = 0;ensurecapacity (default_capacity);} public void ensurecapacity (int newcapacity) {if (Newcapacity < thesize) {return;} e[] Oldes = Theitems;theitems = (e[]) new object[newcapacity];for (int i = 0; i < thesize; i++) {theitems[i] = Oldes[i] ;}} public int size () {return thesize;} public void TrimToSize () {ensurecapacity (size ());} public Boolean isEmpty () {return thesize = = 0;} Public E get (int index) {if (Index < 0 | | index > thesize) {throw new ArrayIndexOutOfBoundsException ();} return Theitems[index];} Public E Set (int index, E newval) {if (Index < 0 | | Index >= thesize) {throw new ArrayIndexOutOfBoundsException ();} E OldvallUE = Theitems[index];theitems[index] = Newval;return oldvallue;} @Overridepublic iterator<e> Iterator () {return new Arraylistiterator ();} Public boolean Add (E value) {Add (Size (), value), return true; public void Add (int index, E value) {if (theitems.length = = thesize) {ensurecapacity (thesize * 2 + 1);} for (int i = thesize; i > Index; i--) {theitems[i] = theitems[i-1];} Theitems[index] = value;thesize++;} Public E Remove (int index) {E oldvalue = theitems[index];for (int i = index; i < thesize; i++) {Theitems[i] = theitems[ i + 1];} Thesize--;return oldvalue;} Class Arraylistiterator<e> implements iterator<e> {private int current = 0; @Overridepublic boolean hasnext () {return current < thesize;} @Overridepublic E Next () {if (!hasnext ()) {throw new Nosuchelementexception ();} Return (E) theitems[current++];} @Overridepublic void Remove () {MyArrayList.this.remove (--current);}}}
2. Linked listA linked list is a data structure that inserts the time complexity of the delete operation as O (1), but the time complexity of the access operation is O (n) Here's our simple implementation
Package Com.algorithm.list;import Java.util.concurrentmodificationexception;import Java.util.iterator;import java.util.nosuchelementexception;/** * Linked list data structure * * @author Chao * * @param <E> */public class mylinkedlist<e> Implements Iterable<e> {Private class Node<t> {public Node () {}public node (T ele, node Next, node Pre) {THIS.E Le = Ele;this.next = Next;this.pre = pre;} T Ele; Node Next; Node Pre;} Private node<e> head;private node<e> tail;private int size;private int modcount = 0;public MyLinkedList () {CLE AR ();} public void Clear () {head = new node<e> (null, NULL, NULL); tail = new node<e> (null, head, NULL); Head.next = Tai L;size = 0;modcount++;} @Overridepublic iterator<e> Iterator () {return new Linkedlistiterator ();} Private class Linkedlistiterator implements iterator<e> {private node<e> current = Head.next;private Boolean Oktoremove = false;private int expectedmodcount = Modcount; @Overridepublic boolean hasnext () {return CurreNT! = Tail;} @Overridepublic E Next () {if (Modcount! = Expectedmodcount) {throw new Concurrentmodificationexception ();} else if (!hasne XT ()) {throw new Nosuchelementexception ();} Current = Current.next;oktoremove = True;return (E) Current.pre.ele;} @Overridepublic void Remove () {if (!oktoremove) {throw new IllegalStateException ();} else if (modcount! = Expectedmodcount {throw new Concurrentmodificationexception ();} MyLinkedList.this.remove (current.pre); oktoremove = false;expectedmodcount++;}} public Boolean IsEmpty () {return size = = 0;} public int size () {return size;} Private Node getnode (int index) {if (Index < 0 | | Index >= size) {throw new Indexoutofboundsexception ();} Node<e> p = null;if (Index < SIZE/2) {p = head.next;for (int i = 0; i < index; i++) p = p.next;} else {p = ta il;for (int i = size; i > Index; i--) p = p.pre;} return p;} Public E get (int index) {return (E) getnode (index). Ele;} Private E Remove (node node) {Node.next.pre = Node.pre;node.pre.next = Node.next;Size--;modcount++;return (E) Node.ele;} Public E Remove (int index) {return remove (GetNode (index));} public void Add (int index, E ele) {Addbefore (GetNode (index), ele);} private void Addbefore (node node, E ele) {node NewNode = new Node<e> (ele, Node.pre, node); node.pre.next = Newnode;no De.pre = newnode;size++;modcount++;} public void Add (E ele) {Add (size, ele);}}

third, queue data structurea queue is a FIFO of first-out data structuresThere are two implementations, one is a list implementation, and the other is an array implementation. The same as the implementation of the table just now, only on the basis of the table to impose restrictions,only new elements can be added at the end of a table at a time, and access elements can only be accessed from the table header.
threads in the thread pool are queued, message queues are in the message pool, and the actual application typically adds priority to the elements in the queue, which are called priority queues and are analyzed separately later.
IV. data structure of stacksStack is a last-in-first-out data structure LIFOlike queues, stacks are implemented in two ways, with arrays implemented with linked lists. On the basis of the table, the limit is added,you can only add elements at the end of a table at a time, and access elements only from the end of the table

Stack is an important concept in computer terminology, in essence, the stack is a memory area, but the stack satisfies certain characteristics, that is, there is only one port, has the characteristics of first-in and out, this feature in the computer has a wide range of applications. In fact, the programmer is using the stack all the while, the function call is our best example of using the stack indirectly, so we can say that one of the most important uses of the stack is the function call. But the use of the stack is more than these, there are many, some of the typical operation is as follows: to judge the balance symbol, to achieve the evaluation of the expression (that is, infix expression to the suffix expression problem and the suffix expression evaluation problem), in the road to explore the implementation of road strength can also be described as one of the classic stack.
two implementations of queues and stacks refer to my GitHub address https://github.com/robertjc/simplealgorithm

GitHub code is also constantly improving, some places may have problems, but also please more advice
Welcome to scan QR Code, follow public account


Algorithm series (vi) Table of data structures queue and stack

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.