1. <ul id="0c1fb"></ul>

      <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
      <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区

      RELATEED CONSULTING
      相關(guān)咨詢
      選擇下列產(chǎn)品馬上在線溝通
      服務(wù)時間:8:30-17:00
      你可能遇到了下面的問題
      關(guān)閉右側(cè)工具欄

      新聞中心

      這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
      java數(shù)據(jù)結(jié)構(gòu)代碼實現(xiàn),數(shù)據(jù)結(jié)構(gòu) java版

      表達式求值 數(shù)據(jù)結(jié)構(gòu) java實現(xiàn)

      1. 定義優(yōu)先級和優(yōu)先級表

      我們注重客戶提出的每個要求,我們充分考慮每一個細節(jié),我們積極的做好網(wǎng)站制作、網(wǎng)站建設(shè)服務(wù),我們努力開拓更好的視野,通過不懈的努力,成都創(chuàng)新互聯(lián)公司贏得了業(yè)內(nèi)的良好聲譽,這一切,也不斷的激勵著我們更好的服務(wù)客戶。 主要業(yè)務(wù):網(wǎng)站建設(shè),網(wǎng)站制作,網(wǎng)站設(shè)計,成都小程序開發(fā),網(wǎng)站開發(fā),技術(shù)開發(fā)實力,DIV+CSS,PHP及ASP,ASP.Net,SQL數(shù)據(jù)庫的技術(shù)開發(fā)工程師。

      Java代碼

      /**

      * 運算符優(yōu)先權(quán)

      */

      public enum Precede {

      /**

      * 優(yōu)先權(quán)高

      */

      LARGER,

      /**

      * 優(yōu)先權(quán)低

      */

      LESS;

      /**

      * 優(yōu)先級表

      * + - * /

      * +

      * -

      * *

      * /

      */

      private static Precede[][] precedes = new Precede[4][4];

      static {

      // 根據(jù)優(yōu)先級表初始化precedes數(shù)組

      for (int i = 0; i precedes.length; i++) {

      for (int j = 0; j precedes[i].length; j++) {

      if ((i == 0 || i == 1) j 1) {

      precedes[i][j] = LESS;

      } else {

      precedes[i][j] = LARGER;

      }

      }

      }

      }

      /**

      * 判斷2個運算符的優(yōu)先級

      */

      public static Precede judgePrecede(char operand1, char operand2) {

      int left = getIndex(operand1);

      int right = getIndex(operand2);

      return precedes[left][right];

      }

      /**

      * 獲取運算符對應(yīng)的數(shù)組索引

      */

      private static int getIndex(char operand) {

      switch (operand) {

      case '+':

      return 0;

      case '-':

      return 1;

      case '*':

      return 2;

      case '/':

      return 3;

      default:

      throw new IllegalArgumentException();

      }

      }

      }

      2. 表達式求值

      Java代碼

      /**

      * 整數(shù)表達式運算

      */

      public class EvaluateExpression {

      /**

      * 表達式

      */

      private String expression;

      /**

      * 最初的表達式

      */

      private String initExpression;

      /**

      * 運算符棧

      */

      private MyStackCharacter optr = new MyArrayStackCharacter();

      /**

      * 操作數(shù)棧

      */

      private MyStackInteger opnd = new MyArrayStackInteger();

      /**

      * 表明下一個是否應(yīng)該是數(shù)字

      */

      private boolean numberNext;

      public EvaluateExpression(String expression) {

      this.expression = expression;

      this.initExpression = expression;

      numberNext = true;

      }

      /**

      * 求值

      */

      public Integer evaluate() {

      delBlank();

      handleParentheses();

      while (true) {

      if ("".equals(expression)) {

      break;

      }

      if (expression.matches("^-?\\d+.*$") numberNext) {

      opnd.push(getInteger());

      continue;

      } else {

      Character operand = expression.charAt(0);

      numberNext = true;

      expression = expression.substring(1);

      Character pop = optr.pop();

      if (pop == null) {

      optr.push(operand);

      continue;

      } else {

      Precede precede = Precede.judgePrecede(pop, operand);

      switch (precede) {

      // 優(yōu)先級高時運算前2個操作數(shù)

      case LARGER: {

      optr.push(operand);

      Integer next = opnd.pop();

      Integer last = opnd.pop();

      evaluateNow(last, pop, next);

      break;

      }

      // 優(yōu)先級低時運算前一個操作數(shù)和后一個操作數(shù)

      case LESS: {

      optr.push(pop);

      Integer last = opnd.pop();

      Integer next = getInteger();

      evaluateNow(last, operand, next);

      break;

      }

      }

      }

      }

      }

      // 運算結(jié)果

      Integer result = null;

      if (optr.length() == 0 opnd.length() == 1) {

      result = opnd.pop();

      } else if (optr.length() == 1 opnd.length() == 2) {

      Integer next = opnd.pop();

      Integer last = opnd.pop();

      evaluateNow(last, optr.pop(), next);

      result = opnd.pop();

      } else {

      throw new RuntimeException();

      }

      return result;

      }

      /**

      * 進行實際的運算,并將結(jié)果入棧

      */

      private void evaluateNow(Integer last, Character operand, Integer next) {

      switch (operand) {

      case '+':

      opnd.push(last + next);

      break;

      case '-':

      opnd.push(last - next);

      break;

      case '*':

      opnd.push(last * next);

      break;

      case '/':

      opnd.push(last / next);

      break;

      }

      }

      /**

      * 獲得表達式開頭部分的整數(shù)

      */

      private Integer getInteger() {

      StringBuilder sb = new StringBuilder();

      int count = 0; // 整數(shù)位

      boolean lessZero = false; // 是否是負(fù)數(shù)

      if (expression.startsWith("-")) {

      sb.append("-");

      count++;

      lessZero = true;

      }

      int i = (lessZero ? 1 : 0);

      for (; i expression.length(); i++) {

      char c = expression.charAt(i);

      if (c = '0' c = '9') {

      sb.append(c);

      count++;

      } else {

      break;

      }

      }

      expression = expression.substring(count);

      numberNext = false;

      return Integer.valueOf(sb.toString());

      }

      /**

      * 處理括號. 將括號內(nèi)的字符串作為子表達式計算.

      */

      private void handleParentheses() {

      while (expression.contains("(")) {

      // 左括號的索引

      int left = 0;

      // 右括號的索引

      int right = 0;

      // 左括號的數(shù)量

      int count = 0;

      // 求出左括號索引

      left = expression.indexOf('(');

      // 求出對應(yīng)的右括號索引

      for (int i = left; i expression.length(); i++) {

      char c = expression.charAt(i);

      if (c == ')') {

      count--;

      // count為0時才是對應(yīng)的右括號

      if (count == 0) {

      right = i;

      break;

      }

      } else if (c == '(') {

      count++;

      } else {

      continue;

      }

      }

      // 左右括號之間是一個子表達式, 計算子表達式的值,并根據(jù)結(jié)果構(gòu)造出新的表達式

      EvaluateExpression evaluateExpression = new EvaluateExpression(expression.substring(left + 1, right));

      expression = expression.substring(0, left) + evaluateExpression.evaluate()

      + expression.substring(right + 1);

      }

      }

      /**

      * 刪除表達式中的空白字符

      */

      private void delBlank() {

      expression = expression.replaceAll("\\s", "");

      }

      @Override

      public String toString() {

      return initExpression;

      }

      }

      3. 進行測試

      Java代碼

      @Test

      public void testEvaluate() {

      EvaluateExpression expression = new EvaluateExpression("1 + 2 ");

      System.out.println(expression + " = " + expression.evaluate());

      expression = new EvaluateExpression("4 + 2 * 3 - 10 / 5");

      System.out.println(expression + " = " + expression.evaluate());

      expression = new EvaluateExpression("(1+2) * (4 + 5) - (9 / 7)");

      System.out.println(expression + " = " + expression.evaluate());

      expression = new EvaluateExpression("(1 + (3 * (4 - 9)))");

      System.out.println(expression + " = " + expression.evaluate());

      expression = new EvaluateExpression("(1 + (3 * (4 - 9))) + (3 * (2 + 3))");

      System.out.println(expression + " = " + expression.evaluate());

      }

      測試的結(jié)果為:

      1 + 2 = 3

      4 + 2 * 3 - 10 / 5 = 8

      (1+2) * (4 + 5) - (9 / 7) = 26

      (1 + (3 * (4 - 9))) = -14

      (1 + (3 * (4 - 9))) + (3 * (2 + 3)) = 1

      用java實現(xiàn)一個數(shù)據(jù)結(jié)構(gòu)!

      import java.io.IOException;

      import java.util.Scanner;

      public class LinkList {

      private static Scanner san = new Scanner(System.in);

      public static void main(String[] args) throws IOException {

      List list = new List();

      for (int i = 1; i = 10; i++) {

      System.out.print("請輸入第" + i + "個數(shù): ");

      list.add(san.nextInt());

      list.print();

      }

      System.out.println("輸入的數(shù)據(jù)如下: ");

      list.print();

      }

      }

      class node {

      int data;

      node next = this; // 指向自己

      }

      class List {

      private node header = new node();

      // 循環(huán)鏈表的尾部添加數(shù)據(jù)

      public node add(int data) {

      node current = new node();

      node temp = header;

      while (temp.next != header)

      temp = temp.next;

      current.data = data;

      current.next = temp.next;

      temp.next = current;

      return current;

      }

      // 查詢某個數(shù)字的位置 如果不在 返回-1;

      public int search(int data) {

      node temp = header;

      int n = 0;

      while (temp.next != header) {

      temp = temp.next;

      n++;

      if (temp.data == data)

      break;

      }

      if (temp.data == data)

      return n;

      else

      return -1;

      }

      // 打印出整個鏈表

      public void print() {

      node temp = header;

      while (temp.next != header) {

      temp = temp.next;

      System.out.print(temp.data + " ");

      }

      System.out.println();

      }

      // 插入數(shù)據(jù)

      public node Insert(int pos, int data) {

      node temp = header;

      node current = new node();

      for (int i = 0; i pos - 1; i++) {

      if (temp.next != header) {

      temp = temp.next;

      } else

      return null;

      }

      current.data = data;

      if (temp.next != header) {

      current.next = temp.next;

      }

      temp.next = current;

      return current;

      }

      // 刪除某個數(shù)據(jù)

      public node del(int data) {

      node temp = header;

      node oldtemp = null;

      node current = null;

      while (temp.next != header) {

      oldtemp = temp;

      temp = temp.next;

      if (temp.data == data) {

      current = temp;

      break;

      }

      }

      if (current == header)

      return null;

      oldtemp.next = current.next;

      return current;

      }

      }

      用Java語言編寫數(shù)據(jù)結(jié)構(gòu)中順序表的插入刪除查找代碼并實現(xiàn)

      public class Test {

      public static void main(String[] args) {

      int length = 5;

      int ai = 1;

      String data = "data";

      String[] array = insertArrar(data, ai, length);

      data = delArray(array, ai, length);

      System.out.println(data);

      }

      public static String[] insertArrar(String data,int ai,int length){

      String[] array = new String[length];

      array[ai] = data;

      return array;

      }

      public static String delArray(String[] array,int ai,int length){

      String data = "";

      data=array[ai];

      array[ai]=null;

      for(int i = 0; iarray.length;i++){

      System.out.println(array[i]);

      }

      return data;

      }

      }

      數(shù)據(jù)結(jié)構(gòu)在java里如何實現(xiàn)?

      首先Java沒有指針(為了安全和方便編程).

      其次數(shù)據(jù)結(jié)構(gòu)和指針無關(guān),和語言也無關(guān).

      Java封裝好了各種基本的數(shù)據(jù)結(jié)構(gòu) 比如:

      數(shù)組,隊列,Stack,HashTable,HashSet,HaspMap等等

      你說的順序表 Java中你可以用:

      ArrayList 這個類:

      例子:

      如果你自己想用Java裸寫一個類似功能的類可以參考這個:


      當(dāng)前名稱:java數(shù)據(jù)結(jié)構(gòu)代碼實現(xiàn),數(shù)據(jù)結(jié)構(gòu) java版
      網(wǎng)頁URL:http://www.ef60e0e.cn/article/dsshsop.html
      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区
      1. <ul id="0c1fb"></ul>

        <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
        <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

        同心县| 横山县| 拜城县| 浦县| 新安县| 新化县| 平罗县| 喀喇| 会泽县| 南雄市| 新乐市| 尚义县| 鸡泽县| 晋江市| 福贡县| 射洪县| 广灵县| 都安| 宽城| 桐乡市| 青铜峡市| 金秀| 尚志市| 山阳县| 财经| 临城县| 驻马店市| 自贡市| 兴化市| 神木县| 平泉县| 林口县| 凤台县| 湛江市| 砚山县| 金溪县| 襄樊市| 茶陵县| 东光县| 香格里拉县| 牙克石市|