Flyingis

          Talking and thinking freely !
          Flying in the world of GIS !
          隨筆 - 156, 文章 - 16, 評(píng)論 - 589, 引用 - 0
          數(shù)據(jù)加載中……

          基本數(shù)據(jù)結(jié)構(gòu)的Java實(shí)現(xiàn)

          鏈表

          class Node {

          Object item; Node next;

            Node (Object v) {

          item = v; next = null;

          }

          }

          頭指針,空尾指針

          初始化:head = null;

          x后插入t

          if ( x == null)

          { head = t; head.next = null; }

          else { t.next = x.next; x.next = t; }

          移走x之后的結(jié)點(diǎn):t = x.next; x.next = t.next;

          循環(huán)遍歷:for ( t = head; t != null; t = t.next )

          檢查鏈表是否為空:if ( head == null )

          空頭結(jié)點(diǎn),空尾指針

          初始化:head = new Node(); head.next = null;

          x后插入tt.next = x.next; x.next = t;

          移走x之后的結(jié)點(diǎn):t = x.next; x.next = t.next;

          循環(huán)遍歷:for ( t = head.next; t != null; t = t.next )

          檢查鏈表是否為空:if ( head.next == null )

          空頭結(jié)點(diǎn),空尾結(jié)點(diǎn)

          初始化:head = new Node(); z = new Node(); head.next = z; z.next = z;

          x后插入tt.next = x.next; x.next = t;

          移走x之后的結(jié)點(diǎn):t = x.next; x.next = t.next;

          循環(huán)遍歷:for ( t = head.next; t != z; t = t.next )

          檢查鏈表是否為空:if ( head.next == z )

          循環(huán)鏈表

          第一次插入:head.next = head;

          x后插入tt.next = x.next; x.next = t;

          移走x之后的結(jié)點(diǎn):t = x.next; x.next = t.next;

          循環(huán)遍歷:t = head; do { t = t.next; } while ( t != head );

          檢查是否只有一個(gè)數(shù)據(jù)項(xiàng):if ( head.next == head )

           

          堆棧

          數(shù)組實(shí)現(xiàn)

          class Stack {

            private Object[] s;

            private int n;

            Stack ( int maxN ) {

              s = new Object[maxN]; n = 0;

          }

          boolean isEmpty() { return ( n == 0 ); }

          void push ( Object item ) { s[n++] = item; }

          Object pop() {

            Object t = s[--n]; s[n] = null; return t;

          }

          }

          鏈表實(shí)現(xiàn)

          class Stack {

            private Node head;

            private class Node {

          Object item; Node next;

          Node ( Object item, Node next ) {

            this.item = item; this.next = next;

          }

          }

          Stack ( Object maxN ) { head = null; }

          boolean isEmpty() { return ( head ==null ); }

          void push ( Object item ) { head = new Node(item, head); }

          Object pop() {

            Object v = head.item;

            Node t = head.next;

            head = t;

            return v;

          }

          }

           

          FIFO隊(duì)列的鏈表實(shí)現(xiàn)

          class Queue {

            private class Node {

          Object item; Node next;

          Node ( Object item ) {

            this.item = item; this.next = null;

          }

          }

          Private Node head, tail;

          Queue ( Object max ) { head = null; tail = null; }

          boolean isEmpty() { return ( head ==null ); }

          void put ( Object item ) {

            Node t = tail;

            tail = new Node(item);

            if ( empty() )

              head = tail;

            else t.next = tail

          }

          Object get() {

            Object v = head.item;

            Node t = head.next;

            head = t;

            return v;

          }

          }

          posted on 2006-02-05 23:08 Flyingis 閱讀(1144) 評(píng)論(1)  編輯  收藏 所屬分類: Algorithm

          評(píng)論

          # re: 基本數(shù)據(jù)結(jié)構(gòu)的Java實(shí)現(xiàn)  回復(fù)  更多評(píng)論   

          不錯(cuò),如果能加入多線程的控制就更好了
          2006-02-06 11:54 | 愛拼才會(huì)贏
          主站蜘蛛池模板: 乡宁县| 宁化县| 望奎县| 康定县| 凤阳县| 榆社县| 镇坪县| 信丰县| 石屏县| 通州市| 龙川县| 隆昌县| 郎溪县| 东明县| 峨眉山市| 台南市| 泊头市| 伊春市| 肇源县| 南宫市| 西畴县| 三原县| 宜昌市| 临邑县| 讷河市| 深水埗区| 蒙山县| 泰安市| 滨海县| 灵寿县| 兴文县| 余姚市| 垦利县| 漳浦县| 宾川县| 湟中县| 漾濞| 湖口县| 桐柏县| 仁布县| 丁青县|