Solved

Interpreting codes

Posted on 2000-03-26
1
203 Views
Last Modified: 2010-04-16
Help! Help!, can you please help me I have been learning to program in Borland Pascal for some months now. I wrote the codes for a bookcase program using link list and pointers, I Sent my codes to Expert exchange about 5 weeks ago for help on writing two extra procedures for sorting and deleting books from the list.

The reply was excellent they both work but looking at it I do not really understand clearly how the codes work.
I would be eternally grateful if you could place a line by line comment on what is happening in the two procedures
as I am Practically teaching my self programming with a little help from a friend when he can find the time which is rarely. 100 point is yours see codes below

Many many thanks
--------------------------------------

procedure Display;{display deleted books}
 var
     ch  :char;
 begin
   writeln;
    writeln(space:10,'!!!!--  LIST OF DELETED BOOKS -!!!!: ');
     writeln(space:10,'**************************************');
     if delhead=nil then writeln(space:10,'No Books were deleted')
     else
     Begin
          del:=delhead;
   repeat
   write(space:10,'NAME OF BOOK >: ', del^.Name);
   writeln;
   writeln;
   writeln(space:10,'AUTHOR       >: ', del^.Author);
   writeln;
   writeln(space:10,'PUBLISHER    >: ', del^.Publisher);
   writeln;
   writeln(space:10,'YEAR         >: ',  del^.Year);
   writeln;
   writeln(space:10,'PAGES        >: ', del^.pages);
   writeln;
   writeln(space:10,'SIZE         >: ', del^.size);
   writeln;
   writeln(space:10,'SALE PRICE   >: ', del^.SalePrice);
   writeln;
   writeln(space:10,'Brief Introduction: ', del^.briefdescription);
   writeln;
   write('Do you want to view next book? ');
   read(ch);
   readln;
   del:=del^.next;
   if del=nil then break;
   Until upcase(ch)='N';
   write('End of list of deleted books.');
   readln;
   ClrScr;
     end;
     writeln;
end;

------------------------------------
{Procedure for sorting books in ascending order}

procedure sort;
var node1,node2 : link;
    m,n : integer;
procedure swap(n1,n2 : link);
var temp: booknode;
begin

       temp:=n1^;

       n1^.Pages:=n2^.Pages;
       n1^.Year:=n2^.Year;
       n1^.Author:=n2^.Author;
       n1^.Publisher:=n2^.Publisher;
       n1^.Name:=n2^.Name;
       n1^.SalePrice:=n2^.SalePrice;
       n1^.Size:=n2^.Size;
       n1^.BriefDescription:=n2^.BriefDescription;

       n2^.Pages:=temp.Pages;
       n2^.Year:=temp.Year;
       n2^.Author:=temp.Author;
       n2^.Publisher:=temp.Publisher;
       n2^.Name:=temp.Name;
       n2^.SalePrice:=temp.SalePrice;
       n2^.Size:=temp.Size;
       n2^.BriefDescription:=temp.BriefDescription;

end;
begin
      writeln;
      write('Sorting the list taking yearofbook as primary key.');
      node1:=head;

      for m:=1 to count-1 do
      begin
           node2:=node1^.next;
           for n:=m+1 to count do
           begin
                if node1^.year > node2^.year then
                begin
                      swap(node1,node2);
                end;
                node2:=node2^.next;
                write('.');
           end;
           node1:=node1^.next;
      end;
      writeln;
      writeln('Sorting complete.');
      readln;
      clrscr;
end;


0
Comment
Question by:greenfly
1 Comment
 
LVL 1

Accepted Solution

