数据结构与算法JAVA语言描述第三章部分课后习题参考答案

3.2:

a:

class Node{

    public int num;

    public Node next;

    public Node(int num) {

        this.num = num;

    }

}

public class singlyLinkedList {

    public static void swap(Node pre){

        Node temp,next;

        temp=pre.next;

        next=temp.next;

        temp.next=next.next;

        pre.next=next;

        next.next=temp;

    }

}

b:

class Node{

    public int num;

    public Node next;

    public Node pre;

    public Node(int num) {

        this.num = num;

    }

}

public class doublyLinkedList {

    public static void swap(Node temp){

        Node prev,next;

        prev=temp.pre;

        next=temp.next;

        temp.next=next.next;

        prev.next=next;

        next.next=temp;

        temp.pre=next;

        temp.next.pre=temp;

        next.pre=prev;

    }

}

3.8:

a:

Because remove call will change the length of the list and affect the for loop,  so theSize should be stored before entering the loop.

b:

The running time of removeFirstHalf is O(N2 ).  Every time you delete the previous element with remove, you need to move all the subsequent elements forward, so the running time is O(N2 ) .

C:

The running time of removeFirstHalf is O(N). Each remove call’s running time is O(1).

d:

No. Because the first element is deleted every time ,and finding the position does not take time, so using iterators does not make the speed faster

3.22:

import java.util.Stack;

public class PostfixExpression {

    public static void main(String[] args) {

        System.out.println(calculate("1 2 + 5 * 4 -"));

    }

    public static double calculate(String str){

        Stack<Double> stack = new Stack<>();

        String[] s = str.split(" ");

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

            if (s[i].matches("\\d+")){

                stack.push(Double.parseDouble(s[i]));

            }else {

                double num2 = stack.pop();

                double num1=stack.pop();

                double res = 0;

                switch (s[i]){

                    case "+":

                        res = num1+num2;

                        break;

                    case "-":

                        res=num1-num2;

                        break;

                    case "*":

                        res=num1*num2;

                        break;

                    case "/":

                        res=num1/num2;

                        break;

                    case "^":

                        res=Math.pow(num1,num2);

                        break;

                    default:

                        throw new RuntimeException("Wrong Operator!");

                }

                stack.push(res);

            }

        }

        return stack.peek();

    }

}

Operation results:

3.29:

class Node {

    private int date;

    public Node next;

    public int getDate() {

        return date;

    }

    public void setDate(int date) {

        this.date = date;

    }

}

public class reverseList {

    public static void main(String[] args) {

        Node head = new Node();

        head.setDate(-1);

        head.next = null;

        Node p;

        p = head;

        for (int i = 0; i < 10; i++) {

            Node n = new Node();

            n.setDate(i);

            n.next = null;

            p.next = n;

            p = n;

        }

        print(head);

        reverseList(head);

        System.out.println();

        print(head);

    }

    public static void reverseList(Node head) {

        if (head.next == null || head.next.next == null) {

            return;

        }

        Node pre = head.next;

        Node q = head.next.next;

        Node temp = null;

        while (q != null) {

            temp = q.next;

            q.next = pre;

            pre = q;

            q = temp;

        }

        head.next.next = null;

        head.next = pre;

    }

    public static void print(Node head) {

        Node p = head;

        while (p.next != null) {

            p = p.next;

            System.out.print(p.getDate() + " ");

        }

    }

}

3.31:

public class LinkQueue<T> {

    class Node<T> {

        private T data;

        private Node<T> next;

        public Node() {

            this.data = null;

            this.next = null;

        }

        public Node(T data) {

            this.data = data;

            this.next = null;

        }

        public T getData() {

            return data;

        }

        public void setData(T data) {

            this.data = data;

        }

        public Node<T> getNext() {

            return next;

        }

        public void setNext(Node<T> next) {

            this.next = next;

        }

    }

    private Node<T> first;

    private Node<T> last;

class Node {

    private int date;

    public Node next;

    public Node(int date) {

        this.date = date;

    }

    public Node() {

    }

    public int getDate() {

        return date;

    }

    public void setDate(int date) {

        this.date = date;

    }

}

public class SingleStack {

    Node first = new Node();

    Node p;

    public SingleStack(Node first) {

        this.first = first;

    }

    public boolean isEmpty() {

        return first == null;

    }

    public void push(int value) {

        if (first.getDate() == 0) {

            first.setDate(value);

            p = first;

            return;

        }

        Node node = new Node(value);

        p.next = node;

        p = node;

    }

    public int pop() {

        if (isEmpty()) {

            System.out.println("Stack is empty");

            return -1;

        }

        Node pre = first;

        while (true) {

            if (pre.next == null) {

                int temp = first.getDate();

                first = null;

                return temp;

            }

            if (pre.next.next == null) {

                break;

            }

            pre = pre.next;

        }

        int value = pre.next.getDate();

        pre.next = null;

        return value;

    }

    public void list() {

        Node temp = first;

        if (isEmpty()) {

            System.out.println("Stack is empty");

            return;

        }

        while (true) {

            if (temp.next == null) {

                break;

            }

            System.out.println(temp.getDate());

            temp = temp.next;

        }

    }

    public static void main(String[] args) {

        Node p = new Node();

        SingleStack stack = new SingleStack(p);

        for (int i = 1; i < 5; i++) {

            stack.push(i);

        }

        for (int i = 1; !stack.isEmpty(); i++) {

            System.out.println(stack.pop());

        }

    }

}

Test results:

3.32:

    public LinkQueue() {

        this.first = null;

        this.last = null;

    }

    public Node<T> p;

    public boolean inQueue(T t) {

        if (first == null) {

            first = new Node<>(t);

            last = first;

            p = first;

            return true;

        }

        Node<T> temp = new Node<>(t);

        p.next = temp;

        p = temp;

        last = temp;

        return true;

    }

    public T outQueue() {

        if (first == null) {

            return null;

        } else {

            T data = first.data;

            first = first.next;

            return data;

        }

    }

    public T peek() {

        if (first == null) {

            return null;

        } else {

            return first.getData();

        }

    }

    public boolean isEmpty() {

        return first == null;

    }

    public static void main(String[] args) {

        LinkQueue<Integer> linkQueue = new LinkQueue<>();

        for (int i = 0; i < 6; i++) {

            linkQueue.inQueue(i);

        }

        for (int i = 0; !linkQueue.isEmpty(); i++) {

            System.out.print(linkQueue.peek() + " " + linkQueue.outQueue());

            System.out.println();

        }

    }

}

Test results:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值