單鏈表顧名思義就是每一個元素只有一個一個直接前驅和一個直接後驅,鏈表中的每一個元素稱爲節點,一個節點包括數據區和指針區,數據區即存儲數據的部分,指針區即下一個節點的存儲地址,操作鏈表主要就是操作節點的指針區,鏈表在內存中存儲不是連續的,也不是一次性分配和釋放的,用鏈表可以方便的插入和刪除元素。下面是一個用Delphi控制檯程序寫的單鏈表的增刪改查程序。
用Delphi新建一個控制檯應用程序,源碼如下:
program linklit;
{$APPTYPE CONSOLE}
uses
SysUtils;
type
MyP=^MyRecord;
MyRecord=record
data:Integer;
next:MyP;
end;
function Createlist():MyP; //創建帶頭指針的單鏈表
var
head1,curnode,newnode:MyP; //定義頭結點、當前節點、新生成節點
ch:Integer;
begin
New(head1);
head1.data:=0;
curnode:=head1; //當前指針指向頭指針
Writeln('input:');
readln(ch);
while ch<1000 do
begin
New(newnode); //生成新節點
newnode.data:=ch;
curnode.next:=newnode; //新節點插入表尾節點
curnode:=newnode; //指針移到指向新的節點
Readln(ch); //輸入下一個節點值
end;
Writeln('list has been created!');
curnode.next:=nil;
Result:=head1;
end;
function Searchlist(head1:MyP;x:Integer):MyP;
var
curnode:MyP;
index:Integer;
begin
index:=0;
if Assigned(head1.next) then //判斷是否爲nil
begin
curnode:=head1.next; //curnode指向第一個節點
while Assigned(curnode) do //遍歷查找指向值爲x的節點
begin
Inc(index);
if curnode.data=x then //找到返回當前節點
begin
Result:=curnode;
Break;
end
else
curnode:=curnode.next; //繼續下次遍歷
end;
if not Assigned(curnode) then
Writeln('data is not in the list!') //沒找到節點
else
begin
Writeln('data has been found!');
Writeln('the location is:'+inttostr(index));
end;
end
else
Writeln('the list is empty');
end;
procedure Insertlist(head1:MyP;apos,x:integer); //在apos節點之前插入元素x
var
curnode,insertnode:MyP;
j:Integer;
begin //只考慮apos大於1的情況
if Assigned(head1.next) then
begin
curnode:=head1.next;
j:=1;
while(Assigned(curnode) and (j<apos-1)) do //查找apos-1個節點,並將curnode指向其直接前驅
begin
curnode:=curnode.next;
Inc(j);
end;
if not Assigned(curnode) then
Writeln('error')
else
begin
New(insertnode);
insertnode.data:=x; //生成插入節點
if Assigned(curnode.next) then //判斷apos-1個節點是否有後繼節點
begin
insertnode.next:=curnode.next;
curnode.next:=insertnode;
end
else
begin
curnode.next:=insertnode;
insertnode.next:=nil;
end;
end;
end
else
Writeln('the list is empty');
end;
procedure Deletelist(head1:MyP;index:integer); //刪除單鏈表中第index個節點
var
curnode,delnode:MyP; //當前節點和要刪除的節點
curpos:Integer;
begin
try
if Assigned(head1.next) then
begin
curpos:=0;
curnode:=head1;
while (Assigned(curnode.next)) and (curpos<index-1) do //查找要刪除節點的前一個節點並將指針指向其直接前驅
begin
curnode:=curnode.next;
Inc(curpos);
end;
if not Assigned(curnode.next) then //不存在第index個節點
Writeln('error')
else
begin
delnode:=curnode.next;
if not Assigned(delnode.next) then //判斷刪除的節點是否有後繼節點
begin
Dispose(delnode);
curnode.next:=nil;
end
else
begin
curnode.next:=delnode.next;
Dispose(delnode);
end;
end;
end
else
Writeln('the list is empty');
except
on e:Exception do
Writeln(e.message);
end;
end;
procedure Viewlist(head1:MyP); //遍歷鏈表
var
p:MyP;
begin
Writeln('the list is:');
if Assigned(head1.next) then
begin
p:=head1.next;
while Assigned(p) do
begin
Write(Inttostr(p.data)+' ');
p:=p.next;
end;
Writeln;
end
else
Writeln('the list is empty');
end;
procedure Updatelist(head1:MyP;index,avalue:Integer);
var
curnode:MyP;
i:integer;
begin
if not Assigned(head1.next) then
writeln('the list is empty')
else
begin
i:=0;
curnode:=head1.next;
while Assigned(curnode) do
begin
Inc(i);
if i=index then Break;
curnode:=curnode.next;
end;
if not Assigned(curnode) then
Writeln(Format('input index %d is error',[index]))
else
curnode.data:=avalue;
end;
end;
//
var
head:MyP;
searchdata,delindex,insertindex,invalue,upIndex,upValue:Integer;
begin
Writeln('Create list!');
head:=Createlist;
Viewlist(head);
Writeln('searching operation!');
Writeln('input data:');
Readln(searchdata);
Searchlist(head,searchdata);
Writeln('deleting operation!');
writeln('input deleted position:');
Readln(delindex);
Deletelist(head,delindex);
Writeln('after deleting operation:');
Viewlist(head);
Writeln('inserting operation!');
writeln('input inserting position and value:');
Readln(insertindex,invalue);
Insertlist(head,insertindex,invalue);
Writeln('after inserting operation:');
Viewlist(head);
Writeln('updating operation!');
Writeln('input updating position and value:');
Readln(upIndex,upValue);
Updatelist(head,upIndex,upValue);
Writeln('after updating operation:');
Viewlist(head);
Readln;
end.
程序運行截圖如下: