Stack java Implementation, java Implementation

Source: Internet
Author: User

Stack java Implementation, java Implementation

In the past few days, I have had a very full life. Every day I keep attending classes. I got up early in the morning and went to the library to read books at night. While studying hard, I am quietly preparing for the soft exam. Recently, I also took over the construction of a company's official website. This is my last full semester at Sichuan mail. I have to do everything seriously. Even if there is loss, there will be failures, there will be unwilling, and do not give up.

Just a few laps, blowing the cold wind at night, and running on the playground. The sweat drenched my body and dripped on the lens, so I could not see the distance. I think of a lot of things. In the past two years, I participated in two sports meetings. This was the third time, and every time it was a long-distance race, I enjoyed this feeling of surpassing myself. The long-distance race is far from over. I have been on this road.

For the schedule, I focused on five questions in the afternoon, algorithm data structure, design mode, data flow diagram, database design, uml example diagram, and project design. After several days of hard work, I used java to implement several common data structures and drew a lot of images, which were amazing. The next step is to design algorithms and then design patterns. I learned about computer basics and network knowledge for two years at the high school. I am going to do exercises and Baidu exercises over and over again. After completing the afternoon review, you will enter the comprehensive review stage. Through real question training, read books, do questions, read code, and write code repeatedly. I hope that through this review, I will be able to absorb this knowledge and convert it into my own ideas, instead of memorizing it and coping with the exam.

Below are the stack's sequential Storage Structure and chain storage implementation:

  

