ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

2022-7-24 第一小组 甘源册 学习笔记

2022-07-25 21:02:31  阅读:110  来源: 互联网

标签:24 Node head 甘源册 next public 2022 Integer stack


算法

学习心情

不太明白迭代器接口的用法,而且链表也一直遍历不到表头,求老师指点一下。

1.单链表

点击查看代码
package LB;


import java.util.Iterator;

public class LinkList<T> implements Iterable<T> {

    private Node head;
    private Integer N=1;


    public LinkList() {
    }


    public LinkList(Node head, Integer N) {
        this.head =head ;
        this.N = N;
    }

    public void clear(){
        this.head=null;
        this.N=0;
    }
    public boolean isEmpty(){
        return N==0;
    }
    public int length(){
        return N;
    }
    public Object get(Integer a){
        Node n=head.next;
        for (Integer i = 0; i < a; i++) {
            n=n.next;
        }
        return n.item;
    }
    public void insert(T t){
        Node node1 = new Node(t,null);
        Node n=head;
        while (n.next!=null){
            n=n.next;
        }
        n.next=node1;
        N++;
    }
    public void insert(Integer i ,T t){
        N++;
        Node no=head;
        for (Integer integer = 0; integer < i; integer++) {
            no=no.next;
        }
        Node curr=no.next;
        Node node = new Node(t,curr);
        no.next=node;

    }
    public Object remove(Integer i){
        if (i<0||i>N) {
            throw new RuntimeException("位置不合法");
        }
        Node pre=head;
            for (Integer integer = 0; integer < i; integer++) {
                pre=pre.next;
            }
        Node curr=pre.next;
            pre.next=curr.next;
            N--;
            return curr.item;
    }
    public int indexOf(T t){
        Node pre=head;
        for (Integer i = 0; i < N; i++) {
            pre=pre.next;
            if (pre.item.equals(t)){
                return i;
            }
        }
        return -1;
    }
    public void recever(){
        if (isEmpty()){
            return;
        }
            recever(head.next);
    }
    public Node recever(Node curr){
        if (curr.next == null){
            head.next=curr;
            return curr;
        }
        Node pre=recever(curr.next);
        pre.next=curr;
        curr.next=null;
        return curr;
    }

    @Override
        public Iterator<T> iterator() {
            return new LIterator();
        }
        private class LIterator implements Iterator{
            private Node n;
            public LIterator(){
                this.n=head;
            }
            @Override
            public boolean hasNext() {
                return n.next!=null;
            }
            @Override
            public Object next() {
                n = n.next;
                return  n.item;
            }
    }

    public static void main(String[] args) {
        Node<Integer> nod = new Node<>(2,null);
        LinkList<String> num1 = new LinkList<>(nod,1);

        num1.insert("11");
        num1.insert("鲁大师");
        num1.insert("的撒法");
        num1.insert(1,"的撒法2阿");
        num1.insert("2");
//        num1.recever();
//        num1.remove(1);
//        System.out.println(num1.indexOf("2"));
        for (String integer : num1) {
            System.out.println(integer);
        }
    }
}
class Node<T>{
    T item;
    Node next;

    public Node(T item, Node next) {
        this.item = item;
        this.next = next;
    }
}

2.双链表

点击查看代码

package LB;

import org.w3c.dom.Node;

import java.util.Iterator;
import java.util.List;

public class TowLayLinkList<T> implements Iterable {
    private Integer length=0;
    private Nod     head;
    private Nod     tail;

    public TowLayLinkList() {
    }

    public TowLayLinkList(Nod head, Nod tail) {
        this.head = head;
        this.tail = tail;
        this.head.setNext( tail );
        this.tail.setPre(head);

    }

    public TowLayLinkList(Integer length, Nod head, Nod tail) {
        this.length = length;
        this.head = head;
        this.tail = tail;



    }

