• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 228
  • Last Modified:

Cannot delete front in pascal

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
zoe_chui
Asked:
zoe_chui
  • 3
1 Solution
 
scrapdogCommented:
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
 
viktornetCommented:
Hey doggy, u on-line??
0
 
zoe_chuiAuthor Commented:
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
 
scrapdogCommented:
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
 
scrapdogCommented:
Note:  a "statement" is delimited by semicolons.  Anything between two semi-colons is ONE statement.
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now