資料結構(java)-順序表/單鏈表
阿新 • • 發佈:2018-12-09
順序表
package com.tuln.datastract; import java.util.Arrays; /** * @Created with IntelliJ IDEA * @Description: * @Package: com.tuln.datastract * @User: FLy * @Date: 2018/11/21 * @Time: 19:04 */ class TestSqlist{ //順序表 private int [] elem; // private int usedsize; //有效資料個數 public TestSqlist(){ this(10); } public TestSqlist(int size){ this.elem=new int[size]; this.usedsize=0; } public boolean isFull(){ //判斷陣列是否裝滿 if(usedsize==elem.length){ return false; } return true; } public boolean Insert(int pos,int value){ //插入元素 if(isFull()){ //陣列擴容 this.elem=Arrays.copyOf(this.elem,this.elem.length); } if(pos>usedsize||pos<0){ return false; }else { for (int i = usedsize-1; i >=pos ; i--) { elem[i+1]=elem[i]; } elem[pos]=value; usedsize++; return true; } } public boolean isEmpty(){ if(usedsize==0){ return true; } return false; } public int search(int key){ //查詢陣列元素 if(isEmpty()){ return -1; } for (int i = 0; i <usedsize ; i++) { if(this.elem[i]==key){ return i; } } return -1; } /* *刪除關鍵字key 第一次開出現的key */ public boolean delete(int key){ int index=search(key); //呼叫查詢方法,找到角標 if(index<0){ return false; }else { for (int i = index; i< this.usedsize-1 ; i++) { this.elem[i]=this.elem[i+1]; } this.usedsize--; this.elem[usedsize]=0; } return true; } /* *得到pos位置的值 */ public int getpos(int pos){ if(pos<0||pos>this.usedsize||isEmpty()){ throw new UnsupportedOperationException("pos位置不合法或者順序表為空"); } show(elem[pos]); return this.elem[pos]; } public static void show(int tmp){ System.out.println(tmp); } } public class TestSqlistDemo { public static void main(String[] args) { } }
單鏈表
- 基本的操作:頭插 尾插
- 判斷一個單鏈表是否有環?環的入口點?環的長度?
- 判斷兩個單鏈表是否相交?相交求交點?
- 合併兩個有序的單鏈表為了一個單鏈表?
- 單鏈表的反轉?
- 單鏈表的逆置?
- O(1)的 時間
package com.tuln.datastract; import java.lang.String; /** * @Created with IntelliJ IDEA * @Description: 單鏈表 * @Package: com.tuln.datastract * @User: FLy * @Date: 2018/11/21 * @Time: 20:22 */ class TestLink{ class Entry{ //內部類 int data; //一般可定義成Object Entry next; public Entry(){ //頭結點 this.data=-1; this.next=null; } public Entry(int val){ this.data=val; this.next=null; } } private Entry head;//頭引用 public TestLink(){ this.head=new Entry(); } public TestLink(int val){ this.insertHead(val); } /* * 頭插法 */ public void insertHead(int val){ Entry cur=new Entry(val); cur.next=this.head.next; //先連結後面的連結串列, head.next=cur; //再用頭部去連結新建的列表 } /* *尾插法 * 找到尾巴 插入資料 */ public void insertTail(int val){ // 找到尾巴 Entry cur=this.head; //頭不動 賦值一個變數去判斷。cur為head後的第一個連結串列 while(cur.next!=null){ cur=cur.next; } Entry entry=new Entry(val); //插入資料 cur.next=entry; } /* *得到單鏈表的長度(資料節點的個數) */ public int getLength(){ int count=0; Entry cur=this.head.next; //同上 複製head變數去判斷 while(cur!=null){ count++; cur=cur.next; } return count; } /* *任意位置插入 */ public void insertPos(int val,int pos) throws Exception { /* if(pos<0||pos>getLength()){ throw new Exception("連結串列為空"); } int count=0; Entry cur=this.head.next; while(cur!=null){ if(count+1==pos){ Entry cur1=new Entry(val); cur1.next=cur.next; cur.next=cur1; } }*/ if(pos<0||pos>getLength()){ return; } Entry cur =this.head; for(int i=0;i<=pos-1;i++){ cur =cur.next; } Entry entry=new Entry(val); entry.next=cur.next; cur.next=entry; } /* * 刪除單鏈表當中所有值為val的節點 */ public void delete(int val){ Entry cur1=this.head; Entry cur2=cur1.next; //需要注意head後第一個元素就為val的情況。 while( cur2!=null ){ if(cur2.data==val){ cur1.next=cur2.next; cur2=cur1.next; }else { cur1=cur1.next; cur2=cur2.next; } } show(); } /* * 列印單鏈表所有的資料 */ public void show(){ Entry cur=this.head.next; while(cur !=null){ System.out.println(cur.data); cur=cur.next; } } } public class TestLinkDemo { public static void main(String[] args) throws Exception { TestLink testLink = new TestLink(); /*testLink.insertHead(10); testLink.insertHead(20);*/ for(int i = 0;i < 10;i++) { testLink.insertTail(i);// } testLink.show(); testLink.insertPos(999,5); testLink.show(); testLink.delete(1); } }