用C 實現鍊錶的一些功能

2021-04-25 20:05:57 字數 2887 閱讀 7482

用c#實現鍊錶的一些功能 

public

class

node

public node(t data)

public

node

next = null; }

public

class

link

//////鍊錶的長度,遍歷整個鍊錶,直到鍊錶結尾

///

///鍊錶的長度

public

int count()

return count; }

//////

取鍊錶的第i個節點的值

///

///第i個節點

///第i個節點的值

public t getelem(int i)

//如果i大於0且小於鍊錶長度,則遍歷鍊錶直到第i個節點為止

while (k < i)

return p.data; }

//////在鍊錶的第i個位置上插入新的節點

///

///要插入節點的值

///鍊錶的第i個節點

public

void insert(t e, int i)

//如果在煉表頭插入,移動頭指標

if (i == 0)

//遍歷鍊錶直到第i-1個節點為止

while (k < i - 1)

//把新節點插入在第i個節點的位置

p.next = q.next;

q.next = p;

}

//////

刪除第i個節點

///

///鍊錶的第i個節點

public

void removeat(int i)

//如果刪除煉表頭,移動頭指標

if (i == 0)

//遍歷鍊錶直到第i-1個節點為止

while (k < i - 1)

//刪除第i個節點

p.next = p.next.next; }

//////在鍊錶尾加入乙個新的節點

///

///新的節點的值

public

void add(t e)

/*---------------------------------------

* 如果鍊錶不為空,則在鍊錶尾加入新的節點

----------------------------------------*/ //

從頭指標開始遍歷,直至鍊錶尾

q = head;

while (q.next != null)

//在鍊錶尾插入新節點

q.next = p; }

//////

查詢某個元素在鍊錶中第一次出現的位置

///

///要查詢的元素

///這個元素在鍊錶中第一次出現的位置的索引

public

int indexof(t e)

k++;

p = p.next; }

if (!p.data.equals(e))

//如果沒有找到,則返回-1

return k >= count() ? -1 : k; }

//////查詢某個元素在鍊錶中最後一次出現的位置

///

///要查詢的元素

///這個元素在鍊錶中最後一次出現的位置的索引

public

int lastindexof(t e)

k++;

p = p.next; }

if (p.data.equals(e))

return index; }

//////判斷鍊錶是否為空

///

///

public

bool empty()

//////清空鍊錶

///

public

void clear()

//////將鍊錶轉成陣列

///

///轉換後的陣列

public t toarray()

array[count() - 1] = p.data;

return array; }

//////

將乙個陣列加到鍊錶中

///

///要加入鍊錶的陣列

public

void addrange(t a)

} ///

///刪除鍊錶中值為某個元素的所有節點

///

///要刪除節點的值

public

void remove(t e)

//如果不是頭指標,則刪除該節點

node

p = head;

while (p.next.next != null)

p = p.next; }

if (p.next.data.equals(e))

} //////

將鍊錶中所有為某個值的節點替換為另乙個值

///

///被替換的值

///替換的值

public

void replace(t first, t second)

p = p.next; }

if (p.data.equals(first))

} ///

///鍊錶反轉

///

public

void reverse()

q = p;

q.next = newhead;

newhead = p;

head = newhead; }

用C 語言實現的一些功能總結

1 用enter鍵從乙個文字框到另乙個文字框的功能 在keydown事件中,if e.keycode keys.enter sendkeys.send private void form1 keydown object sender,system.windows.forms.keyeventargs...

用鍊錶實現佇列的功能

鍊錶不限定元素的長度,可以動態分配元素並新增,另外經常的增刪是鍊錶優於其他資料結構的特點 今天我們用鍊錶來實現乙個佇列.linklist.h include include include include include include define new type type malloc siz...

用鍊錶實現佇列的功能

鍊錶不限定元素的長度,可以動態分配元素並新增,另外經常的增刪是鍊錶優於其他資料結構的特點 今天我們用鍊錶來實現乙個佇列.linklist.h include include include include include include define new type type malloc siz...