步步为营,蚕食数据结构与算法---(一)数组篇(上篇)
数组是最基础的数据结构之一,也是很多高级数据结构的基础。
我们简单复习一下数组的创建与遍历,先做一下热身,后面逐渐一步步实现自己的数组
我们的开发环境主要是JDK1.8,c++11编辑工具是 jetbrains的强大开发工具 IteliJ(java),Clion(c++)
Java版:
1 public class Main { 2 public static void main(String[] args){ 3 int [] arr = new int[10];//init a new array with asigned prams 4 for(int i = 0 ;i < arr.length ; i++){ 5 6 arr[i] = i ; 7 System.out.println(arr[i]);} 8 System.out.println("*****************************"); 9 int scores[] = new int[]{23,44,54};//please be noted diffrent ways of creating a new array and diffrent ways to iterate array 10 11 for( int i = 0 ; i < scores.length ; i ++) 12 System.out.println(scores[i]); 13 System.out.println("*****************************"); 14 for(int score : scores) 15 System.out.println(score); 16 System.out.println("*****************************"); 17 scores[0] = 88 ; 18 for( int i = 0 ; i< scores.length ; i ++) 19 System.out.println(scores[i]); 20 21 }
C++版:
#include <iostream> int main() { int arr[10]; for(int i = 0 ; i < 10 ; i++){ arr[i] = i; std::cout << arr[i] <<" "; } std::cout << std::endl; int scores[] = {23,34,45}; for(int i = 0; i < sizeof(scores)/ sizeof(int) ; i++){ std::cout << scores[i] <<" "; } std::cout<<std::endl; scores[0] = 88; for(int i = 0 ;i < sizeof(scores)/ sizeof(int); i++){ std::cout << scores[i] << " "; } std::cout << std::endl; return 0; }
首先我们创造自己的数组,在Java中,要创建自己的Array类,
Java版:
1 public class Array { 2 private int[] data; 3 private int size; 4 //constructor ,create Array with the input param "capacity" 5 public Array(int capacity){ 6 data = new int[capacity]; 7 size = 0; 8 } 9 //constructor without params,our default capacity is 10 10 public Array(){ 11 this(10); 12 } 13 //get the capacity of this array 14 public int getCapacity(){ 15 return data.length; 16 } 17 //get the amount of elements of this array 18 public int getSize(){ 19 return size; 20 } 21 //judge whether this array is empty or not 22 public boolean isEmpty(){ 23 return size==0; 24 } 25 }
C++版:
1 class Array { 2 private: 3 int *data; 4 int size; 5 int capacity; 6 7 public: 8 Array(int capacity){ 9 data = new int[capacity]; 10 size = 0 ; 11 this->capacity = capacity; 12 } 13 Array(){ 14 data = new int[10]; 15 size = 0 ; 16 capacity = 10; 17 18 } 19 int getCapcity(){ 20 return capacity; 21 } 22 int getSize(){ 23 return size; 24 25 } 26 bool isEmpty(){ 27 return size == 0; 28 } 29 30 };
上面的Array类并不完善,我们将对其进行改进和提升
以Java为例:我们向Array中所有元素 后面添加一个元素,则有代码:
1 public void addLast(int e){ 2 //judge whether can we add a new element 3 if(size == data.length){ 4 throw new IllegalArgumentException("AddLust failed,the array is full"); 5 } 6 //asign the new last element 7 data[size] = e ; 8 //maintain the size of the array 9 size ++ ; 10 }
继续我们创建为索引位置的插入一个元素的方法:
1 public void add(int index ,int e){ 2 if(size ==data.length) 3 throw new IllegalArgumentException("AddLust failed,the array is full"); 4 5 if(index < 0 || index > size) { 6 throw new IllegalArgumentException("Add failed. the range of index must within [0,size)"); 7 } 8 for(int i = size - 1 ;i >= index ; i--){//move elements from the tail of the array 9 data[i+1] = data[i]; 10 } 11 data[index] = e ; 12 size++; 13 14 }
在插入元素时的逻辑如下:插入位置前面的元素不变,插入位置及其插入位置以后的元素均后移一位,空出的位置(也就是插入位置)插入新元素,在插入时我们采用从数组尾端开始移动,这是这段代码的巧妙所在
这时我们观察新加入的两个方法,似乎有重复,可以因为第二个方法明显包含了第一种方法,因此我们可以对代码进行优化,直接将第一种方法优化:
public void addLast(int e){ add(size,e); }
同理我们也可以添加新的方法,在所有元素前面添加一个新元素:
public void addFirst(int e){ add(0,e); }
获取索引位置的元素:
//get elements by given index public int get(int index){ if(index < 0 ||index >= size) throw new IllegalArgumentException("Get failed,the index must be a non negative number that smaller than the size"); return data[index]; }
根据索引替换元素:
//change the elements by elements public void set(int index,int e){ if(index < 0 ||index >= size) throw new IllegalArgumentException("Get failed,the index must be a non negative number that smaller than the size"); data[index] = e ; }
方便了解数组元素:
@Override public String toString(){ StringBuilder res = new StringBuilder(); res.append(String.format("Array: size = %d,capacity = %d\n", size, data.length)); res.append('['); for(int i = 0 ; i < size ; i++){ res.append(data[i]); if(i != size - 1) res.append(", "); } res.append(']'); return res.toString(); }
测试Array类:
public class Main { public static void main(String[] args){ Array arr = new Array(20); for(int i = 0 ; i < 10 ; i++){ arr.addLast(i); } System.out.println(arr); arr.add(1,12); System.out.println(arr); arr.addFirst(-1); System.out.println(arr); } }
测试结果:
Array: size = 10,capacity = 20 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] Array: size = 11,capacity = 20 [0, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9] Array: size = 12,capacity = 20 [-1, 0, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9]
综合到目前我们的Array类的代码:
Java版:
1 public class Array { 2 private int[] data; 3 private int size; 4 //constructor ,create Array with the input param "capacity" 5 public Array(int capacity){ 6 data = new int[capacity]; 7 size = 0; 8 } 9 //constructor without params,our default capacity is 10 10 public Array(){ 11 this(10); 12 } 13 //get the capacity of this array 14 public int getCapacity(){ 15 return data.length; 16 } 17 //get the amount of elements of this array 18 public int getSize(){ 19 return size; 20 } 21 //judge whether this array is empty or not 22 public boolean isEmpty(){ 23 return size==0; 24 } 25 //enrich our Array----part 2 26 public void addLast(int e){ 27 // //judge whether can we add a new element 28 // if(size == data.length){ 29 // throw new IllegalArgumentException("AddLust failed,the array is full"); 30 // } 31 // //asign the new last element 32 // data[size] = e ; 33 // //maintain the size of the array 34 // size ++ ; 35 add(size,e); 36 } 37 public void addFirst(int e){ 38 add(0,e); 39 } 40 public void add(int index ,int e){ 41 if(size ==data.length) 42 throw new IllegalArgumentException("AddLust failed,the array is full"); 43 44 if(index < 0 || index > size) { 45 throw new IllegalArgumentException("Add failed. the range of index must within [0,size)"); 46 } 47 for(int i = size - 1 ;i >= index ; i--){//move elements from the tail of the array 48 data[i+1] = data[i]; 49 } 50 data[index] = e ; 51 size++; 52 53 } 54 //get elements by given index 55 public int get(int index){ 56 if(index < 0 ||index >= size) 57 throw new IllegalArgumentException("Get failed,the index must be a non negative number that smaller than the size"); 58 return data[index]; 59 } 60 61 //change the elements by elements 62 public void set(int index,int e){ 63 if(index < 0 ||index >= size) 64 throw new IllegalArgumentException("Get failed,the index must be a non negative number that smaller than the size"); 65 data[index] = e ; 66 } 67 @Override 68 public String toString(){ 69 StringBuilder res = new StringBuilder(); 70 res.append(String.format("Array: size = %d,capacity = %d\n", size, data.length)); 71 res.append('['); 72 for(int i = 0 ; i < size ; i++){ 73 res.append(data[i]); 74 if(i != size - 1) 75 res.append(", "); 76 } 77 res.append(']'); 78 return res.toString(); 79 } 80 81 82 }
C++版Array类:
1 #include <iostream> 2 #include <cassert> 3 4 class Array { 5 private: 6 int *data; 7 int size; 8 int capacity; 9 10 public: 11 Array(int capacity) { 12 data = new int[capacity]; 13 size = 0; 14 this->capacity = capacity; 15 } 16 17 Array() { 18 data = new int[10]; 19 size = 0; 20 capacity = 10; 21 } 22 23 int getCapacity() { 24 return capacity; 25 } 26 27 int getSize() { 28 return size; 29 } 30 31 bool isEmpty() { 32 return size == 0; 33 } 34 35 void add(int index, int e) { 36 assert(size < capacity && index >= 0 && index <= size); 37 for (int i = size - 1; i >= index; --i) { 38 data[i + 1] = data[i]; 39 } 40 data[index] = e; 41 size++; 42 } 43 44 void addFirst(int e) { 45 add(0, e); 46 } 47 48 void addLast(int e) { 49 add(size, e); 50 } 51 52 int get(int index) { 53 assert(index >= 0 && index < size); 54 return data[index]; 55 } 56 57 void set(int index, int e) { 58 assert(index >= 0 && index < size); 59 data[index] = e; 60 } 61 62 63 void print() { 64 std::cout << "Array: size = " << size << ", capacity = " << getCapacity() << std::endl; 65 toPrint(); 66 std::cout << std::endl; 67 } 68 69 void toPrint() { 70 std::cout << "["; 71 for (int i = 0; i < size; ++i) { 72 std::cout << data[i]; 73 if (i != size - 1) { 74 std::cout << ", "; 75 } 76 } 77 std::cout << "]"; 78 } 79 };
C++测试:
1 #include <iostream> 2 #include "Array.h" 3 4 int main() { 5 Array *array = new Array(20); 6 for (int i = 0; i < 10; ++i) { 7 array->addLast(i); 8 } 9 array->print(); 10 array->add(1, 100); 11 array->addFirst(-1); 12 array->print(); 13 return 0; 14 }
测试结果:
1 C:\Users\SAMSUNG\CLionProjects\DataStructrus\cmake-build-debug\DataStructrus.exe 2 Array: size = 10, capacity = 20 3 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 4 Array: size = 12, capacity = 20 5 [-1, 0, 100, 1, 2, 3, 4, 5, 6, 7, 8, 9] 6 7 Process finished with exit code 0

浙公网安备 33010602011771号