C#實現連結串列
今天受一個帖子的刺激,再次複習起了資料結構與演算法,那本《資料結構與演算法(java版)》我還剩圖和高階排序的幾章沒看,工作上也沒我的事需要處理,就用C#重新寫了一遍連結串列結構,權作複習。
定義List介面:
實現單向連結串列:
實現雙向連結串列:
定義List介面:
public interface List
{
bool IsEmpty();
void Unshift(Object obj);
Object Shift();
void Push(Object obj);
Object Pop();
bool Contain(Object obj);
void Delete(Object obj);
void PrintAll();
Object getHead();
Object getTail();
void Clear();
}
{
bool IsEmpty();
void Unshift(Object obj);
Object Shift();
void Push(Object obj);
Object Pop();
bool Contain(Object obj);
void Delete(Object obj);
void PrintAll();
Object getHead();
Object getTail();
void Clear();
}
實現單向連結串列:
//單向連結串列
public class SList:List
{
private SNode head, tail;
public SList()
{
this.head = this.tail = null;
}
public bool IsEmpty()
{
return head == null;
}
public void Unshift(Object obj)
{
head = new SNode(obj, head);
if (tail == null)
tail = head;
}
public Object Shift()
{
if (head == null)
throw new NullReferenceException();
Object value = head.value;
if (head == tail)
head = tail = null;
else
head = head.next;
return value;
}
public void Push(Object obj)
{
if (!IsEmpty())
{
tail.next = new SNode(obj);
tail = tail.next;
}
else
head = tail = new SNode(obj);
}
public Object Pop()
{
if (head == null)
throw new NullReferenceException();
Object obj = tail.value;
if (head == tail)
head = tail = null;
else
{
//查詢前驅節點
for (SNode temp = head; temp.next != null && !temp.next.Equals(tail); temp = temp.next)
tail = temp;
tail.next = null;
}
return obj;
}
public void PrintAll()
{
string result = "";
for (SNode temp = head; temp != null; temp = temp.next)
result += " " + temp.value.ToString();
Console.WriteLine(result);
}
public bool Contain(Object obj)
{
if (head == null)
return false;
else
{
for (SNode temp = head; temp != null; temp = temp.next)
{
if (temp.value.Equals(obj))
return true;
}
}
return false;
}
public void Delete(Object obj)
{
if (!IsEmpty())
{
if (head == tail && head.value.Equals(obj))
head = tail = null;
else if (head.value.Equals(obj))
head = head.next;
else
{
//temp_prev為刪除值的前驅節點
for (SNode temp_prev = head, temp = head.next; temp != null; temp_prev = temp_prev.next, temp = temp.next)
{
if (temp.value.Equals(obj))
{
temp_prev.next = temp.next; //設定前驅節點的next為下個節點
if (temp == tail)
tail = temp_prev;
temp = null;
break;
}
}
}
}
}
public Object getHead()
{
return this.head.value;
}
public Object getTail()
{
return this.tail.value;
}
public void Clear()
{
do
{
Delete(head.value);
} while (!IsEmpty());
}
}
class SNode
{
public Object value;
public SNode next;
public SNode(Object value, SNode next)
{
this.value = value;
this.next = next;
}
public SNode(Object value)
{
this.value = value;
this.next = null;
}
}
public class SList:List
{
private SNode head, tail;
public SList()
{
this.head = this.tail = null;
}
public bool IsEmpty()
{
return head == null;
}
public void Unshift(Object obj)
{
head = new SNode(obj, head);
if (tail == null)
tail = head;
}
public Object Shift()
{
if (head == null)
throw new NullReferenceException();
Object value = head.value;
if (head == tail)
head = tail = null;
else
head = head.next;
return value;
}
public void Push(Object obj)
{
if (!IsEmpty())
{
tail.next = new SNode(obj);
tail = tail.next;
}
else
head = tail = new SNode(obj);
}
public Object Pop()
{
if (head == null)
throw new NullReferenceException();
Object obj = tail.value;
if (head == tail)
head = tail = null;
else
{
//查詢前驅節點
for (SNode temp = head; temp.next != null && !temp.next.Equals(tail); temp = temp.next)
tail = temp;
tail.next = null;
}
return obj;
}
public void PrintAll()
{
string result = "";
for (SNode temp = head; temp != null; temp = temp.next)
result += " " + temp.value.ToString();
Console.WriteLine(result);
}
public bool Contain(Object obj)
{
if (head == null)
return false;
else
{
for (SNode temp = head; temp != null; temp = temp.next)
{
if (temp.value.Equals(obj))
return true;
}
}
return false;
}
public void Delete(Object obj)
{
if (!IsEmpty())
{
if (head == tail && head.value.Equals(obj))
head = tail = null;
else if (head.value.Equals(obj))
head = head.next;
else
{
//temp_prev為刪除值的前驅節點
for (SNode temp_prev = head, temp = head.next; temp != null; temp_prev = temp_prev.next, temp = temp.next)
{
if (temp.value.Equals(obj))
{
temp_prev.next = temp.next; //設定前驅節點的next為下個節點
if (temp == tail)
tail = temp_prev;
temp = null;
break;
}
}
}
}
}
public Object getHead()
{
return this.head.value;
}
public Object getTail()
{
return this.tail.value;
}
public void Clear()
{
do
{
Delete(head.value);
} while (!IsEmpty());
}
}
class SNode
{
public Object value;
public SNode next;
public SNode(Object value, SNode next)
{
this.value = value;
this.next = next;
}
public SNode(Object value)
{
this.value = value;
this.next = null;
}
}
實現雙向連結串列:
//雙向連結串列
public class LinkedList:List
{
private LinkedNode head, tail;
public LinkedList()
{
head = tail = null;
}
public bool IsEmpty()
{
return head == null;
}
public void Unshift(Object obj)
{
if (IsEmpty())
head = tail = new LinkedNode(obj);
else
{
head = new LinkedNode(obj, null, head);
head.next.prev = head;
}
}
public Object Shift()
{
if (IsEmpty())
throw new NullReferenceException();
Object obj = head.value;
if (head == tail)
head = tail = null;
else
{
head = head.next;
head.prev = null;
}
return obj;
}
public void Push(Object obj)
{
if (IsEmpty())
head = tail = new LinkedNode(obj);
else
{
tail = new LinkedNode(obj, tail, null);
tail.prev.next = tail;
}
}
public Object Pop()
{
if (IsEmpty())
throw new NullReferenceException();
Object value = tail.value;
if (head == tail)
head = tail = null;
else
{
tail = tail.prev;
tail.next = null;
}
return value;
}
public bool Contain(Object obj)
{
if (IsEmpty())
return false;
else
{
for (LinkedNode temp = head; temp != null; temp = temp.next)
if (temp.value.Equals(obj))
return true;
}
return false;
}
public void Delete(Object obj)
{
if (IsEmpty())
throw new NullReferenceException();
if (head == tail)
head = tail = null;
else
{
for (LinkedNode temp = head; temp != null; temp = temp.next)
{
if (temp.value.Equals(obj))
{
if (temp.value.Equals(obj))
{
if (temp == tail)
{
tail = tail.prev;
tail.next = null;
break;
}
else if (temp == head)
{
head.next.prev = null;
head = head.next;
}
else
temp.prev.next = temp.next;
}
}
}
}
}
public void PrintAll()
{
string result = "";
for(LinkedNode temp=head;temp!=null;temp=temp.next)
result += " " + temp.value.ToString();
Console.WriteLine(result);
}
public Object getHead()
{
return this.head.value;
}
public Object getTail()
{
return this.tail.value;
}
public void Clear()
{
do
{
Delete(head.value);
} while (!IsEmpty());
}
}
class LinkedNode
{
public Object value;
public LinkedNode prev;
public LinkedNode next;
public LinkedNode(Object value, LinkedNode prev, LinkedNode next)
{
this.value = value;
this.next = next;
this.prev = prev;
}
public LinkedNode(Object value)
{
this.value = value;
}
}
public class LinkedList:List
{
private LinkedNode head, tail;
public LinkedList()
{
head = tail = null;
}
public bool IsEmpty()
{
return head == null;
}
public void Unshift(Object obj)
{
if (IsEmpty())
head = tail = new LinkedNode(obj);
else
{
head = new LinkedNode(obj, null, head);
head.next.prev = head;
}
}
public Object Shift()
{
if (IsEmpty())
throw new NullReferenceException();
Object obj = head.value;
if (head == tail)
head = tail = null;
else
{
head = head.next;
head.prev = null;
}
return obj;
}
public void Push(Object obj)
{
if (IsEmpty())
head = tail = new LinkedNode(obj);
else
{
tail = new LinkedNode(obj, tail, null);
tail.prev.next = tail;
}
}
public Object Pop()
{
if (IsEmpty())
throw new NullReferenceException();
Object value = tail.value;
if (head == tail)
head = tail = null;
else
{
tail = tail.prev;
tail.next = null;
}
return value;
}
public bool Contain(Object obj)
{
if (IsEmpty())
return false;
else
{
for (LinkedNode temp = head; temp != null; temp = temp.next)
if (temp.value.Equals(obj))
return true;
}
return false;
}
public void Delete(Object obj)
{
if (IsEmpty())
throw new NullReferenceException();
if (head == tail)
head = tail = null;
else
{
for (LinkedNode temp = head; temp != null; temp = temp.next)
{
if (temp.value.Equals(obj))
{
if (temp.value.Equals(obj))
{
if (temp == tail)
{
tail = tail.prev;
tail.next = null;
break;
}
else if (temp == head)
{
head.next.prev = null;
head = head.next;
}
else
temp.prev.next = temp.next;
}
}
}
}
}
public void PrintAll()
{
string result = "";
for(LinkedNode temp=head;temp!=null;temp=temp.next)
result += " " + temp.value.ToString();
Console.WriteLine(result);
}
public Object getHead()
{
return this.head.value;
}
public Object getTail()
{
return this.tail.value;
}
public void Clear()
{
do
{
Delete(head.value);
} while (!IsEmpty());
}
}
class LinkedNode
{
public Object value;
public LinkedNode prev;
public LinkedNode next;
public LinkedNode(Object value, LinkedNode prev, LinkedNode next)
{
this.value = value;
this.next = next;
this.prev = prev;
}
public LinkedNode(Object value)
{
this.value = value;
}
}
相關文章
- 連結串列-單連結串列實現
- 單連結串列實現
- 連結串列以及golang介入式連結串列的實現Golang
- Linux核心連結串列-通用連結串列的實現Linux
- C#集合----連結串列C#
- FreeRTOS連結串列實現
- 實現雙向連結串列
- 【資料結構】連結串列(單連結串列實現+詳解+原碼)資料結構
- java實現連結串列反轉Java
- go 實現單向連結串列Go
- Go實現雙向連結串列Go
- Python實現單連結串列Python
- TypeScript 實現連結串列反轉TypeScript
- golang 實現連結串列爽不爽?Golang
- java實現雙向連結串列Java
- C語言實現連結串列C語言
- c++實現單連結串列C++
- pta重排連結串列(一個很清晰的實現,完全模擬連結串列的實現)
- PHP 使用連結串列實現對映PHP
- 連結串列找環(python實現)Python
- Java實現連結串列帶註釋Java
- python3實現連結串列Python
- Java雙向連結串列的實現Java
- JAVA基礎:語言中連結串列和雙向連結串列的實現(轉)Java
- js實現資料結構--單連結串列JS資料結構
- 024 透過連結串列學Rust之非安全方式實現連結串列2Rust
- 024 通過連結串列學Rust之非安全方式實現連結串列2Rust
- 用連結串列實現佇列的功能佇列
- 單連結串列實現增刪改查
- C++實現通用雙向連結串列C++
- 模擬STL連結串列類的實現
- 雙向連結串列的功能實現(初版
- 單向迴圈連結串列的實現
- C#資料結構-靜態連結串列C#資料結構
- php實現基本資料結構之連結串列PHP資料結構
- LRU快取-實現雜湊連結串列結合快取
- 資料結構 - 單連結串列 C++ 實現資料結構C++
- Python資料結構——連結串列的實現Python資料結構