Solved

Cannot delete front in pascal

Posted on 1998-12-26
5
210 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
Comment Utility
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
Comment Utility
Hey doggy, u on-line??
0
 

Author Comment

by:zoe_chui
Comment Utility
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
Comment Utility
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
Comment Utility
Note:  a "statement" is delimited by semicolons.  Anything between two semi-colons is ONE statement.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

HOW TO: Upload an ISO image to a VMware datastore for use with VMware vSphere Hypervisor 6.5 (ESXi 6.5) using the vSphere Host Client, and checking its MD5 checksum signature is correct.  It's a good idea to compare checksums, because many installat…
Find out what Office 365 Transport Rules are, how they work and their limitations managing Office 365 signatures.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

772 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

13 Experts available now in Live!

Get 1:1 Help Now