translating c++ code into pseudo code

hi,

i just want to check if my c++ code
1
2
3
4
5
6
7
8
9
10
11
12
void insertLast(Node* &f, int v)
{
        if (f==NULL)
                f = new Node(v, NULL);
        else
        {
                Node* p=f;
                while (p->next != NULL)
                        p = p->next;
                p->next = new Node(v, NULL);
                }
}


is equivalent to my pseudo code ?
1
2
3
4
5
6
7
8
9
10
11
void function insertLast(Node pointer n, integer v)
if n = NULL then
        n ← new Node(v, NULL)
else
        Node pointer p ← n
        while p.next ≠ NULL do
        p ← p.next
        repeat
        p.next ← new Node(v, NULL)
endif
end insertLast


and if not, then please help by telling me what i am doing wrong.
Note: this is my first time writing pseudo code
Last edited on
I would say that is pretty good -- though you are still stuck on some language specifics -- and your pseudocode has the common textbook failing of not being very structurally consistent.

The whole point of pseudo code is to make an algorithm that users of any (imperative) language can follow. Hence, get rid of the C++-specific stuff.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
routine: insertLast
arguments: n: reference to a node pointer, v: integer value
returns: nothing
begin
  if n is nil
    then
      n <-- allocate new node
      value of n <-- v
      next n <-- nil
    else
      p <-- n
      while next p is not nil
        p <-- next p
      next p <-- allocate new node
      p = p->next
      value of p <-- v
      next p <-- nil
end

This can be readily translated into any language.

C++
1
2
3
4
5
6
7
8
9
10
11
12
void insertLast( Node*& n, int v )
  {
  if (!n)
    n = new Node( v, NULL );
  else
    {
    Node* p = n;
    while (p->next)
      p = p->next;
    p->next = new Node( v, NULL );
    }
  }

Standard Pascal
1
2
3
4
5
6
7
8
9
10
11
12
procedure insertLast( var n: pNode; v: integer );
  var p: pNode;
  begin
  if n = nil
    then new( n, v, nil )
    else begin
         p := n;
         while p^.next <> nil
           do p := p^.next;
         new( p^.next, v, nil )
         end
  end;

My pseudo is very likely a bit long-winded. The variable names for the original code are bad -- f probably means 'first' but it is better just to say 'first' or 'head' or whatever, and 'value' or whatever.

Hope this helps.
thanks for your help, i really appreciate it. my main problem with pseudo code is that it is not very exact (syntax wise). i have looked at websites that have a general guideline for it. but none of them dealt with everything (like do i write % or modulos).

if anyone have a website that deals with the specifics of pseudo code then please post the link here, thanks.
Last edited on
That's just it -- there is not any exact way to write pseudocode. That is why I said your go at it was pretty good.

My only thoughts are that pseudocode should:
  - avoid language-specific structures
  - be structurally consistent

I would tend to write 'remainder' or 'mod' over '%', but that's me.
Thanks again for your help. I will keep your remarks in mind when i write pseudo code again.
Topic archived. No new replies allowed.