    //插入元素
    public void insert(T t){
        Nod n=head;

        while (n.getNext()!=tail ){
            n=n.getNext();
        }
        Nod<T> nod = new Nod<>(t,n,tail);
        n.setNext(nod);
        tail.setPre(nod);
         length++;
    };
    //按位置插入元素
    public void insert(Integer a,T t){
        Nod n=head;
        for (Integer i = 0; i < a; i++) {
            n=n.getNext();
        }
        Nod no=n.getNext();
        Nod<T> nod = new Nod<>(t,n,no);
        n.setNext(nod);
        nod.setNext(no);
        length++;
    }
    public T remove(Integer a ){
        Nod n=head;
        for (Integer i = 0; i < a; i++) {
            n=n.getNext();
        }

        Nod no=n.getNext();
        n.setNext(n.getNext().getNext());
        length--;
        return (T) no.getT();
    }
    public T get(Integer a){
        Nod n=head;
        for (Integer i = 0; i <= a; i++) {
            n=n.getNext();
        }
        return (T) n.getT();
    }
    public T getFirst(){
        return (T) head.getNext().getT();
    }
    public T getLast(){
        return (T) tail.getPre().getT();
    }
    public void clear(){
        head.setNext(null);
        head.setPre(null);
        head.setT(null);
        tail.setPre(null);
        length=0;
    }
    public Integer indexof(T t){
        Nod n=head;

        while (n.getNext()!=tail ){
            if (n.equals(t)) {
                return 1;
            }
            n=n.getNext();
        }
        return -1;
    }
    @Override
    public Iterator<T> iterator() {
        return new LIterator();
    }
    private class LIterator implements Iterator{
        private Nod n;
        public LIterator(){
            this.n=head;
        }
        @Override
        public boolean hasNext() {
            return n.getNext()!=null;
        }
        @Override
        public Object next() {
            n = n.getNext();
            return  n.getT();
        }
    }




    public static void main(String[] args) {
        Nod<String> nod = new Nod<>("st");
        Nod<String> nod1 = new Nod<>("");
        TowLayLinkList<String> list = new TowLayLinkList<>(nod,nod1);
         list.insert("12");
        list.insert("122");
        list.insert("1312");
        list.insert(1,"dwa12");
         for (Object o : list) {

            System.out.println(o);
        }
        System.out.println("---------------------");
        System.out.println("最后一个元素"+list.getLast());
        System.out.println("第一个元素"+list.getFirst());
        System.out.println("---------------------");
        System.out.println(list.get(2));
        System.out.println("---------------------");
        System.out.println(list.remove(1));
        for (Object o : list) {
            System.out.print (o+"-->");
        }
        System.out.println();
        System.out.println("---------------------");
        list.clear();
//         System.out.println(list.length);
        for (Object o : list) {
            System.out.println(o);
        }

    }
}
class Nod<T>{
    private T t;
    private Nod pre ;
    private Nod next;

    public Nod() {
    }

    public Nod(T t) {
        this.t = t;

     }

    public Nod(T t, Nod pre, Nod next) {
        this.t = t;
        this.pre = pre;
        this.next = next;
    }

    public T getT() {
        return t;
    }

    public void setT(T t) {
        this.t = t;
    }


    public Nod getPre() {
        return pre;
    }

    public void setPre(Nod pre) {
        this.pre = pre;
    }

    public Nod getNext() {
        return next;
    }

    public void setNext(Nod next) {
        this.next = next;
    }
}

3.栈


package stack;

