using system;
using system.collections.generic;
using system.linq;
using system.text;
namespace doublelinkedlist
// 指向下乙個元素指標
public node next
// 資料,可以是任何型別
public int data
public int index
///
/// 建立乙個新的節點
///
public node(int data)
}// 頭指標
private node head = null;
// 尾指標
private node tail = null;
// 鍊錶長度
private int count = 0;
private int tailindex = 0;
///
/// 建立乙個空的鍊錶
///
public doublelinkedlist()
///
/// 獲得鍊錶長度
///
public int getcount()
public void myquicksort()
private void myqsort(int low, int high)
}private int mypartition(int low, int high)
setvi(h, t.index);
while (h != t && tmp.data >= h.data)
setvi(t, h.index);
}h.index = pivotindex;
return h.index;
}private int getelebyvi(int index)
return -1;
}private node getnobyvi(int index)
p = p.next;
}return null;
}void setvi(node node, int index)
p = p.next;}}
private int partitionascending(int low, int high)
this[low] = this[high];
while (low < high && this[low] <= pivotkey)
this[high] = this[low];
}this[low] = pivotkey;
return low;
}private void qsortascending(int low, int high)
}public void quicksortascending()
private int partitiondecending(int low, int high)
this[low] = this[high];
while (low < high && this[low] >= pivotkey)
this[high] = this[low];
}this[low] = pivotkey;
return low;
}private void qsortdecending(int low, int high)
}public void quicksortdecending()
///
/// 在尾部新增元素
///
public void add(int item)
// 鍊錶不空
else
// 將新建立的指標置空
newnode = null;
// 鍊錶長度加1
count++;
tailindex++;
}///
/// 利用索引取元素
///
/// 元素位置索引
/// index索引所對應的元素值
public int this[int index]
node newnode = null;
newnode = head;
int i = 0;
// 找到index所對應的元素位置
while (newnode.next != null && i < index)
return newnode.data;
}set
node newnode = null;
newnode = head;
int i = 0;
while (newnode.next != null && i < index)
newnode.data = value;}}
///
/// 向鍊錶中插入資料
///
public void insert(int item, int index)
// 索引超出範圍
if (index < 0 || index >= count)
node beforenode = null;
int i = 0;
// 如果在第乙個位置插入
if (0 == index)
beforenode = head;
while (beforenode.next != null && i < index - 1)
node toinsert = new node(item);
node tnode = beforenode.next;
toinsert.next = tnode;
toinsert.previous = beforenode;
beforenode.next = toinsert;
tnode.previous = toinsert;
count++;
tailindex++;
}///
/// 刪除指定值的元素
///
///
///
public bool remove(int item)
node p = head;
while (p != null)
// 刪除的是最後乙個節點
if (p == tail)
// 刪除的是中間節點
node previous = p.previous;
node next = p.next;
previous.next = next;
next.previous = previous;
p.next = null;
p.previous = null;
p = null;
count--;
tailindex--;
return true;
}p = p.next;
}return false;
}///
/// 刪除index所在位置的元素
///
/// 索引值
public void removeat(int index)
// 迴圈指標
node p = head;
// 刪除頭節點
if (0 == index)
// 刪除尾節點
if (getcount() - 1 == index)
p = head;
int i = 0;
while (p.next != null && i < index)
p.previous.next = p.next;
p.next.previous = p.previous;
p.next = null;
p.previous = null;
p = null;
count--;
tailindex--;
}///
/// 列印鍊錶
///
public void printelements()
node p = head;
while (p != null)
console.writeline();
}///
/// 清空鍊錶
///
public void clear()
tail = null;
count = 0;
} ///
/// 判斷鍊錶是否為空
///
public bool isempty()}}
C 泛型程式設計之雙向鍊錶
ifndef listex h define listex h function 鍊錶模板 param include using namespace std template class clist snode m phead,m ptail public function 建構函式 param ...
使用泛型類實現Node鍊錶
是msdn中的乙個經典例子。using system using system.collections.generic using system.linq using system.text namespace 使用泛型類實現node鏈 不實現genericlist中的ienumrator 則for...
用c 實現單向鍊錶和雙向鍊錶
鍊錶是一種非常基礎的資料結構,本身也比較靈活,突破了陣列在一開始就要確定長度的限制,能夠做到隨時使用隨時分配記憶體。同時還有新增,刪除,查詢等功能。總的來說,鍊錶是由幾個模組構成的。一,單向鍊錶 鍊錶基本元素 struct node 建立節點 node current nullptr node he...