Exercise: Stack and Queue implemented by a linked list.

Assuming an algorithmic linked list with the following behaviour:

addFirst(element data)

addLast(element data)

element removeFirst()

element removeLast()

boolean isEmpty()

  1. Write algorithms to implement a stack using the functions alone,
    • push (element data)
    • element pop ()
    • element peek() (Hint: get the data out then put it back in)

  2. Write algorithms to implement a Queue using the functions alone,
    • enqueue (element data) //add
    • element dequeue () //remove

© 2023  Vedesh Kungebeharry. All rights reserved. 

Leave a comment