WuLiang-cola

线性结构

线性结构是一组有序的元素集合,常见的线性结构有一维数组,链表,栈,队列。

数组

数组是一个用于存储相邻位置的存储线性结构,使用数组之前需要声明数组的大小,数组访问数组内的元素O(1),因为数组是一块连续的内存,可以利用元素索引,直接算出元素对应的位置,但是数组的删除和插入就需要O(N),

数组的删除操作需要把其他元素向后移动一位,插入需要移动其他元素,这个过程也是O(N),但是要注意是如果数组满了就不能再添加元素,要不然会发生溢出,同理,数组为空的情况下,也不能删除。搜索的情况分两种,一种是在排序好的情况下,搜索的时间复杂度是O (logn),最坏情况是O(N)。

 

线性结构

接口类

public interface LinearAPI {
   public int[]Delete(int arr[] ,int index); //删除
   public int Inset(int[] arr, int index, int num,int ArrLength); //插入
   public int Search(int arr[],int x, int y,int index); //搜索
   public boolean isEmpty(int arr[]); //判断数组是否为空
}

 

实现接口类

public class Array implements LinearAPI {

   @Override
   //判断数组是否为空,如果为空就不能进行删除
   public int[] Delete(int arr[],int index) {
       if (isEmpty(arr)) return arr;
       int[]newsArray = new int[arr.length-1];
       for (int i = 0,idx = 0; i<arr.length; i++)
      {
           if (i == index)
          {
               continue;
          }
           newsArray[idx++] = arr[i];
      }
       return newsArray;
  }

   @Override

   public int Inset(int[] arr, int index, int num,int ArrLength) {
       if (index >= ArrLength)
           return index;
       int i;
       for (i = index-1; (i >= 0 && arr[i] > num); i--)
           arr[i+1] = arr[i];
       arr[i+1] = num;

       return (index+1);
  }

   @Override
   public int Search(int arr[], int x, int y,int index) {

       // 线性查找,如果是有序数组,查找就是O (logn),如果不是有序的话就是O(N)
       if (isEmpty(arr)) return 0;
       int num = 0;
       while (x < y)
      {
           num = x+(y-x)/2;
           if (arr[num] >= index) y = num;
           else x = num+1;
      }
       return x;
  }

   @Override
   public boolean isEmpty(int arr[])
  {
       if (arr.length == 0 || arr == null) return true;
        return false;
  }

}

 

链表

这里只讨论单链表和双链表,链表是指具有N个相同特征的元素,在这个集合里面,除了表头(A[0])和表尾(A[N-1]),其他每个元素都有前趋和后继,对于在集合A里面的每个A[i],它的前趋是A[i-1],后继是A[i+1],表头只有后继,表尾只有前趋,因为链表的元素是动态的,随时都要插入删除,所以使用动态数组来实现更好,链表可以删除和插入在表上的任意位置,但是不能随机存储,链表包括两个域,一个信息域,一个指针域,信息域存储当前信息,指针域指向下一个节点地址。

 

链表接口类

public interface LinkedListAPI {
   public void push(int new_data);
   public void append(int new_data);
}

 

单链表

单链表的链接方向是单向的,因为对链表的访问时从头到尾,所以链表的查找是O(n),插入是O(1),删除是根据情况来的,如果是要删除给定关键字元素,最坏情况是O(n),最好情况是O(1)。

Java实现

public class LinkedList {

   Node head;

   class  Node
  {
       int data; //当前数据
       Node next; //指向下一个

       Node(int d){data = d; next = null;}
  }

   //打印链表
   void printLint()
  {
       Node n = head;
       while (n!= null)
      {
           System.out.print(n.data+" ");
           n = n.next;
      }
  }

   //压入节点到链表
   public void push(int new_data)
  {
       Node new_node = new Node(new_data);
       new_node.next = head;
       head = new_node;
  }

   //插入数据
   public void insertAfter(Node prev_node,int new_data)
  {
       if (prev_node == null) return;

       Node new_node = new Node(new_data);
       new_node.next = prev_node.next;
       prev_node.next = new_node;
  }

   //在尾部添加一个新的节点

   public void append(int new_data)
  {
       Node new_node = new Node(new_data);

       if (head == null)
      {
           head = new Node(new_data);
           return;
      }

       new_node.next = null;

       Node last = head;

       while (last.next != null)
           last = last.next;
       last.next = new_node;
       return;
  }

   //删除节点
   public void deleteNode(int key)
  {
       Node temp = head,prev = null;
       if (temp != null && temp.data == key)
      {
           head = temp.next;
           return;
      }

分类:

技术点:

相关文章: