zoukankan      html  css  js  c++  java
  • 线性表

    顺序存储线性表(数组实现)

    public class Sqlist {
    
        static int [] table;
        static int n;
        
        public Sqlist(int n)
        {
            table=new int[n];
            this.n=0;
        }
        
        public boolean isEmpty()
        {
            return n==0;
        }
        
        public boolean isFull()
        {
            return n>=table.length;
        }
        
        public int getcapacity()
        {
            return table.length;
        }
        
        public int getlength()
        {
            return n;
        }
        
        //ADD
        public void addElem(int loc,int value)
        {
            if(n>=table.length)
            {
                System.out.println("数组满了");
            }
            if((loc<1)||(loc>table.length))
            {
                System.out.println("插入位置错误");
            }
            if(loc<=n+1)
            {
                for(int i=n-1;i>=loc;i--)
                {
                    table[i+1]=table[i];
                }
                table[loc-1]=value;
                n++;
                System.out.println("插入成功,在第"+loc+"个位置之前插入:"+value);
            }
            
        }
        
        //DELETE
        public int getElem(int i)
        {
            if((i<1)||(i>table.length))
            {
                return -1;
            }
            else {
                return table[i-1];
            }
        }
    }

    链式存储 单链表 (头插法、尾插法)

    import java.util.Random;
    
    
    //结点
    public class ListNode {
    
        public int value;
        public ListNode next;
        public ListNode(int data)
        {
            this.value=data;
        }    
        
    }
    
    
    class MySinglelinkedList
    {
        private static ListNode head;
        private static ListNode tail;
        private static int length=0;
        
        public void init()
        {
            
        }
        
        public void print()
        {
            ListNode temp=head;
            while (temp!=null) {
                System.out.println(temp.value);
                temp=temp.next;
            }
        }
        
        
        
        
        //头插法
        public void addNode()
        {
            Random random=new Random();
            head=null;
            for(int i=0;i<20;i++)
            {
                int temp=random.nextInt(100);
                ListNode newNode=new ListNode(i);
                length++;
                if(head==null)
                {
                    head=newNode;
                }
                else {
                    newNode.next=head.next;
                    head.next=newNode;
                }
            }
        }
        
        
        //尾插法
        public void addNode1()
        {
            tail=head=null;
            for(int i=0;i<20;i++)
            {
                ListNode newNode=new ListNode(i);
                length++;
                if(head==null)
                {
                    head=tail=newNode;
                }
                else
                {
                    tail.next=newNode;
                    tail=newNode;
                }
            }
        }
        
    }
  • 相关阅读:
    Android 应用程序集成FaceBook 登录及二次封装
    Android MVP 设计模式
    java 接口的作用和好处
    Android版本和API Level对应关系
    Android 开源库和项目 2
    高效开发iOS系列 -- 那些不为人知的KVC
    HDU 1019 Least Common Multiple 数学题解
    程序猿喜欢如何的职位描写叙述?
    从零開始搭建微信硬件开发环境全过程——1小时掌握微信硬件开发流程
    Spring ORM数据訪问——Hibernate
  • 原文地址:https://www.cnblogs.com/Maskisland/p/10048850.html
Copyright © 2011-2022 走看看