/**
* 棧 先進後出
* 陣列來實現棧的基本操作
* @author fancy
* @date 2018-12-05 09:31
*/public class arraystack
/*** 壓入資料
* @author fancy
* @param value
*/public void push (int value)
array[++topindex] = value;
}/**
* 彈出棧頂資料/並且刪除資料
* @author fancy
*/public int pop () throws exception
if (topindex > size)
int i = array[topindex];
// 初始化頂部
array[topindex] = 0;
topindex --;
return i;
}/**
* 返回棧頂部資料 - 不刪除頂部資料
* @return
*/public int peek () throws exception
if (topindex > size)
return array[topindex];
}/**
* 判斷棧是否滿的
* @return
*/private boolean isfull ()
return false;
}/**
* 判斷棧是否是空的
* @return
*/private boolean isempty ()
return false;
}}
/**
* 棧 先進後出
* 鍊錶實現 棧的基本操作
* @author fancy
* @date 2018-12-05 11:02
*/public class linkedliststack
public int getsize()
public boolean isempty()
return false;
}/**
* 壓棧
* @author fancy
* @param value
*/public void push(int value)
/*** 彈棧
* @author fancy
* @return
*/public int pop()
public int peek()
}
/**
* 單項鍊表 - 組成乙個環形
* 鍊錶沒有下標
* @author fancy
* @date 2018-12-05 11:03
*/public class mylinkedlist
// 獲取鍊錶中的元素個數
public int getsize()
// 返回鍊錶是否為空
public boolean isempty()
// 在鍊錶的index(0-based)位置新增新的元素e
// 在鍊錶中不是乙個常用的操作,練習用:)
public void add(int index, int data)
node prev = currentnode;
for(int i = 0 ; i < index ; i ++)
prev.setnext(new node(data, prev.getnext()));;
size ++;
}// 獲得鍊錶的第index(0-based)個位置的元素
// 在鍊錶中不是乙個常用的操作,練習用:)
public int get(int index)
node cur = currentnode.getnext();
for(int i = 0 ; i < index ; i ++)
return cur.getdata();
}// 修改鍊錶的第index(0-based)個位置的元素為e
// 在鍊錶中不是乙個常用的操作,練習用:)
public void set(int index, int data)
node cur = currentnode.getnext();
for(int i = 0 ; i < index ; i ++)
cur.setdata(data);
size ++;
}// 查詢鍊錶中是否有元素e
public boolean contains(int value)
cur = cur.getnext();
}return false;
}// 從鍊錶中刪除index(0-based)位置的元素, 返回刪除的元素
// 在鍊錶中不是乙個常用的操作,練習用:)
public int remove(int index)
node prev = currentnode;
for(int i = 0 ; i < index ; i ++)
node retnode = prev.getnext();
prev.setnext(retnode.getnext());
retnode.setnext(null);
size --;
return retnode.getdata();
}}
/**
* 單向鍊錶節點
* @author fancy
* @date 2018-12-05 11:34
*/public class node
public node(int data)
public node(int data, node next)
public int getdata()
public void setdata(int data)
public node getnext()
public void setnext(node next)
}
資料結構與演算法02 之棧與佇列
棧的主要機制可以用陣列來實現,也可以用鍊錶來實現,下面用陣列來實現棧的基本操作 public class arraystack public void push long value a top value public long peek return a top public long pop ...
資料結構與演算法02 之棧與佇列
棧的主要機制可以用陣列來實現,也可以用鍊錶來實現,下面用陣列來實現棧的基本操作 public class arraystack public void push long value a top value public long peek return a top public long pop ...
資料結構 資料結構與演算法02
1 演算法設計的原則 設計演算法時,通常應考慮達到以下目標 1,正確性 2,可讀性 3,健壯性 4,高效率與低儲存量需求 1,正確性 規格說明 四個層次 a,程式中不含語法錯誤 b,程式對於幾組輸入資料能夠得出滿足要求的結果 c,程式對精心選擇的 典型 苛刻切帶有刁難性的幾組輸入資料能夠得出滿足要求...