数据结构--线性表

来源:互联网 发布:语音广播软件 编辑:程序博客网 时间:2024/05/29 04:07

1 顺序表
1.1顺序表的顺序存储结构称为顺序表,顺序表是用一段连续的存储单元依次存储线性表的数据结构。用数组来存储数据元素。
核心代码如下:

public class SequentialList<T> {    /**     * 默认的大小     */    private static final Integer DEFAULT_CAPACITY = 16;    /**     * 存储的元素个数     */    private Integer elementSize = 0;    /**     * 最大存储元素个数     */    private static final Integer MAX_CAPACITY = Integer.MAX_VALUE;    /**     * 当前容量大小     */    private Integer elementCapacity=DEFAULT_CAPACITY;    /**     * 数据元素     */    private Object[] elementData;    /**     * 构造顺序表,设置大小为默认值     */    public SequentialList() {        elementData = new Object[DEFAULT_CAPACITY];//数组初始化    }    /**     * 构造顺序表 设置集合大小     *      * @param size     */    public SequentialList(Integer size) {        this.elementCapacity = size;        elementData = new Object[size];//生成指定大小的数组    }    /**     * 传递一个数组来构造顺序表     *      * @param t     *            构造集合的数组     */    public SequentialList(T[] t) {        elementData = new Object[t.length];//生成指定大小的数组        this.elementCapacity = t.length;        for (int i = 0; i < t.length; i++) {            addElement(t[i]);        }    }    /**     * 添加元素     *      * @param index     *            要添加的位置索引,从1开始数     * @param t     *            元素     * @return 添加成功返回true,超过空间最大值返回false     */    public boolean addElement(Integer index, T t) {        index = index - 1;// 传递进来的数从1开始数,索引从0开始        if (index > MAX_CAPACITY - 1) {            return false;        } else {            if (elementData.length >= elementSize) {                growList(elementSize + 1);            }            Integer temp = elementData.length - (index + 1);            System.arraycopy(elementData, index, elementData, index + 1, temp);            elementData[index] = t;            return true;        }    }    /**     * 扩充集合大小     *      * @param newSize     *            扩充的大小     */    public void growList(Integer newSize) {        elementData = Arrays.copyOf(elementData, newSize);        elementCapacity=newSize;    }    /**     * 删除指定的元素     *      * @param index     *            要删除的位置索引,从1开始数     * @return     */    public boolean remove(Integer index) {        index=index-1;        if (index > elementSize - 1) {            return false;        } else {            Integer temp = elementData.length - (index+1);            System.arraycopy(elementData, index+1, elementData, index, temp);            elementData[elementSize - 1] = null;            elementSize--;            return true;        }    }    /**     * 遍历输出元素     */    public void printCollection() {        for (int i = 0; i < elementData.length; i++) {            System.out.print(elementData[i]+"  ");        }    }    public static void main(String[] agrs) {        SequentialList<Integer> seq = new SequentialList<Integer>();        seq.addElement(1);        seq.addElement(3);        seq.addElement(2);        seq.addElement(4);        seq.remove(1);        seq.printCollection();    }}

2 单链表
2.1单链表是用一组任意的存储单元来存放线性表元素,可以不连续,一般节点中有数据域和指针域,数据域用于存储数据,而指针域用于指向其指向的下一个节点。一般有一个头指针,指向第一个节点。

public class LinkListCustom<E> {    /**     * 头指针,指向第一个节点     */    private Node first=null;    /**     * 链表大小     */    private Integer size;    /**     * 节点结构     * @author Tang     */    private class Node<E>    {        private Node<E> next;        private E data;        public Node(E data,Node<E> next)        {            this.data=data;            this.next=next;        }    }    /**     * 根据一个数组生成一个单链表     * @param data 数组     */    public LinkListCustom(E[] data)    {        Node<E> lastNode=first;        for(int i=0;i<data.length;i++)        {            if(i==0){                Node<E> node=new Node(data[i],null);                this.first=node;                lastNode=node;//存储当前节点,当有下一个节点时更新该节点的next指向            }else{                Node<E> node=new Node(data[i],null);                lastNode.next=node;                lastNode=node;//存储当前节点,当有下一个节点时更新该节点的next指向            }        }        this.size=data.length;    }    /**     * 格式化节点大小     */    public void trimSize()    {        Node<E> p=first;        int tempSize=0;        while(p.next!=null)        {            p=p.next;            tempSize++;         }        this.size=tempSize;    }    /**     * 插入     * @param index 位置,从1开始数     * @param d 数据     */    public void put(Integer index,E data)    {        Node<E> p=first;        int count=1;        while(count<index-1)//找到要删除的节点的前面的一个节点        {            p=p.next;            count++;            }        Node newNode=new Node(data, p.next);        p.next=newNode;        this.size++;    }    /**     * 删除     * @param index      * @return 返回被删除的节点     */    public Node<E> Delete(Integer index)    {        Node p=first;        int count=1;        Node<E> temp;        while(count<index-1)//找到要删除的节点的前面的一个节点        {            p=p.next;            count++;            }        temp=p.next;        p.next=p.next.next;        return temp;    }    /**     * 遍历输出     */    public void PrintList()    {        Node tempNode=first;        while(tempNode!=null)        {            System.out.println(tempNode.data);            tempNode=tempNode.next;        }    }    public static void main(String[] args) {        // TODO Auto-generated method stub        Integer[] data={1,2,3,4,5};        LinkListCustom<Integer> linklist=new LinkListCustom<Integer>(data);//数组构造链表        linklist.PrintList();        System.out.println("........");        linklist.Delete(3);//测试删除        linklist.PrintList();        System.out.println("........");        linklist.put(3,10);        linklist.PrintList();    }}
原创粉丝点击