In: Computer Science
Write a pseudocode function that interchanges two adjacent items of:
(a) singly linked lists
(b) doubly linked lists
if you can make it clean and short that be nice
I assume, the question is to swap all the adjacent terms in a list.
SINGLY LINKED LIST :
FUNCTION SWAP_ITEMS ( head )
DECLARE first , second , temp , new_head
first = head
new_head = first.next
WHILE ( TRUE )
second = first.next
temp = second.next
IF temp == NULL or temp.next == NULL
p.next = temp
break
END IF
p.next = temp.next
p = temp
END WHILE
RETURN new_head
END FUNCTION
=======================================================
OR
========================================================================
b)
Thank you, If you have any doubt ask in commnet section. If you like my effort please upvote.