資料結構和演算法 單向鍊錶 雙向鍊錶的演算法實現

2021-10-06 08:15:10 字數 2869 閱讀 6984

單向鍊錶演算法實現

#includeusing namespace std;

typedef int typedata;

//單向鍊錶資料結構

typedef struct onewaylistnode onewaylistnode, onewaylist; //結構相同方便區別節點和頭指標

//初始化單向鍊錶

bool initonewaylist(onewaylist*& list)

//單向鍊錶前插法

bool onewaylistpush_front(onewaylist*& list, typedata data)

//單向鍊錶後插法

bool onewaylistpush_back(onewaylist*& list, typedata data)

//單向鍊錶指定位置插入

bool onewaylistinsert(onewaylist*& list, int where, typedata data)

if (!tmp || count > where - 1 )return false;

onewaylistnode* node = new onewaylistnode;//建立新節點

node->data = data; //新節點的資料域為要插入的值

node->next = tmp->next; //新節點尾指標域指向當前找的位置的下乙個位置

tmp->next = node;

return true;

}//單向鍊錶的取值

bool onewaylistvalue(onewaylist*& list, int where, int& element)

if (!tmp || count > where)return false;

element = tmp->data;

return true;

}//單向鍊錶的遍歷

void onewaylistprint(onewaylist*& list)

//單向鍊錶刪除元素

bool onewaylistdelete(onewaylist*& list, int where)

if (!tmp->next || count > where - 1)return false;

onewaylist* deletenode = tmp->next;

tmp->next = deletenode->next;

delete deletenode;

return true;

}//單向鍊錶的清除

void onewaylistclear(onewaylist*& list)

}//測試**

int main(void)

雙向鍊錶演算法實現

#includeusing namespace std;

typedef int typedata;

typedef struct bothwaylistnode bothwaylist, bothwaylistnode;

//初始化雙向鍊錶

bool initbothwaylist(bothwaylist*& blist)

//雙向鍊錶前插法

bool bothwaylistpush_front(bothwaylist*& blist, typedata data)

else

return true;

}//雙向鍊錶後插法

bool bothwaylistpush_back(bothwaylist*& blist, typedata data)

//雙向鍊錶指定位置插入

bool bothwaylistinsert(bothwaylist*& blist, int where, typedata data)

if (!tmp || count != where)return false;

bothwaylistnode* node = new bothwaylistnode;

node->data = data;

node->next = tmp;

node->prev = tmp->prev;

tmp->prev->next = node;

tmp->prev = node;

return true;

}//雙向鍊錶的取值

bool bothwaylistvalue(bothwaylist*& blist, int where, int& element)

if (!tmp || count > where)return false;

element = tmp->data;

return true;

}//雙向鍊錶的遍歷

void bothwaylistprint(bothwaylist*& blist)

}//雙向鍊錶刪除元素

bool bothwaylistdelete(bothwaylist*& blist, int where)

if (!tmp) return false;

tmp->prev->next = tmp->next;

if (tmp->next)

delete tmp;

return true;

}//雙項鍊表的清除

void bothwaylistclear(bothwaylist*& blist)

}//測試**

int main(void)

資料結構 反轉單向鍊錶和雙向鍊錶

程式設計師 面試指南 左程雲 讀書筆記 第三章反轉單向鍊錶和雙向鍊錶 單向鍊錶 public class node public class returnlist public static node relist node head 逆序後的第乙個節點 node pre null node nex...

資料結構 二 單向鍊錶 雙向鍊錶

資料結構 一 資料結構基本概念 基於陣列實現線性表 資料結構 二 單向鍊錶 雙向鍊錶 雙鏈表 一 基本概念 單鏈表由乙個個節點組成 public class mysinglelistnode 二 介面 public inte ce mylist 三 功能實現public class mysingle...

單向鍊錶和雙向鍊錶

1.單向鍊錶 單向鍊錶只可向乙個方向遍歷。查詢乙個節點的時候需要從第乙個節點開始每次訪問下乙個節點,一直訪問到需要的位置。也可以提前把乙個節點的位置另外儲存起來,然後直接訪問。2.雙向鍊錶 可以從任何乙個節點訪問前乙個節點,也可以訪問後乙個節點,以至整個鍊錶。一般是在需要大批量的另外儲存資料在鍊錶中...