Solved

Cannot delete front in pascal

Posted on 1998-12-26
5
221 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
The business world is becoming increasingly integrated with tech. It’s not just for a select few anymore — but what about if you have a small business? It may be easier than you think to integrate technology into your small business, and it’s likely…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

809 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