Inserting items onto a sorted linked list, popping items off the list, and destroying the list... http://www.awitness.org/delphi_pascal_tutorial/source/sorted_linked_list_insert_pop_dispose.html unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type link=^node; node=record C:integer; s:string; nextlink:link end; TForm1 = class(TForm) Memo1: TMemo; Edit1: TEdit; Edit2:TEdit; InsertButton: TButton; PopButton: TButton; DestroyButton: TButton; Procedure PopSortedStack(cpop:integer); procedure InsertButtonClick(Sender: TObject); procedure PopButtonClick(Sender: TObject); function SortedLinkedListInsert(cnew:integer; head:link):link; procedure FormCreate(Sender: TObject); procedure DestroyStack(ptr:link); procedure DestroyButtonClick(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; head, linkpoint:link; implementation {$R *.DFM} procedure TForm1.FormCreate(Sender: TObject); begin new(head); head^.C:=-1; head^.nextlink:=nil; end; Procedure TForm1.PopSortedStack(cpop:integer); var temp, temp2:link; begin temp:=head; if temp^.nextlink<>nil then begin while temp^.nextlink^.C<>cpop do temp:=temp^.nextlink; temp2:=temp^.nextlink^.nextlink; dispose(temp^.nextlink); temp^.nextlink:=temp2; end; {the following code is for visual testing of the code above} temp2:=head; Memo1.clear; while temp2<>nil do begin Memo1.Lines.Append(InttoStr(temp2^.C)); temp2:=temp2^.nextlink; end; end; procedure TForm1.DestroyStack(ptr:link); begin if ptr<>nil then begin if ptr^.nextlink<>nil then DestroyStack(ptr^.nextlink); dispose(ptr); end; end; procedure TForm1.InsertButtonClick(Sender: TObject); var temp:link; begin temp:=SortedLinkedListInsert(StrToInt(Edit1.Text), head); {temp is only needed if you have additional fields in the record to fill} temp^.s:=Edit2.Text; {enter the data into the fields of the record} {the following code is test code for the list insert routine} temp:=head; Memo1.Clear; While temp<>nil do begin Memo1.Lines.Append(IntToStr(temp^.C)); temp:=temp^.nextlink; end; end; procedure TForm1.PopButtonClick(Sender: TObject); begin PopSortedStack(StrToInt(Edit1.Text)); end; function TForm1.SortedLinkedListInsert(cnew:integer; head:link):link; var Previous:link; found:boolean; begin found:=false; Previous:=nil; repeat If cnew <= head^.C then found:=true; If (cnew > head^.C) then begin Previous:=Head; head:=head^.nextlink; end; until (found) or (head=nil); new(linkpoint); linkpoint^.C := cnew; Linkpoint^.nextlink:=Previous^.nextlink; Previous^.nextlink:=linkpoint; Result:=Linkpoint; end; {This code rejects duplicate entries on the linked list function TForm1.SortedLinkedListInsert(cnew:integer; head:link):link; var Previous:link; found, dupe:boolean; begin found:=false; dupe := false; Previous:=nil; result := nil; repeat If cnew < head^.C then found:=true; If cnew = head^.c then dupe := true; If (cnew > head^.C) then begin Previous:=Head; head:=head^.nextlink; end; until (found) or (head=nil) or (dupe); If not dupe then begin new(linkpoint); linkpoint^.C := cnew; Linkpoint^.nextlink:=Previous^.nextlink; Previous^.nextlink:=linkpoint; Result:=Linkpoint; end; { else MessageDlg('that was a dupe', mterror, [mbok],0); } {end; } procedure TForm1.DestroyButtonClick(Sender: TObject); var temp:link; begin DestroyStack(head^.nextlink); head^.nextlink:=nil; Memo1.Clear; temp:=head; While temp<>nil do begin Memo1.Lines.Append(IntToStr(temp^.C)); temp:=temp^.nextlink; end; end; end.