線性表
最近在學習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;
}
}