Solved

Cannot delete front in pascal

Posted on 1998-12-26
5
214 Views
Last Modified: 2010-04-16
Hi,

I'm writing a DeQueues program by using PASCAL and I've problems in procedure in deletefront and delrear as well.
I'll mark as *** in the following statements which I cannot fully understand or finish.

Could you please advise me which statement was wrong ?


unit DequeADT;
{Provides type Deque, a sequence permitting
 insertions and deletions at either end.
 This is a Deque with fully circular doubly
 linked list and dummy head.}

interface

  type
         DequeElement = Char;
         DequePtr = ^DequeNode;
         DequeNode = record
                             val : DequeElement;
                             prev, next : DequePtr;
                     end;
         Deque = DequePtr;

  procedure MakeEmpty (var d {input/output} : Deque);
  {Make d = the empty deque. This procedure allocates
   the dummy head node and set the prev and next field
   pointing to the dummy head node itself.}

  procedure JoinAtFront (var d {input/output} : Deque;
                         e {input} : DequeElement);
  {Prepends e to (front of) d.}

  procedure JoinAtRear (var d {input/output} : Deque;
                         e {input} : DequeElement);
  {Appends e to (end of) d.}

  procedure DeleteFront (var d {input/output} : Deque;
                         var v {output} : DequeElement);
  {Requires d not to be empty; removes first element
   of d and stores in v.}

  procedure DeleteRear (var d {input/output} : Deque;
                         var v {output} : DequeElement);
  {Requires d not to be empty; removes last element
   of d and stores in v.}

  function Empty (d {input} : Deque) : Boolean;
  {Returns (d is empty). To test whether the next field
   of the dummy head points to the dummy head.}

  function Head (d {input} : Deque) : DequeElement;
  {Requires d not to be empty; returns first element
   of d.}

  procedure PrintDeque (d {input} : Deque);
  {Requires d not to be empty; print all the elements
   of d.}

implementation

  procedure MakeEmpty (var d {input/output} : Deque);
  begin {MakeEmpty}
         New(d);
         d^.next := d;
         d^.prev := d;
  end; {MakeEmpty}

  procedure JoinAtFront (var d {input/output} : Deque;
                         e {input} : DequeElement);
    var
         p : DequePtr;

  begin {JointAtFront}
        new(p);
        p^.val :=e;
        p^.next := d^.next;
        d^.next := p;
        p^.prev := d;
        p^.next^.prev := p
  end; {JointAtFront}

  procedure JoinAtRear (var d {input/output} : Deque;
                         e {input} : DequeElement);
    var
         p : DequePtr;

  begin {JointAtRear}
        new(p);
        p^.val := e;
        p^.prev := d^.prev;
        d^.prev := p;
        p^.next := d;
        p^.prev^.next := p
  end; {JointAtRear}

  procedure DeleteFront (var d {input/output} : Deque;
                         var v {output} : DequeElement);
   var
         p : DequePtr;

  begin {DeleteFront}
    if Empty(d) then
                WriteLn('Error - Can''t delete empty deque.')
    else
          begin
***              p := d^.prev;
***              v := p^.val;
***              if d^.next <> nil then
***              d^.next ^.prev := nil;
***              Dispose(p)
          end; {else}
  end; {DeleteFront}

  procedure DeleteRear (var d {input/output} : Deque;
                         var v {output} : DequeElement);
   var
         p : DequePtr;

  begin {DeleteRear}
    if Empty(d) then
                WriteLn('Error - Can''t delete empty deque.')
    else
          begin
