Java 数据结构实例


  1. 利用堆栈将中缀表达式转换成后缀表达式
  2. 在链表(LinkedList)的开头和结尾添加元素、获取链表(LinkedList)的第一个和最后一个元素、删除链表中的元素、链表元素查找、链表修改
  3. 获取链表的元素
  4. 获取向量元素的索引值、获取向量的最大元素、旋转向量
  5. 栈的实现
  6. 压栈出栈的方法实现字符串反转
  7. 队列(Queue)用法

  1. 利用堆栈将中缀表达式转换成后缀表达式

    import java.io.IOException;
    
    public class Demo {
        private Stack theStack;
        private String input;
        private String output = "";
    
        public Demo(String in) {
            input = in;
            int stackSize = input.length();
            theStack = new Stack(stackSize);
        }
    
        public String doTrans() {
            for (int j = 0; j < input.length(); j++) {
                char ch = input.charAt(j);
                switch (ch) {
                case '+':
                case '-':
                    gotOper(ch, 1);
                    break;
                case '*':
                case '/':
                    gotOper(ch, 2);
                    break;
                case '(':
                    theStack.push(ch);
                    break;
                case ')':
                    gotParen(ch);
                    break;
                default:
                    output = output + ch;
                    break;
                }
            }
            while (!theStack.isEmpty()) {
                output = output + theStack.pop();
            }
            return output;
        }
    
        public void gotOper(char opThis, int prec1) {
            while (!theStack.isEmpty()) {
                char opTop = theStack.pop();
                if (opTop == '(') {
                    theStack.push(opTop);
                    break;
                } else {
                    int prec2;
                    if (opTop == '+' || opTop == '-')
                        prec2 = 1;
                    else
                        prec2 = 2;
                    if (prec2 < prec1) {
                        theStack.push(opTop);
                        break;
                    } else
                        output = output + opTop;
                }
            }
            theStack.push(opThis);
        }
    
        public void gotParen(char ch) {
            while (!theStack.isEmpty()) {
                char chx = theStack.pop();
                if (chx == '(')
                    break;
                else
                    output = output + chx;
            }
        }
    
        public static void main(String[] args) throws IOException {
            String input = "1+2*4/5-7+3/6";
            String output;
            Demo theTrans = new Demo(input);
            output = theTrans.doTrans();
            System.out.println("Postfix is " + output + '\n');
        }
    
        class Stack {
            private int maxSize;
            private char[] stackArray;
            private int top;
    
            public Stack(int max) {
                maxSize = max;
                stackArray = new char[maxSize];
                top = -1;
            }
    
            public void push(char j) {
                stackArray[++top] = j;
            }
    
            public char pop() {
                return stackArray[top--];
            }
    
            public char peek() {
                return stackArray[top];
            }
    
            public boolean isEmpty() {
                return (top == -1);
            }
        }
    }

  2. 在链表(LinkedList)的开头和结尾添加元素、获取链表(LinkedList)的第一个和最后一个元素、删除链表中的元素、链表修改

    import java.util.LinkedList;
    
    public class Demo {
        public static void main(String[] args) {
            // demo1(); // 在链表(LinkedList)的开头和结尾添加元素
            // demo2(); // 获取链表(LinkedList)的第一个和最后一个元素
            // demo3(); // 删除链表中的元素
            demo4(); // 链表元素查找
        }
    
        // 使用 LinkedList 类的 addFirst() 和 addLast() 方法在链表的开头和结尾添加元素
        public static LinkedList<String> demo1() {
            LinkedList<String> ll = new LinkedList<String>();
            ll.add("1");
            ll.add("2");
            ll.add("3");
            System.out.println(ll);
            ll.addFirst("0");
            System.out.println(ll);
            ll.addLast("4");
            System.out.println(ll);
            return ll;
        }
    
        // 使用 LinkedList 类的 linkedlistname.getFirst() 和 linkedlistname.getLast()
        // 来获取链表的第一个和最后一个元素
        public static void demo2() {
            LinkedList<String> ll = new LinkedList<String>();
            ll.add("100");
            ll.add("200");
            ll.add("300");
            System.out.println("链表的第一个元素是:" + ll.getFirst());
            System.out.println("链表的最后一个元素是:" + ll.getLast());
        }
    
        // 使用 clear() 方法来删除链表中的元素
        public static void demo3() {
            LinkedList<String> ll = new LinkedList<String>();
            ll.add("1");
            ll.add("2");
            ll.add("3");
            ll.add("4");
            ll.add("5");
            System.out.println(ll);
            ll.subList(2, 4).clear();
            System.out.println(ll);
        }
    
        // 使用 linkedlistname.indexof(element) 和 linkedlistname.Lastindexof(elementname)
        // 方法在链表中获取元素第一次和最后一次出现的位置
        public static void demo4() {
            LinkedList<String> ll = new LinkedList<String>();
            ll.add("1");
            ll.add("2");
            ll.add("3");
            ll.add("4");
            ll.add("5");
            ll.add("2");
            ll.set(0, "2"); // listname.set() 方法来修改链接中的元素
            System.out.println(ll);
            System.out.println("元素2第一次出现的位置:" + ll.indexOf("2"));
            System.out.println("元素2最后一次出现的位置:" + ll.lastIndexOf("2"));
        }
    }
    

  3. 获取链表的元素

    import java.util.LinkedList;
    
    public class Demo {
        private LinkedList<Object> list = new LinkedList<Object>();
    
        public void push(Object v) {
            list.addFirst(v);
        }
    
        // 使用 top() 和 pop() 方法来获取链表的元素
        public Object top() {
            return list.getFirst();
        }
    
        public Object pop() {
            return list.removeFirst();
        }
    
        public static void main(String[] args) {
            Demo stack = new Demo();
            for (int i = 30; i < 35; i++)
                stack.push(new Integer(i));
            System.out.println(stack.top());
            System.out.println(stack.pop());
            System.out.println(stack.pop());
            System.out.println(stack.top());
        }
    }
  4. 获取向量元素的索引值、获取向量的最大元素、旋转向量

    import java.util.Collections;
    import java.util.Vector;
    
    public class Demo {
        public static void main(String[] args) {
            Vector<String> v = new Vector<String>();
            v.add("X");
            v.add("M");
            v.add("D");
            v.add("A");
            v.add("O");
            System.out.println(v);
            // 使用 Collections 类的 sort() 方法对向量进行排序
            Collections.sort(v);
            System.out.println(v);
            // 使用 binarySearch() 方法来获取向量元素的索引值
            int index = Collections.binarySearch(v, "D");
            System.out.println("元素索引值为 : " + index);
            Object obj = Collections.max(v);
            System.out.println("最大元素是:" + obj);
            // 使用 swap() 函数来旋转向量
            Collections.swap(v, 0, 4);
            System.out.println("旋转后");
            System.out.println(v);
        }
    }

  5. 栈的实现

    public class Demo {
        private int maxSize;
        private long[] stackArray;
        private int top;
    
        public Demo(int s) {
            maxSize = s;
            stackArray = new long[maxSize];
            top = -1;
        }
    
        // 用于插入元素
        public void push(long j) {
            stackArray[++top] = j;
        }
    
        // 用于弹出元素
        public long pop() {
            return stackArray[top--];
        }
    
        public long peek() {
            return stackArray[top];
        }
    
        public boolean isEmpty() {
            return (top == -1);
        }
    
        public boolean isFull() {
            return (top == maxSize - 1);
        }
    
        public static void main(String[] args) {
            Demo theStack = new Demo(10);
            theStack.push(10);
            theStack.push(20);
            theStack.push(30);
            theStack.push(40);
            theStack.push(50);
            while (!theStack.isEmpty()) {
                long value = theStack.pop();
                System.out.print(value);
                System.out.print(" ");
            }
        }
    }

  6. 压栈出栈的方法实现字符串反转

    import java.io.IOException;
    
    public class Demo {
        private String input;
        private String output;
    
        public Demo(String in) {
            input = in;
        }
    
        public String doRev() {
            int stackSize = input.length();
            Stack theStack = new Stack(stackSize);
            for (int i = 0; i < input.length(); i++) {
                char ch = input.charAt(i);
                theStack.push(ch);
            }
            output = "";
            while (!theStack.isEmpty()) {
                char ch = theStack.pop();
                output = output + ch;
            }
            return output;
        }
    
        public static void main(String[] args) throws IOException {
            String input = "www.w3cschool.cc";
            String output;
            Demo theReverser = new Demo(input);
            output = theReverser.doRev();
            System.out.println("反转前: " + input);
            System.out.println("反转后: " + output);
        }
    
        class Stack {
            private int maxSize;
            private char[] stackArray;
            private int top;
    
            public Stack(int max) {
                maxSize = max;
                stackArray = new char[maxSize];
                top = -1;
            }
    
            public void push(char j) {
                stackArray[++top] = j;
            }
    
            public char pop() {
                return stackArray[top--];
            }
    
            public char peek() {
                return stackArray[top];
            }
    
            public boolean isEmpty() {
                return (top == -1);
            }
        }
    }

  7. 队列(Queue)用法

    import java.util.LinkedList;
    import java.util.Queue;
    
    public class Demo {
        public static void main(String[] args) {
            // add()和remove()方法在失败的时候会抛出异常(不推荐)
            Queue<String> queue = new LinkedList<String>();
            // 添加元素
            queue.offer("a");
            queue.offer("b");
            queue.offer("c");
            for (String q : queue) {
                System.out.println(q);
            }
            System.out.println("===");
            System.out.println("poll=" + queue.poll()); // 返回第一个元素,并在队列中删除
            for (String q : queue) {
                System.out.println(q);
            }
            System.out.println("===");
            System.out.println("element=" + queue.element()); // 返回第一个元素
            System.out.println("===");
            System.out.println("peek=" + queue.peek()); // 返回第一个元素
        }
    }

猜你喜欢

转载自blog.csdn.net/shu_ze/article/details/80457012