單向鍊錶的新增 刪除與遍歷

2021-05-23 21:33:07 字數 4524 閱讀 6136

unitunit1;

inte***ce

uses

windows, messages, sysutils, variants, classes, graphics, controls, forms,

dialogs, stdctrls;

type

tform1 =class(tform)

memo1: tmemo;

button1: tbutton;

button2: tbutton;

button3: tbutton;

button4: tbutton;

procedurebutton1click(sender: tobject);

procedurebutton2click(sender: tobject);

procedurebutton3click(sender: tobject);

procedurebutton4click(sender: tobject);

procedureformcreate(sender: tobject);

end;

var

form1: tform1;

implementation

type

pmyrec = ^myrec;

myrec =record

name:string[8

];

age : word;

link: pmyrec;

end;

var

currentnode, firstnode: pmyrec;

proceduretform1.formcreate(sender: tobject);

begin

new(firstnode);

firstnode.name := '李四'

;

firstnode.age  := 44

;

firstnode.link :=nil;

currentnode := firstnode;

new(firstnode);

firstnode.name := '張三'

;

firstnode.age  := 33

;

firstnode.link := currentnode;

currentnode := firstnode;

new(firstnode);

firstnode.name := '錢二'

;

firstnode.age  := 22

;

firstnode.link := currentnode;

currentnode := firstnode;

new(firstnode);

firstnode.name := '趙一'

;

firstnode.age  := 11

;

firstnode.link := currentnode;

currentnode := firstnode;

button1.caption := '遍歷鍊錶'

;

button2.caption := '插入節點'

;

button3.caption := '刪除節點'

;

button4.caption := '刪除鍊錶'

;

memo1.align := alleft;

memo1.scrollbars := ssvertical;

end;

proceduretform1.button1click(sender: tobject);

begin

memo1.clear;

currentnode := firstnode;

whileassigned(currentnode)do

begin

memo1.lines.add(currentnode.name + #9

+ inttostr(currentnode.age));

currentnode := currentnode.link;

end;

end;

proceduretform1.button2click(sender: tobject);

var

newnode: pmyrec;

begin

currentnode := firstnode.link;

new(newnode);

newnode.name := 'aaa'

;

newnode.age  := 100

;

newnode.link := currentnode.link;

currentnode.link := newnode;

button1.click;

end;

proceduretform1.button3click(sender: tobject);

begin

currentnode := firstnode.link;       

ifassigned(currentnode.link)then

begin

firstnode.link := currentnode.link;

dispose(currentnode);              

end;

button1.click;

end;

proceduretform1.button4click(sender: tobject);

begin

currentnode := firstnode;    

whileassigned(firstnode)do

begin

currentnode := firstnode;

firstnode := currentnode.link;

dispose(currentnode);

end;

button1.click;

end;

end.

單向鍊錶 建立 插入 刪除 遍歷

include include include using namespace std struct list create 新建鍊錶 struct list insert struct list head,struct list temp 插入 struct list deletes struct...

指標單向鍊錶的遍歷新增方法

struct listnode 對於這種單向結構的指標鍊錶,依次遍歷新增,然後返回頭節點。有兩種方法。設定乙個頭指標head 和移動指標h.listnode head new listnode 0 頭指標 listnode h head 移動指標 h next sumnode 給鍊錶該位置賦值 h ...

單向鍊錶的遍歷

鍊錶的遍歷和陣列類似,就是跑鍊錶 輸出單向鍊錶尾結點的值 include include define n 5 typedef struct nodeelemsn elemsn creatlink int a return h elemsn printlink elemsn h return p i...