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
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.
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.