# Pascal linked list sorting problem

Page 1 of 1

## 2 Replies - 13566 Views - Last Post: 11 June 2011 - 03:50 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=235295&amp;s=9ff941b47fed808a6f6ca9585ed23da0&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 ToastyToast

• New D.I.C Head

Reputation: 0
• Posts: 2
• Joined: 11-June 11

# Pascal linked list sorting problem

Posted 11 June 2011 - 03:26 AM

Hello.
I'm trying to finish my linked list sorting procedure , but it's not working properly.
I need to sort the list in ascending alphabetical order.
Am i doing something wrong?

```program linked_lists;

const LIM = ';';

type node = ^nd;

nd = record
data:string[50];
next:node;
end;

mn = set of char;

var newNode:node;
begin
new(newNode);
newNode^.data:=data;
newNode^.next:=list;
list:=newNode;
end;

procedure PrintList(list:node);
var nnode:node;
begin
nnode:=list;
while nnode <> nil do begin
writeln(nnode^.data);
nnode:=nnode^.next;
end;
end;

procedure SortList(var list:node);
var  l1,l2:node;
temp:string[50];
o1,o2:integer;
begin
l1:=list;
l2:=list;
while l1<> nil do begin
while l2<>nil do begin
o1:=ord(l1^.data[1]);
o2:=ord(l2^.data[1]);
if o1<o2 then begin
temp:=l1^.data;
l1^.data:=l2^.data;
l2^.data:=temp;
exit;
end;
l2:=l2^.next;
end;
l1:=l1^.next;
end;
end;

function checkRepeating(st:string[50]):boolean;
var i,j:integer;
begin
checkRepeating:=false;
for i:=1 to length(st) do
for j:=1 to length(st) do
if i<>j then
if st[i]=st[j] then checkRepeating:=true;

end;

var list:node;
ald:mn;
f1:text;
temp:string;

begin

PrintList(list);
SortList(list);
writeln('=========================');
PrintList(list);

end.

```

This post has been edited by ToastyToast: 11 June 2011 - 03:26 AM

Is This A Good Question/Topic? 0

## Replies To: Pascal linked list sorting problem

### #2 baavgai

• Dreaming Coder

Reputation: 6979
• Posts: 14,598
• Joined: 16-October 07

## Re: Pascal linked list sorting problem

Posted 11 June 2011 - 03:38 AM

Two things jump out.

First, why on earth do you exit after you swap? How does this make even a little sense? Get rid of it.

Second, l2 isn't really properly initialized. You want to reset it on the inner loop.

Something like this might do it for you:
```l1:=list;
while l1<> nil do begin
l2:=l1^.next;
while l2<>nil do begin

```

Hope this helps.

### #3 ToastyToast

• New D.I.C Head

Reputation: 0
• Posts: 2
• Joined: 11-June 11

## Re: Pascal linked list sorting problem

Posted 11 June 2011 - 03:50 AM

Thank you! It works now
I don't know why i placed exit after swap , probably by an accident.