用單向鍊錶(頭插法)實現棧

2021-10-09 19:26:49 字數 575 閱讀 6186

用單向鍊錶實現棧,入棧時,新建乙個node結點,並把鍊錶的最後乙個node結點指向這個新建的node;出棧時,將最後乙個結點的值用乙個臨時變數儲存並輸出,然後將結點置為null。

package com.bhy.stacktest;

public

class

singlelinkedliststackdemo

}/**

* 使用頭插法實現

*/class

singlelinkedliststack

public

intpol()

node thisnode = curnode;

int temp =thisnode.value;

thisnode = null;

curnode = curnode.forward;

return temp;}}

class

node

}

執行結果如下:

鍊錶 頭插法實現逆序

usr bin env python coding utf 8 實現鍊錶的逆序 給定乙個帶頭節點的單鏈表,將其逆序。head 1 2 3 4 5 變為head 5 4 3 2 1 定義鍊錶的節點類 class listnode def init self,val 0,next none self.v...

單向鍊錶 尾插法

include include malloc函式標頭檔案 1設計節點 放置資料和指標 不同資料用結構體 結構體模板 struct node 關鍵字 結構體模板名稱 指標名字 struct node next next為結構體變數,如此定義會陷入死迴圈 定義乙個函式,初始化鍊錶,棧空間,函式呼叫後返回...

資料結構鍊錶 頭插法 尾插法 雙向鍊錶

我們最近學了資料結構鍊錶中的尾插法,頭插法,雙向鍊錶 鍊錶的步驟 1.申請乙個新的節點空間 2.給新的節點賦值資訊域 3.修改這個節點的指標域,將節點連線起來 尾插法 顧名思義就是從節點的尾部進行插入,首先申請乙個節點空間,給新的節點賦值資訊域,然後修改這個 節點的指標域,寫鍊錶首先要判斷頭節點是否...