Java data structure (stack, queue, double-stranded table)

Source: Internet
Author: User

 
(1) Stack
Package chapterone; public class Stack {// Stack Array long stackarr []; // stack size int maxsize; // The top int top of the stack; // initialize a public stack (INT size) {maxsize = size; stackarr = new long [size]; Top =-1 ;} // public long POP () {return stackarr [top --];} // public void push (long value) {stackarr [++ top] = value;} // determines whether the stack is empty. Public Boolean isempty () {return Top =-1 ;} // determine whether the stack is full public Boolean isfull () {Return Top = maxSize-1;} // get the top element of the stack public long PEEK () {return stackarr [Top];} public static void main (string [] ARGs) {stack = new stack (10); While (! Stack. isfull () {long v = (long) (math. random () * 100); stack. push (V); system. out. print (V + "");} system. out. println (); While (! Stack. isempty () {long topvalue = stack. Pop (); system. Out. Print (topvalue + "") ;}system. Out. println ();}}

(2) queue

Package chapterone; public class queue {// queue array private long queuearr []; // the front-end subscript of the queue private int front; // The tail subscript of the queue private int rear; // queue size private int maxsize; // number of elements in the queue private int nitems; // initialize a queue with the size public Queue (INT size) {queuearr = new long [size]; maxsize = size; front = 0; Rear =-1; nitems = 0 ;}// insert public void insert (long value) {// The queue is full if (Rear = maxSize-1) rear =-1; queuearr [++ Rear] = value; nitems ++;} // Delete public long remove () {long temp = queuearr [Front ++]; If (front = maxsize) front = 0; nitems --; return temp;} // return the first element of the queue public long peakfront () {return queuearr [Front];} // judge whether it is empty public Boolean isempty () {return nitems = 0;} // determine whether it is full public Boolean isfull () {return nitems = maxsize ;} // return the number of elements in the queue. Public int size () {return nitems;} public void print () {( Int I = front; I <front + nitems; I ++) {system. out. print (queuearr [I] + "");} system. out. println ();} public static void main (string [] ARGs) {queue q = new Queue (10); While (! Q. isfull () {long value = (long) (math. Random () * 100); q. insert (value) ;}q. Print (); While (! Q. isempty () {q. remove (); q. print ();} Q. print (); system. out. println (Q. isempty ());}}
(3) priority queue
package ChapterOne;    public class PriorityQueue {        private int nItems;            private long pqArr[];            private int maxSize;            public PriorityQueue(int size){          maxSize = size;          pqArr = new long[size];          nItems = 0;      }            public void insert(long value){          int i;          if(nItems == 0)              pqArr[nItems++] = value;          else{              for(i = nItems-1;i >= 0;i--){                  if(value < pqArr[i]){                      pqArr[i+1] = pqArr[i];                  }                  else                      break;              }              pqArr[i+1] = value;              nItems++;          }      }            public long remove(){          return pqArr[--nItems];      }            public boolean isEmpty(){          return nItems == 0;      }            public boolean isFull(){          return nItems == maxSize;      }            public void print(){          for(int i = 0;i < nItems;i++)              System.out.print(pqArr[i]+" ");          System.out.println();      }            public static void main(String[] args) {          PriorityQueue pq = new PriorityQueue(10);          while(!pq.isFull()){              long value = (long)(Math.random()*100);              pq.insert(value);          }          pq.print();      }  }
(4) double-stranded table
Class Chain {chain pre = NULL, next = NULL; int ID; string name;} class list {private chain header = new chain (); Public chain add (int id, string name) {// Add the node chain current = new chain () at the end of the linked list; // create the Chain List header chain temp = header; while (temp. next! = NULL) // loop to the end of the linked list temp = temp. next; temp. next = current; current. pre = temp; current. id = ID; current. name = Name; Return Current;} public chain remove (int id) {// Delete the node chain temp = header with the specified ID; chain current = NULL; while (temp. next! = NULL) {temp = temp. next; If (temp. id = ID) {current = temp; break;} If (current = NULL) return NULL; current. pre. next = current. next; If (current. next! = NULL) Current. next. pre = current. pre; Return Current;} public chain remove (string name) {// Delete the node chain temp = header with the specified name; chain current = NULL; while (temp. next! = NULL) {temp = temp. next; If (temp. name = Name) {current = temp; break;} If (current = NULL) return NULL; current. pre. next = current. next; If (current. next! = NULL) Current. next. pre = current. pre; Return Current;} public chain remove () {// Delete the last node chain temp = header; while (temp. next. next! = NULL) {temp = temp. next;} temp. next = NULL; return temp;} public void clear () {// Delete All node headers. next = NULL;} public chain insert (int id, string name, int POS) {// insert node chain temp = header at the specified position; chain current = new chain (); int I = 0; for (I = 0; I <= Pos; I ++) {If (temp. next! = NULL) {temp = temp. Next;} else {return NULL;} current. ID = ID; current. Name = Name; If (temp. Next! = NULL) {temp. next. pre = current; current. next = temp. next;} temp. next = current; current. pre = temp; Return Current;} public void print_all () {chain temp = header; system. out. println ("------------------------------------"); While (temp. next! = NULL) {temp = temp. next; system. out. println ("ID:" + temp. ID); system. out. println ("name:" + temp. name);} system. out. println ("------------------------------------") ;}} public class chainlist {public static void main (string [] ARGs) {List A = new list ();. add (1, "Tan Meng Jun 1");. add (2, "Tan Meng Jun 2");. add (3, "Tan mengshu 3");. add (4, "Tan Meng Jun 4");. insert (12, "fat", 2);. print_all ();}}

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.