?
Solved

Cannot delete front in pascal

Posted on 1998-12-26
5
Medium Priority
?
225 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 5

Accepted Solution

by:
scrapdog earned 150 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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Introducing Priority Question, our latest feature.
Learn how to use the free Acronis True Image app to easily transfer data between iPhones and Android phones.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

770 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