1 package stack; 2 3 public interface IStack <E> {4 // 1. judge empty stack 5 public boolean isEmpty (); 6 7 // 2. judge that the stack is full 8 public boolean isMax (); 9 10 // 3. 11 public boolean push (E e); 12 13 // 4. outbound stack 14 public E pop (); 15 16 // 5. return stack top 17 public E peek (); 18 19 // 6. the position of the returned element in the stack is 20 public int getIndex (E e); 21 22 // 7. returns the actual stack length 23 public int size (); 24 25 // 8. return stack capacity 26 public int getStackSize (); 27 28 // 9. print stack 29 public void display (); 30 31 32 33}

// Ordered Stack

1 package stack; 2 3 // my stack data structure 4 public class MyStack <E> implements IStack <E> {5 private Object [] data = null; // data domain 6 private int top =-1; // The top pointer of the stack is initialized to-1 7 private int maxSize = 0; // maximum stack capacity 8 9 // The default stack capacity is 10 10 MyStack () {11 this (10); 12} 13 14 public MyStack (int initialSize) {15 if (initialSize> = 0) {16 this. data = new Object [initialSize]; // initializes the array 17 this. maxSize = initialSize; // set the maximum stack capacity to 18 this. to P =-1; 19} 20} 21 22 public boolean isEmpty () {23 return top =-1? True: false; // Based on the stack top value, if the stack top pointer is not updated, it is empty stack 24} 25 26 public boolean isMax () {27 return top> = maxSize-1? True: false; // Based on the stack top value, if the stack top pointer is greater than the maximum capacity, it is full stack 28} 29 30 public boolean push (E e) {31 if (isMax () {32 System. err. println ("sorry, the stack is full and cannot be written into the stack"); 33 return false; 34} 35 top ++; // update the bottom of the stack to 36 data [top] = e; // Add the element to table 37 // System. out. println ("added" + e + "succeeded"); 38 return true; 39} 40 41 @ SuppressWarnings ("unchecked") 42 public E pop () {43 if (isEmpty () {44 System. err. println ("Sorry, it is currently an empty stack and no elements can be pushed out of the stack"); 45 return Null; 46} 47 E e = (E) data [top]; // return the top 48 elements of the current stack --; // update the stack top 49 return e; 50} 51 52 @ SuppressWarnings ("unchecked") 53 public E peek () {54 if (isEmpty () {55 System. err. println ("sorry, the current stack is empty and the top element of the stack cannot be returned"); 56 return null; 57} 58 return (E) data [top]; // return stack top element 59} 60 61 public int getIndex (E e) {62 // traverse stack 63 while (top! =-1) {64 // peek () returns the top 65 if (peek () of the current stack (). equals (e) {66 return top; 67} 68 top --; 69} 70 71 return-1; 72} 73 74 public int size () {75 return this. top + 1; // stack top value + 1, which is the actual number of stack elements 76} 77 78 public int getStackSize () {79 return this. maxSize; // return the actual stack length 80} 81 82 public void display () {83 // traverse 84 while (top! =-1) {85 System. out. println (top); 86 top --; 87} 88} 89 90 public static void main (String [] args) {91 MyStack <Integer> stack = new MyStack <Integer> (); 92 // inbound stack 93 for (int I = 0; I <10; I ++) {94 stack. push (I); 95} 96 // output stack 97 // stack. pop (); 98 // return stack top 99 // System. out. println (stack. peek (); 100 // length: 101 // System. out. println (stack. size (); 102 103} 104 105}


// Chain Stack

1 package stack; 2 3 // The chain storage structure of the stack, which consists of multiple nodes. The top of the stack in the stack is the first node, and the node consists of the data domain and pointer domain. 4 // The operations on the stack are indirectly completed through the top of the stack (the first node. 5 // The ordered stack is a special sequence table 6 // The chain stack is a special chain table 7 public class objective stack {8 // defines Node class 9 private class Node {10 public object data = null; // data field 11 public Node next = null; // pointer field 12 13 // constructor initialization 14 @ SuppressWarnings ("unused") 15 public Node () {} 16 17 public Node (Object data, Node next) {18 this. data = data; 19 this. next = next; 20} 21 22 @ Override23 public String toString () {24 return "Node [data =" + data + ", next =" + next + "]"; 25} 26}/* Node */27 28 private Node top = null; // defines the stack top 29 private int size = 0; // define the number of stack nodes 30 31 // judge the empty stack 32 public boolean isEmpty () {33 return size = 0? True: false; 34} 35 36 // pressure stack 37 public boolean push (Object obj) {38 // update the header node so that the new node points to the original header node 39 System. out. println ("Stack pressure success:" + obj + "pointing to->" + top); 40 top = new Node (obj, top ); // stack pressure is to insert the node before the stack top. That is, update the header node. Change pointer to 41 size ++; // stack Length + 42 return true; 43} 44 45 // stack 46 public Object pop () {47 if (isEmpty ()) {48 System. out. println ("sorry, the current stack is empty and cannot be out of the stack"); 49 return null; 50} 51 Node temp = top; // the header Node References 52 top = top. next; // update the header node 53 temp. next = null; // release the reference and delete the pointer pointing to 54 size --; // Number of stack nodes-155 return temp. data; // output stack 56} 57 58 // return the top element of the stack, but the stack 59 public Object peek () {60 return this. top. data; // directly return the top stack element 61} 62 63 // traverse the stack and print 64 public void Display () {65 // from the top of the stack node to the bottom of the stack node null traversal 66 while (top! = Null) {67 System. out. println (top. data); 68 top = top. next; 69} 70} 71 72 // position of the returned element in the stack 73 public int getIndex (Object obj) {74 int I = 0; 75 while (top! = Null) {76 if (peek (). equals (obj) {77 return I; 78} 79 top = top. next; 80 I ++; 81} 82 return-1; 83} 84 85 // return stack length 86 public int getSize () {87 return this. size; 88} 89 90 public static void main (String [] args) {91 Alibaba stack = new Alibaba stack (); 92 for (int I = 0; I <10; I ++) {93 stack. push (I); 94} 95 // stack. display (); 96 System. out. println (stack. getIndex (9); 97} 98 99}

The queue and tree will be updated tomorrow, followed by the depth-first, breadth-first, and various algorithms of the graph.

 

 

 

 

 

 

  

  

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.