import LB.LinkList;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class Stack<T> implements Iterable<T>  {
    private Integer N;
    private Node head;

    public Stack() {
    }

    public Stack(Node head) {
        this.head = head;
    }

    public Stack(Integer n, Node head) {
        N = n;
        this.head = head;
    }

    public void push(T t) {
        Node n = head;
        while (n.next != null) {
            n = n.next;
        }
        Node nod = new Node(t, null);
        n.next = nod;
        N++;
    }

    public T pop() {
        Node n = head;
        Node node=null;
        if (n!=null) {
            if (n.next != null) {
                while (n.next != null) {
                    node = n;
                    n = n.next;
                }

                node.next = null;
                N--;
                return (T) n.item;
            }
        }

        return null;
    }

    public int size() {
        return N;
    }
    public void test(Stack<Integer> stack,String str){
        Integer a=0;
        for (int i = 0; i < str.length(); i++) {
            if (str.indexOf("(", i) >= 0) {
                i = str.indexOf("(", i);
                stack.push(i);
            }
        }
        for (int i = 0; i < str.length(); i++) {
            if (str.indexOf(")", i) >= 0) {
                i = str.indexOf(")", i);
                  a = stack.pop();
            }
        }

        if (a!=null&&stack.size()==0){
            System.out.println(str+"----"+"正确匹配");
            stack.N=0;
        }else {
            System.out.println(str+"----"+"错误匹配");
            stack.N=0;
        }
    }
    public static int caculate(String[] notaion,Stack<Integer> stack){
        for (String s : notaion) {
            switch (s){
                case "+":
                    Integer pop = stack.pop();
                    Integer pop1 = stack.pop();
                    Integer result=pop+pop1;
                    stack.push(result);
                    break;
                case "-":
                    Integer pop2 = stack.pop();
                    Integer pop3 = stack.pop();
                    Integer result1=pop3-pop2;
                    stack.push(result1);
                    break;
                case "*":
                    Integer pop4 = stack.pop();
                    Integer pop5 = stack.pop();
                    Integer result2=pop4*pop5;
                    stack.push(result2);
                    break;
                case "/":
                    Integer pop6 = stack.pop();
                    Integer pop7 = stack.pop();
                    Integer result3=pop7/pop6;
                    stack.push(result3);
                    break;
                default:
                    stack.push(Integer.parseInt(s));
            }
        }
        for (Integer integer : stack) {
            System.out.println("<"+integer+">");
            return integer;
        }

        return -1;
    }

    @Override
    public Iterator iterator() {
        return new LIterator();
    }

    private class LIterator implements Iterator {
        private Node n;

        public LIterator() {
            this.n = head;
        }

        @Override
        public boolean hasNext() {
            return n.next != null;
        }

        @Override
        public Object next() {
            n = n.next;
            return n.item;
        }
    }

    public static void main(String[] args) {
        Node<Integer> node = new Node<Integer>(12, null);
        Stack<Integer> stack = new Stack<>(0, node);

        String str1 = "(上海)(长安)";
        String str2 = "上海((长安))";
        String str3 = "上海(长安(北京)(深圳)南京)";
        String str4 = "上海(长安))";
        String str5 = "((上海)长安";
        String str6 = "(((())";
        String str7 = "(()))))";
        String[] str=new String[]{"3","17","15","-","*","18","6","/","+"};
        String[] ss=new String[]{"1","5","2","-","3","*","+"};

//        stack.test(stack,str1);
//        stack.test(stack,str2);
//        stack.test(stack,str3);
//        stack.test(stack,str4);
//        stack.test(stack,str5);
//        stack.test(stack,str6);
//        stack.test(stack,str7);

        System.out.println("运算结果"+caculate(ss, stack));

//        stack.push(12);
//        stack.push(41);
//        stack.push(5124);
//        stack.pop();
//        for (Integer s : stack) {
//            System.out.println(s);
//        }
    }
}

class Node<T> {
    T item;
    Node next;

    public Node(T item, Node next) {
        this.item = item;
        this.next = next;
    }
}
用栈实现队列
package stack;
public class Tse    {
    public static void main(String[] args) {
        Node<Integer> node = new Node<Integer>(12, null);
        Node<Integer> node1 = new Node<Integer>(11, null);
        Stack<Integer> stack1 = new Stack<>(0, node);
        Stack<Integer> stack2 = new Stack<>(0, node1);
        Tse tse = new Tse();

        if (stack2.size()==0) {
            stack1.push(1);
            stack1.push(2);
            stack1.push(3);
            stack1.push(4);
            Integer s=stack1.size();
            d(stack1,stack2);
            for (Integer i = 0; i < s; i++) {
                stack2.push(stack1.pop());
            }
            for (Integer i = 0; i < s; i++) {
                System.out.println("第"+(i+1)+"个出栈的元素为:"+stack2.pop());
            }

        }else {
            System.out.println("输出栈不为空");
        }

    }

    public static void d(Stack<Integer> stack1 ,Stack<Integer> stack2){
        System.out.println();
        System.out.print("输入栈:");
        for (Object o : stack1) {
            System.out.print(o+"->");
        }
        System.out.println();
        System.out.print("输出栈:");
        for (Object o : stack2) {
            System.out.print(o+"->");
        }
        System.out.println();
    }
}

标签:24,Node,head,甘源册,next,public,2022,Integer,stack
来源: https://www.cnblogs.com/gycddd/p/16516901.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有