线性表是具有相同特性的数据元素的一个有限序列,该序列中所含元素的个数叫线性表的长度。
线性表的特性:
线性表的基本运算(c++下):
下面将对上面的这些基本运算以Java来实现。
为了简便,这里存储的数据类型全都设为了int型。
package com.muzile.List.arrayList;
public class MyArrayList {
private int capacity;
private int size;
private int[] list;
/**
* 默认容量的顺序表
*/
public MyArrayList(){
capacity = 16;
list = new int[capacity];
size = 0;
}
/**
* 初始化容量的顺序表
* @param capacity
*/
public MyArrayList(int capacity){
this.capacity = capacity;
list = new int[capacity];
size = 0;
}
/**
* 获得顺序标的大小
* @return
*/
public int size(){
return this.size;
}
/**
* 向线性表中顺序添加一个元素
* @param value
* @return
*/
public boolean add(int value){
if(size==capacity){
return false;
}
list[size] = value;
size++;
return true;
}
/**
* 获得一个指定下标的元素
* @param index
* @return
*/
public int get(int index){
if(index>=size||index<0){
return -1;
}else{
return list[index];
}
}
/**
* 插入一个元素,其他元素向后移动
* @param location
* @param value
* @return
*/
public boolean insert(int location,int value){
if(location<0||location>size||size == capacity){
return false;
}
for(int i=size;i>location;i--){
list[i] = list[i-1];
}
list[location] = value;
size++;
return true;
}
/**
* 删除一个元素,其他元素向前移动
* @param location
* @return
*/
public int remove(int location){
int retVal;
if(location<0||location>=size){
return -1;
}
retVal = list[location];
for(int i = location;i<list.length-1;i++){
list[i]=list[i+1];
}
size--;
return retVal;
}
/**
* 判断顺序表是否为空
* @return
*/
public boolean isEmpty(){
return size==0;
}
/**
* 打印顺序表中的所有元素
*/
public String toString(){
String retStr = "[";
for(int i=0;i<size;i++){
if(i<size-1){
retStr+=list[i]+",";
}else {
retStr+=list[i];
}
}
return retStr+"]";
}
/**
* 测试
* @param args
*/
public static void main(String[] args){
MyArrayList list = new MyArrayList();
for(int i = 0 ;i<16;i++){
list.add(i);
}
System.out.println(list);
System.out.println(list.isEmpty());
System.out.println(list.remove(3));
System.out.println(list.insert(3, 1));
System.out.println(list);
System.out.println(list.get(6));
}
}
运行结果:
[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
false
3
true
[0,1,2,1,4,5,6,7,8,9,10,11,12,13,14,15]
6
Node类:
package com.muzile.List.linkedList;
public class Node {
private Node next = null;
private int data;
public Node(int data){
this.data = data;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
}
链表类:其中循环查找、删除结点可以改进,将参数下标与size/2比较,分别从两端到中间扫描。
package com.muzile.List.linkedList;
public class MyLinkedList {
private int size;
private Node head = null;
public MyLinkedList(){
size = 0;
}
/**
* 头插法
* @param val
public void add(int val){
Node node = new Node(val);
if(head==null){
head = node;
}else{
node.setNext(head.getNext());
head.setNext(node);
}
size++;
}*/
/**
* 尾插法
* @param val
*/
public void add(int val){
Node node = new Node(val);
if(head==null){
head = node;
}else{
Node p = head;
while(p.getNext()!=null){
p = p.getNext();
}
p.setNext(node);
}
size++;
}
/**
* 插入一个元素
* @param location
* @param val
* @return
*/
public boolean insert(int location,int val){
if(location<0||location>size){
return false;
}
Node node = new Node(val);
if(head==null){
head = node;
}else{
Node sign = head;
Node pre = null;
for(int i=0;i<=location;i++){
if(sign.getNext()!=null){
pre = sign;
sign = sign.getNext();
}else{
sign.setNext(node);
size++;
return true;
}
}
pre.setNext(node);
node.setNext(sign);
}
size++;
return true;
}
/**
* 删除一个元素
* @param index
* @return
*/
public int remove(int index){
int retVal = -1;
if(index<0||index>=size){
return -1;
}
if(head!=null){
Node pre=head,p=head,sign=head;
for(int i=0;i<=index;i++){
pre = p;
p = sign;
sign = sign.getNext();
}
pre.setNext(p.getNext());
retVal = p.getData();
}
size--;
return retVal;
}
/**
* 获得一个元素
* @param index
* @return
*/
public int get(int index){
int retVal = -1;
if(index<0||index>=size){
return retVal;
}
if(head!=null){
Node pre=head,sign=head;
for(int i=0;i<=index;i++){
pre = sign;
sign = sign.getNext();
}
retVal = pre.getData();
}
return retVal;
}
/**
* 返回所有元素的字符串
*/
@Override
public String toString(){
Node p = head;
String retVal = "[";
while(p!=null){
if(p.getNext()!=null){
retVal += p.getData()+",";
}else{
retVal += p.getData()+"";
}
p = p.getNext();
}
return retVal+"]";
}
/**
* 获得链表长度
* @return
*/
public int size(){
return size;
}
/**
* 判空
* @return
*/
public boolean isEmpty(){
return size==0;
}
/**
* 测试
* @param args
*/
public static void main(String[] args){
MyLinkedList list = new MyLinkedList();
for(int i = 0 ; i < 10 ; i++){
list.add(i);
}
System.out.println(list);
System.out.println(list.get(10));
System.out.println(list.remove(9));
System.out.println(list);
System.out.println(list.insert(9, 2));
System.out.println(list);
}
}
结果:
[0,1,2,3,4,5,6,7,8,9]
-1
9
[0,1,2,3,4,5,6,7,8]
true
[0,1,2,3,4,5,6,7,8,2]