Q
鏈表帕斯卡爾
2
A
回答
4
我發現早在我的舊郵件,我們做了一個實現。代碼是「法語」,所以我不會去碰它,以免忘記某個地方會破壞彙編的功能:-)
首先,我們有一個「元素」類型,讓我們輕鬆改變什麼,我們想在清單上存儲:
unit U_ELEMENT;
interface
{ We store integers }
type
T_ELEMENT = Integer;
{ Reads an element }
procedure lireElement(out res:INTEGER; out code:WORD; out ch:STRING);
{ Write and element }
procedure ecrireElement(const t:T_ELEMENT);
implementation
procedure lireElement(out res:T_ELEMENT; out code:WORD; out ch:STRING);
begin
write('> ');readln(ch);
val(ch,res,code);
end;
procedure ecrireElement(const t:T_ELEMENT);
begin
write(t);
end;
end.
然後,實際列表模塊,其目的是在列表的開頭添加元素:
unit U_LISTE;
interface
uses U_ELEMENT;
const LISTEVIDE = NIL;
type
T_LISTE = ^T_CELLULE;
T_CELLULE = record
info: T_ELEMENT; { The stored information }
suivant: T_LISTE; { Pointer to the next element }
end;
{ Add an heading element }
function ajouteEnTete(e:T_ELEMENT;l:T_LISTE):T_LISTE;
{ returns the head of the list }
function tete(l:T_LISTE):T_ELEMENT;
{ returns the list, without the head }
function reste(l:T_LISTE):T_LISTE;
{ List empty? }
function estListeVide(l:T_LISTE):BOOLEAN;
{ Modify the header element }
procedure modifierTete(var l:T_LISTE;const e:T_ELEMENT);
{ Modify the list after the head }
procedure modifierReste(var l1:T_LISTE; const l2:T_LISTE);
implementation
function ajouteEnTete(e:T_ELEMENT;l:T_LISTE):T_LISTE;
var l1:T_LISTE;
begin
new(l1);
l1^.info := e;
l1^.suivant := l;
ajouteEnTete := l1;
end;
function tete(l:T_LISTE):T_ELEMENT;
begin
tete := l^.info;
end;
function reste(l:T_LISTE):T_LISTE;
begin
reste := l^.suivant;
end;
function estListeVide(l:T_LISTE):BOOLEAN;
begin
estListeVide := l=NIL;
end;
procedure modifierTete(var l:T_LISTE;const e:T_ELEMENT);
begin
l^.info := e;
end;
procedure modifierReste(var l1:T_LISTE; const l2:T_LISTE);
begin
l1^.suivant := l2;
end;
end.
和一個小的測試程序:
program testeliste;
uses U_ELEMENT,U_LISTE;
procedure afficherListe(const l:T_LISTE);
var l1: T_LISTE;
vide: BOOLEAN;
begin
write('(');
l1 := l;
vide := estListeVide(l1);
while not(vide) do
begin
ecrireElement(tete(l1));
l1 := reste(l1);
vide := estListeVide(l1);
if not(vide) then
write(',');
end;
write(')');
writeln;
end;
var res:T_ELEMENT;
code:WORD;
ch:STRING;
i:INTEGER;
l:T_LISTE;
Begin
l:=LISTEVIDE;
for i:=0 to 9 do
begin
lireElement(res,code,ch);
l := ajouteEnTete(res,l);
end;
afficherListe(l);
afficherListe(reste(l));
afficherListe(reste(reste(reste(l))));
afficherListe(ajouteEnTete(tete(l),l));
End.
正如我所說的,這是當我開始學習CS,所以它可能不適合:-),但我認爲這有助於語法和全球念頭讓老(很舊的)計劃。
1
好文章:http://www.learn-programming.za.net/programming_pascal_learn14.html
網站似乎下降。在這裏,你可以找到 - >Archived Version
相關問題
- 1. Exercice與帕斯卡爾
- 2. 算法smbPitchShift(帕斯卡爾)
- 3. 帕斯卡爾布爾返回值?
- 4. 德爾福/帕斯卡爾例
- 5. 檢查輸入字符 - 帕斯卡爾
- 6. 摘要功能帕斯卡爾
- 7. 長度入住帕斯卡爾
- 8. 帕斯卡爾:後一句話,如果
- 9. 帕斯卡爾變量不是堆疊
- 10. 對於循環帕斯卡爾腳本
- 11. 字符串數組在帕斯卡爾
- 12. 渦輪帕斯卡爾在家工作
- 13. LineInput功能在帕斯卡爾
- 14. 定義最大帕斯卡爾
- 15. 帕斯卡三角
- 16. 德爾福/帕斯卡爾解析字符串到一個ComboBox
- 17. 德爾福/帕斯卡爾字符串文字到C/C++
- 18. TEdit註冊輸入(德爾福/免費帕斯卡爾)
- 19. 帕斯卡記錄集
- 20. 帕斯卡到C#轉換
- 21. 帕斯卡三角SML
- 22. 幫助帕斯卡三角
- 23. 圖像處理帕斯卡
- 24. 帕斯卡**定義(指數)
- 25. 帕斯卡範圍溢出
- 26. 計算以帕斯卡
- 27. 約帕斯卡輸出
- 28. 帕斯卡計劃練習
- 29. 帕斯卡進展狀態
- 30. 帕斯卡長度+圓?
是的,我知道那篇文章。它是一個很好的,我喜歡所有的代碼在一個文件中,所以我可以修補它,但不幸的是,該頁面上的鏈接似乎已被打破。 – NumberFour 2010-10-09 08:52:47
這也是第一個谷歌命中「pascal鏈表」。 – asveikau 2010-10-09 08:52:54
+1 @NumberFour,代碼就是你所需要的。由於兩個原因,您無法找到更好的現成代碼:(1)這是一個非常基本的數據結構,在日常生活中不是很有用。 (2)任何*真實*實現將在所有內容上都有太多的「裝飾」代碼。學習鏈表是關於處理指針,分配內存,釋放內存,以及在出錯時獲取訪問衝突。這實際上並不涉及數據結構。 – 2010-10-09 19:17:19