by:
guyss earned 100 total points
ID: 2660453
procedure Display;{display deleted books}
 var
     ch  :char;
 begin
{ print list header }
   writeln;
    writeln(space:10,'!!!!--  LIST OF DELETED BOOKS -!!!!: ');
     writeln(space:10,'**************************************');
{ delhead points to the head of the list so we never change it !! }
{ if it equals nil the list is empty }
     if delhead=nil then writeln(space:10,'No Books were deleted')
     else
{ otherwise... }
     Begin
{ set del to delchange }
          del:=delhead;
   repeat
{ write book information }
{ del is a pointer so del^ is the structure it points to and del^.Name is the Name field in the structure del points to }
   write(space:10,'NAME OF BOOK >: ', del^.Name);
   writeln;
   writeln;
   writeln(space:10,'AUTHOR       >: ', del^.Author);
   writeln;
   writeln(space:10,'PUBLISHER    >: ', del^.Publisher);
   writeln;
   writeln(space:10,'YEAR         >: ',  del^.Year);
   writeln;
   writeln(space:10,'PAGES        >: ', del^.pages);
   writeln;
   writeln(space:10,'SIZE         >: ', del^.size);
   writeln;
   writeln(space:10,'SALE PRICE   >: ', del^.SalePrice);
   writeln;
   writeln(space:10,'Brief Introduction: ', del^.briefdescription);
   writeln;
   write('Do you want to view next book? ');
   read(ch);
   readln;
{ set del to the next field in the record del points to --> will move del
to the next record...
this is the way a linked list works. }
   del:=del^.next;
{ if the next field was nil, we have reached the end of the records ,
we'll exit the loop }
   if del=nil then break;
{ also exit if user pressed 'n' or 'N'}
   Until upcase(ch)='N';
{ print list footer }
   write('End of list of deleted books.');
{ wait for a character }
   readln;
{ clear screen }
   ClrScr;
     end;
     writeln;
end;

------------------------------------
{Procedure for sorting books in ascending order}

procedure sort;
var node1,node2 : link;
    m,n : integer;

{ swaps information between two books }
procedure swap(n1,n2 : link);
var temp: booknode;
begin
{ copy all information in the record n1 points to into temp, notice temp is not a pointer }
       temp:=n1^;

{ copy information from n2 into n1 field
by field. }
{ the reason we can't copy the entire record is that both are pointers and}
{ the result of n1:=n2 would be both pointers pointing to the same record...
but no copy operation takes place
}
       n1^.Pages:=n2^.Pages;
       n1^.Year:=n2^.Year;
       n1^.Author:=n2^.Author;
       n1^.Publisher:=n2^.Publisher;
       n1^.Name:=n2^.Name;
       n1^.SalePrice:=n2^.SalePrice;
       n1^.Size:=n2^.Size;
       n1^.BriefDescription:=n2^.BriefDescription;

{ copy from temp into n2 }
       n2^.Pages:=temp.Pages;
       n2^.Year:=temp.Year;
       n2^.Author:=temp.Author;
       n2^.Publisher:=temp.Publisher;
       n2^.Name:=temp.Name;
       n2^.SalePrice:=temp.SalePrice;
       n2^.Size:=temp.Size;
       n2^.BriefDescription:=temp.BriefDescription;

end;
{ start of sort procedure }
begin
      writeln;
      write('Sorting the list taking yearofbook as primary key.');
{ set node to the first record }
      node1:=head;
{ we are going to use a bubble-sort procedure here, just check every adjustant pair of books and swap if neccesary }
{ by doing ths enough times every book will end up in the right place }
      for m:=1 to count-1 do
      begin
{ set node2 to the record following the record node1 points to }
           node2:=node1^.next;
{ go through the entire list }
           for n:=m+1 to count do
           begin
{ check if we need to swap }
                if node1^.year > node2^.year then
                begin
{ swap them }
                      swap(node1,node2);
                end;
{ advance node2 }
                node2:=node2^.next;
{ write progress indicator }
                write('.');
           end;
{ advance node1 }
           node1:=node1^.next;
      end;
{ end of sort procedure }
{ write footer }
      writeln;
      writeln('Sorting complete.');
      readln;
      clrscr;
end;


{ hope this helps.. }
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When converting a physical machine to a virtual machine using VMware vCenter Converter Standalone or vCenter Converter Enterprise, if an adapter type is not selected during the initial customization the resulting virtual machine may contain an IDE d…
Note: This is the third blog post in a series on email clearinghouses (https://www.xmatters.com/alert-management/blog-email-has-failed-us?utm_campaign=70138000000ydLoAAI&utm_source=exex&utm_medium=article&utm_content=blog-post).   We’ve been talki…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

947 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now