C 雙向鍊錶實現

2022-01-11 16:41:44 字數 2894 閱讀 7593

using system;

using system.collections;

using system.collections.generic;

using system.linq;

using system.text;

using system.threading.tasks;

namespace csharplinkedlist

console.writeline(test.count);

test.print();

foreach (var i in data.where(d => d % 2 == 0))

console.writeline(test.count);

test.print();

foreach (var i in data.where(d => d % 3 == 0))

console.writeline(test.count);

test.print();

console.readkey();}}

/// /// 雙向鍊錶

///

///

public class csharplinkedlist: ienumerable

}//鍊錶第乙個成員

public csharplinkedlistnodefirst

}//鍊錶最後乙個成員

public csharplinkedlistnodelast

return null;}}

//建構函式什麼都不需要做

public csharplinkedlist()

public csharplinkedlistnodefind(t value)

}return next;

}//如果傳入的是null,遍歷鍊錶,返回第乙個值為null的節點

while (next.item != null)

}return next;

}return null;

}internal void insertnodetoemptylist(csharplinkedlistnodenode)

internal void insertnodebefore(csharplinkedlistnodenode, csharplinkedlistnodenewnode)

//在鍊錶開頭插入節點(插入到head節點之前)

public void addfirst(csharplinkedlistnodenode)

else

}//在鍊錶開頭插入值為value的節點(插入到head節點之前)

public void addfirst(t value)

//在鍊錶結尾插入節點

public void addlast(csharplinkedlistnodenode)

else

}//在鍊錶開頭插入值為value的節點(插入到head節點之前)

public void addlast(t value)

//在某個節點前面插入乙個值

public void addbefore(csharplinkedlistnodenode, csharplinkedlistnodenewnode)

}//在某個節點後面插入乙個值

public void addafter(csharplinkedlistnodenode, csharplinkedlistnodenewnode)

public void remove(csharplinkedlistnodenode)

else

}//乾掉node發出來的兩根指標,使它成為垃圾

node.invalidate();

count--;

}//傳入值的過載形式

public void remove(t value)

}public void print()

} while (current != first);

console.write("\n");

}public ienumeratorgetenumerator()

ienumerator ienumerable.getenumerator()

//實現csharplinkedlistenumerator,繼承ienumerator介面

public class csharplinkedlistenumerator : ienumerator

}object ienumerator.current

}//資料來源

private readonly csharplinkedlistlist;

//當前節點

private csharplinkedlistnodenode;

//當前位置

private int _position = -1;

//初始化

public csharplinkedlistenumerator(csharplinkedlistdata)

//訪問下乙個

public bool movenext()

current = node.item;

node = node.next;

return true;

}//不需要實現

public void reset()

//不需要**,c#知道該如何做

public void dispose()}}

/// /// 雙向鍊錶節點

///

///

public class csharplinkedlistnode

public csharplinkedlistnode(t value)

//令乙個節點成為垃圾

public void invalidate()

}}

雙向鍊錶(C實現)

list.h ifndef list h define list h typedef struct node node typedef struct list list initlist int insertnode list l,void data,int size int deletenode ...

雙向鍊錶(c 實現)

雙向鍊錶與單鏈表有許多的相似之處,但是也有不同之處。雙向鍊錶與單鏈表主要的不同在於 雙向鍊錶可以從兩個方向進行遍歷,但是單鏈表只能從頭節點遍歷到尾節點,不能從尾節點遍歷到頭節點,對於鍊錶中一些插入和刪除等操作,雙向鍊錶較單鏈表更為簡單些。所以,雙向鍊錶有其存在的特殊意義。下面是通過c 來實現雙向鍊錶...

雙向鍊錶 C 實現

雙向鍊錶在類中的實現 include include includeusing namespace std typedef int datatype struct node node pnext node ppre int data class list list const list l else...