鍊錶相關操作

2021-07-11 08:53:21 字數 2557 閱讀 7538

#include #include using namespace std;

//鍊錶結構體

struct listnode

;//是否為空

bool isempty(listnode*list)

//position是否是最後乙個

bool islast(listnode*position, listnode*list)

//在鍊錶中找到某個元素

listnode* find(int value, listnode*list)

return pnode;

}//找到某乙個元素的前驅節點

listnode* findprevious(int value, listnode*list)

return pnode;

}//移除某個節點

void deletenode(int value, listnode*list)

listnode* ptobedeletednode=null;

listnode* pnode=list;

//pnode = findprevious(value, list);//找到之前的節點

while (pnode->m_pnext != null&&pnode->m_pnext->m_nvalue != value)

if (pnode->m_pnext!=null) //不是最後乙個

else

}//將乙個元素插入到ptobeinsertednode指示的節點之後,元素的值為value

void insertnode(int value, listnode*list, listnode*ptobeinsertednode)

listnode*pnewnode = new listnode();

if (pnewnode==null) //分配失敗

pnewnode->m_nvalue = value;

pnewnode->m_pnext = ptobeinsertednode->m_pnext;

ptobeinsertednode->m_pnext = pnewnode;

}//在末尾新增元素

void addtotail(listnode*list, int value)

pnewnode->m_nvalue = value;

pnewnode->m_pnext = null;

if (list==null) //鍊錶為空,那就等於頭節點

else

pnode->m_pnext = pnewnode;//最後乙個的下乙個節點指向新節點 }}

//刪除整個鍊錶

void deletelist(listnode*list)

listnode*pnode = list->m_pnext;

listnode*ptemp = null;

list->m_pnext = null; //斷開頭結點

while (pnode!=null) }

//列印鍊錶

void printlist(listnode*list)

listnode*pnode = list->m_pnext;

while (pnode!=null)

cout << endl;

}//逆序列印鍊錶

void printlistreversingly(listnode*list)

while (!nodes.empty())

cout << endl;

}//判斷乙個鍊錶是否有環

bool linklisthasloop(listnode*phead)

return false;

}//測試

int main()

cout << "列印鍊錶:" << endl;

printlist(list);

cout << "逆序列印鍊錶:" << endl;

printlistreversingly(list);

cout << "在末尾新增元素:10" << endl;

addtotail(list, 10);

printlist(list);

cout << "移除節點值為5的節點:" << endl;

deletenode(5, list);

printlist(list);

cout << "找到節點值為6的節點的前驅節點:" << endl;

listnode* pnode=findprevious(6, list);

cout << pnode->m_nvalue << endl;

cout << "刪除鍊錶:" << endl;

deletelist(list);

printlist(list);

cout << "鍊錶是否為空:" << endl;

鍊錶相關操作

class listnode 1.鍊錶反轉,遍歷原鍊錶,採用頭插法將數值插入新鍊錶 public listnode reverse listnode p return cur 2.兩個鍊錶相加,如 1 2 3加4 5 6等於5 7 9。思路 短的鍊錶高位用0補。public class soluti...

鍊錶相關操作

關於鍊錶的頭插法 尾插法 刪除節點 插入節點。include include typedef struct listlist,linklist linklist creat onhead linklist head,int x linklist creat ontail linklist head,...

java實現鍊錶相關操作

public class test 構造乙個鍊錶 return 煉表頭結點 private static node createlink int count else return node 輸出鍊錶 param head 煉表頭結點 private static void printnodelin...