***              p := d^.prev;
***              v := p^.val;
***              if d^.next <> nil then
***              d^.next ^.prev := nil;
***              Dispose(p)
          end; {else}
  end; {DeleteRear}

  function Empty (d {input} : Deque) : Boolean;
  begin {Empty}
         Empty := d = nil;
  end; {Empty}

  function Head (d {input} : Deque) : DequeElement;
  begin {Head}
    if Empty(d) then
                begin
                       WriteLn('Error - No head of empty deque.');
                       Head := '?'
                end
    else
          d^.next := nil
  end; {Head}

  procedure PrintDeque (d {input} : Deque);
   var
         p : DequePtr;

  begin {PrintDeque}
    if Empty(d) then
                WriteLn('Error - Can''t print empty deque.')
    else
          begin
                 p := d^.next;
                 Write('> ');
                 repeat
                         Write(p^.val);
                         p := p^.next;
                 until (p^.next = d);
                 WriteLn(p^.val);
          end; {else}
  end; {PrintDeque}

end. {DequeADT}


Thanks,

Zoe
0
Comment
Question by:zoe_chui
  • 3
5 Comments
 
LVL 5

Accepted Solution

by:
scrapdog earned 50 total points
ID: 1216717
procedure DeleteFront (var d {input/output} : Deque;
                         var v {output} : DequeElement);
   var
         p,temp :DequePtr;

  begin {DeleteFront}
    if Empty(d) then
                WriteLn('Error - Can''t delete empty deque.')
    else
          begin
                 temp := d^.next;
                 p := d^.prev;
                 v := d^.val;
                 Dispose(d);
                 d := p;
                 p^.next := temp;
                 temp^.prev := p;
          end; {else}
  end; {DeleteFront}


procedure DeleteRear (var d {input/output} : Deque;
                         var v {output} : DequeElement);
   var
         p, temp : DequePtr;

  begin {DeleteRear}
    if Empty(d) then
                WriteLn('Error - Can''t delete empty deque.')
    else
          begin
                 p := d^.prev;
                 v := p^.val;
                 temp := p^.prev;
                 temp^.next := p^.next;
                 temp^.next^.prev := temp;
                 Dispose(p)
          end; {else}
  end; {DeleteRear}

--------------------------------------------
I also found an error in your Head function.


function Head (d {input} : Deque) : DequeElement;
  begin {Head}
    if Empty(d) then
                begin
                       WriteLn('Error - No head of empty deque.');
                       Head := '?'
                end
    else
          Head := d^.val;
  end; {Head}

0
 
LVL 10

Expert Comment

by:viktornet
ID: 1216718
Hey doggy, u on-line??
0
 

Author Comment

by:zoe_chui
ID: 1216719
scrapdog,

Thanks for sugguestion.
Actually, this program is my assignment and there have some rules in doing this prgram. First, we cannot add and modify the VAR and we need to write this program in limited statements.

For the procedure deletfront, I've try to write it in 6 statements as follows.

p := d^.front;
v := p^.val;
if p^.next^<> nil then
p^.next^.prev := nil;
d^.front := p^.next;
Dispose(p)  

But the rules of this program do not allow 6 statements. It only allow 5 statements to write the procedure deletefront and delterear.

Do you have any suggestion for this ?
0
 
LVL 5

Expert Comment

by:scrapdog
ID: 1216720
p := d^.front;
v := p^.val;
if p^.next^<> nil then
p^.next^.prev := nil;
d^.front := p^.next;
Dispose(p)  


That IS five statements.  

"if p^.next^<> nil then p^.next^.prev := nil;"

is one statement.  In fact, you probably don't even need to check for nil.


0
 
LVL 5

Expert Comment

by:scrapdog
ID: 1216721
Note:  a "statement" is delimited by semicolons.  Anything between two semi-colons is ONE statement.
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

Suggested Solutions

Title # Comments Views Activity
using a list of objects, Delphi 3 597
Neural Networks with Delphi 3 1,533
Conversion ? 15 88
my activex application give System Error.  Code: 5.  Access is denied 16 113
Facing problems with you memory card? Cannot access your memory card? All stored data, images, videos are lost? If these are your questions...than this small article might help you out in retrieving your lost or inaccessible data.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

919 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

14 Experts available now in Live!

Get 1:1 Help Now