Mike
Mike

Reputation: 772

Reversing the elements in a list ADT

It's been a while since I've visited SO. Anyway, let me cut to the chase. I have a homework assignment that asks to perform various tasks with a linked list, and a list abstract data type. I have everything done, except for the very last question which asks me to reverse the elements in a list ADT. I have done this for the linked list already, however I am stuck on the list ADT part. My TA had mentioned that it is easier to do with a list ADT than with a linked list, but I don't know where to start. Maybe I'm overthinking it. Would it be possible for anybody to point me in the right direction? Here is my pseudocode for reversing a linked list:

c = pointer to current node
p = pointer to previous node
n = pointer to next node
H = head pointer

c <- H
if (c.next  != null)
    n <- c.next
while (n.next != null) {
    p <- c
    c <- n
    n <- c.next
    c.next -> p
}
if (H.next != null)
    H.next -> n
if (n != null AND c != null)
    n.next -> c

Any and all help would be appreciated.

Upvotes: 0

Views: 1235

Answers (1)

Rogue
Rogue

Reputation: 11483

If by using List ADT they mean the abstract List object in java, then you may want to just look into using Collections.reverse()

List<String> myList = new LinkedList<String>();
Collections.reverse(myList);

If you are asking for something else for "ADT", then by all means let me know

Upvotes: 1

Related Questions