线性表
最近在学习java数据结构有感觉很有意思,在此与大家分享一下自己的丝丝感悟。
一、线性表的特点
1、线性表是一种各元素间具有线性关系的线性结构。
2、对线性表的操作主要有,插入、获取元素值、查找、删除等操作。
二、线性表的存储结构。
所谓存储结构就是线性表中的元素是通过怎样的方式存储。
线性表的存储结构有顺序存储结构和链式存储结构
。
1、顺序存储结构:
用一组连续的内存单元依次存放线性表的数据元素。通过元素在内存中的物理存储次序表示元素间的逻辑 存储次序。因为使用顺序结构存储线性表,也称为顺序表。
2、链式存储结构:
线性表的链式存储结构,是在分散的存储单元存中储数据元素的值,数据之间是逻辑上相邻但物理位置不 一定是相邻的。因此必须使用附加的信息来表示数据元素之间的顺序关系,所以存储一个数据元素就至少都 要包含两部分。其中一部分用来保存数据叫做数据域,一部分用来存放指向下一个数据元素的地址叫做地址 域。
由数据域和地址域构成一个节点。一个节点表示一个数据元素,节点中的数据域存放数据,通过节点中 的地址域可以将各个节点连接起来,使得各数据元素在逻辑上是相邻的。
线性表的实现
/**
*
* 顺序表的实现
*
*/
public class SqList {
private int[] element;
private int size;
public SqList(int size) {
element=new int[size>10?size:10];
}
public SqList(){//默认构造函数,默认构造数组大小为10
this(10);
}
//判断是否为空
public boolean isEmpty(){
return this.size==0;
}
//获得顺序表中元素个数
public int length(){
return this.size;
}
//顺序表的插入操作
public boolean insert(int index,int x){
if(this.size==this.element.length){
int []temp=this.element;
this.element=new int[this.length()*2+1];
for(int i=0;i<this.length();i++){
this.element[i]=temp[i];
}
}
if(index<0||index>this.length()){
throw new IndexOutOfBoundsException(index+"");
}else{
for(int j=this.length();j>index;j--){
this.element[j]=this.element[j-1];
}
this.element[index]=x;
this.size++;
return true;
}
}
//追加元素
public boolean append(int x){
insert(this.size,x);
return true;
}
//获取元素
public int get(int index){
rangeCheck(index);
return this.element[index];
}
//范围检查
public void rangeCheck(int index){
if(index<0||index>this.length()){
throw new IndexOutOfBoundsException(index+"");
}
}
//删除元素
public int delete(int index){
rangeCheck(index);
int old=this.element[index];
for(int i=index;i<this.length();i++){
this.element[i]=this.element[i+1];
}
this.size--;
return old;
}
//删除最后一个元素
public int deleteLast(){
int old=this.element[this.length()-1];
delete(this.length()-1);
return old;
}
//遍历--打印所有元素
public void printAll(SqList sqlist){
for(int i=0;i<sqlist.length();i++){
System.out.print(sqlist.get(i)+"\t");
}
}
public void printAll(){
for(int i=0;i<this.length();i++){
System.out.print(this.element[i]+"\t");
}
}
public static void main(String[] args) {
SqList sqlist=new SqList();
sqlist.insert(0, 1);
sqlist.insert(1, 10);
sqlist.insert(2, 15);
sqlist.append(20);
// System.out.println(sqlist.get(0));
// System.out.println(sqlist.length());
System.out.println("*******************");
sqlist.deleteLast();
sqlist.printAll();
}
}
/**
* 带头节点的链表的实现
*
*
*/
public class SinglyLinkedList {
private Node head;
private int size;
public SinglyLinkedList() {
head=new Node(null,0,null);
this.head.nex=head;//构造空的循环链表
}
//判断是否为空表
public boolean isEmpty(){
return this.head.nex==head;
}
//链表元素个数
public int length(){
return this.size;
}
//插入操作
public boolean insert(int index,int x){
if(index>this.size){
index=this.size;
}
if(index<0){
index=0;
}
Node p=this.head;
if(p.nex==null){
Node q=new Node(p,x,null);
p.nex=q;
q.nex=this.head;
this.size++;
}
int i=0;
while(p.nex!=null&&i<index){
p=p.nex;
i++;
}
// Node q=p;
Node s=new Node(p,x,p.nex.nex);
p.nex=s;
s.nex=this.head;
this.size++;
return true;
}
//获取元素
public int get(int index){
if(index<0||index>this.size){
throw new IndexOutOfBoundsException(index+"");
}
Node p=this.head;
int i=0;
while(p.nex!=null&&i<index){
p=p.nex;
i++;
}
if(p.nex!=null){
return p.nex.data;
}
return -1;
}
//追加元素
public void append(int x){
insert(this.size,x);
}
//删除元素
public int delete(int index){
if(index<0||index>this.size){
throw new IndexOutOfBoundsException(index+"");
}
Node p=this.head;
int i=0;
while(p.nex!=null&&i<index+2){
p=p.nex;
i++;
// System.out.println("EE");
}
if(p.nex!=null){
Node q=p.pre;
p.pre=q.pre;
q.pre.nex=p;
this.size--;
return q.data;
}
return -1;
}
//得到第一个元素
public int getFirst(){
Node p=this.head;
if(p.nex!=null){
return p.nex.data;
}
return -1;
}
//得到最后一个元素
public int getLast(){
return get(this.size-1);
}
}
class Node{//节点类
public Node pre;
public int data;
public Node nex;
public Node(){
}
public Node(Node pre,int data,Node nex){
this.pre=pre;
this.data=data;
this.nex=nex;
}
}