2 Replies - 5828 Views - Last Post: 11 June 2011 - 03:50 AM Rate Topic: -----

#1 ToastyToast  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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;

procedure AddToList(var list:node;data:string[50]);
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

AddToList(list,'c');
AddToList(list,'a');
AddToList(list,'b');
AddToList(list,'z');
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  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5770
  • View blog
  • Posts: 12,582
  • 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.
Was This Post Helpful? 1
  • +
  • -

#3 ToastyToast  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1