1. 程式人生 > >數據結構與算法-線性表

數據結構與算法-線性表

con urn head play strong 理解 數據 ble 位置

近期在學習數據結構,反反復復已經看過幾遍了,也做了一些練習題,但總感覺不記錄一下,思路就不是很清晰,所以,從今天開始總結這段時間對數據結構的學習。

無論學習什麽,基礎知識都是最總要的,數據結構也不例外。線性表就是數據結構的基礎,很多常見的數據結構都是基於線性表來實現的。

那麽,什麽是線性表呢?官方的定義是:

  零個或多個數據元素的有限序列

可以從兩個方面來理解線性表,首先它是一個序列,也就是其中的元素有先後順序,其次是有限的,對於無線數列,也只能存在於理論數學中。

說了這麽多,小結一下:

  1)線性表是數據結構中的基礎,它是零個或多個數據元素的有限序列。

2)線性表中頭結點沒有直接前驅節點

3)線性表中尾節點沒有直接後繼節點

4)線性表中節點的類型相同

每種數據結構都有與之對應的操作,線性表的基本操作包括:初始化、獲取長度、是否為空、獲取節點、添加節點、刪除節點。

線性表包括兩種存儲方式,一種是順序存儲,一種是鏈式存儲。下面就分別用這兩種結構實現線性表的基本操作。

線性表的順序存儲:

 1 public class ArrayLineTable<E> {
 2 
 3     private E[] datas;
 4     private int maxSize;
 5     private int size;
 6     
 7
//初始化 8 public ArrayLineTable(int maxSize){ 9 datas = (E[])new Object[maxSize]; 10 this.maxSize = maxSize; 11 this.size = 0; 12 } 13 14 public ArrayLineTable(){ 15 this(10); 16 } 17 18 19 //添加元素:向最後添加 20 public void add(E value){
21 //判斷線性表是否已滿 22 if(this.size == maxSize) { 23 throw new RuntimeException("線性表已滿"); 24 } 25 datas[size] = value; 26 this.size ++; 27 } 28 29 public void add(int index, E value) { 30 if(this.size == maxSize){ 31 throw new RuntimeException("線性表已滿"); 32 } 33 34 if(index < 0 || index >= datas.length - 1){ 35 throw new RuntimeException("索引錯誤"); 36 } 37 38 //移動元素 39 for(int i = datas.length- 1; i <= index; i --) { 40 datas[i + 1] = datas[i]; 41 } 42 43 datas[index] = value; 44 this.size ++; 45 } 46 47 48 //刪除 49 public void delete(int index) { 50 51 if(this.size == 0) { 52 throw new RuntimeException("線性表為空"); 53 } 54 55 if(index < 0 || index > datas.length - 1){ 56 throw new RuntimeException("索引錯誤"); 57 } 58 59 for(int i = index; i < datas.length - 1; i ++) { 60 datas[i] = datas[i + 1]; 61 } 62 this.size --; 63 } 64 65 //查詢 66 public E get(int index) { 67 if(this.size == 0){ 68 throw new RuntimeException("線性表為空"); 69 } 70 71 if(index < 0 || index > datas.length - 1){ 72 throw new RuntimeException("索引錯誤"); 73 } 74 75 return datas[index]; 76 } 77 }

線性表的連式存儲:

public class LinkedLineTable<E> {

    
    private Node<E> head;
    private int size;
    
    //初始化
    public LinkedLineTable(){
        this.head = new Node<E>(null, null);
    }
    
    
    //在鏈表的末尾添加元素
    public void add(E data){
        
        Node<E> temp = head;
        //遍歷到最後一個節點
        while(temp.next != null){
            temp = temp.next;
        }
        
        //創建新的節點
        Node<E> newNode = new Node<E>(data, null);
        temp.next = newNode;
    
        this.size ++;
    }
    
    
    //在指定為位置添加節點
    public void add(E data, int index){
        
        int counter = 0;
        Node<E> temp = head.next;
        while(temp.next != null && counter < index - 1){
            temp = temp.next;
            counter ++;
        }
        
        //遍歷到index位置的前一個節點
        //創建一個新的節點
        Node<E> node = new Node<E>(data, null);
        
        node.next = temp.next;
        temp.next = node;
        
        this.size ++;
    }
    
    //刪除指定位置上的元素
    public void delete(int index){
        
        if(head.next == null){
            throw new RuntimeException("線性表為空");
        }
        
        if(index >= this.size){
            throw new RuntimeException("索引錯誤");
        }
        
        int counter = 0;
        Node<E> temp = head.next;
        while(temp.next != null && counter < index - 1){
            temp = temp.next;
            counter ++;
        }
        
        //遍歷到index的前一個位置
        //刪除index上的節點
        Node<E> delNode = temp.next;
        temp.next = delNode.next;
        delNode.next = null;
        this.size --;
        
        
    }
    
    
    //獲取指定位置上的元素
    public E get(int index) {
        
        Node<E> temp = head.next;
        int counter = 0;
        while(temp.next != null && counter < index){
            temp = temp.next;
            counter ++;
        }
        return temp.data;
        
    }
    
    
    
    //打印鏈表中的值
    public void display(){
        
        Node<E> temp = head.next;
        while(temp != null){
            System.out.print(temp.data + " ");
            temp = temp.next;
        }
        
        System.out.println();
    }
    
    
    private static class Node<T>{
        private T data;
        private Node<T> next;
        
        public Node(T data, Node<T> next){
            this.data = data;
            this.next = next;
        }
    }
}

循環鏈表的實現:

/**
 * Created by Administrator on 2017/5/25.
 */

//循環鏈表的實現:帶有頭結點的實現
public class CircleLinkedLineList<E> {


    private Node<E> head;
    //private Node<E> tail;
    private int size;

    //初始化
    public CircleLinkedLineList(){
        this.head = new Node<E>(null, null);
        //tail = head;
        this.head.next = head;

        this.size = 0;
    }


    //尾部添加節點
    public void add(E data){

        //創建新的節點
        Node<E> newNode = new Node<E>(data, null);

        //第一次添加節點
        if(this.head.next == this.head){
            this.head.next = newNode;
            newNode.next = head;

        }else{

            Node<E> temp = head;
            while(temp.next != head){
                temp = temp.next;
            }

            temp.next = newNode;
            newNode.next = head;

        }

        this.size ++;
    }

    //刪除鏈表中的節點
    public void delete(E data){
        Node<E> temp = this.head;

        while(temp.next != head){
            if(temp.next.data.equals(data)){
                Node<E> delNode = temp.next;

                temp.next = delNode.next;
                delNode.next = null;

                this.size --;
            }else{
                temp = temp.next;
            }
        }
    }

    public Node<E> get(int i){
        if(i <= 0 || i >size){
            throw new RuntimeException("索引位置錯誤");
        }

        Node<E> newNode = new Node<E>(null, null);
        int count = 0;
        Node<E> temp = head;
        while(temp.next != head){

            if(count == i){
                newNode.data = temp.next.data;

            }
            temp = temp.next;
            count ++;

        }
        return newNode;
    }

    //判斷鏈表中是否存在某個元素
    public boolean isContain(E data){
        Node<E> temp = head;

        while(temp.next != head){
            if(temp.next.data.equals(data)){
                return true;
            }
            temp = temp.next;
        }

        return false;
    }



    private static class Node<T> {
        private T data;
        private Node<T> next;

        public Node(T data, Node<T> next){
            this.data = data;
            this.next = next;
        }
    }
}

數據結構與算法-線性表