Stack Implemented by a Linked List

Assume that there exists an implementation for a linked list which contains the following state:

first // a reference to the first node in the list

and behavior:

addFirst(element data)

addLast(element data)

element removeFirst()

element removeLast()

boolean isEmpty()

Write algorithms which use the only the above linked list functions to implement a stack,

i.e write algorithms which use to above state and behavior to implement:

  1. push(element data)
  2. pop()
  3. peek()

(Ensure that you include appropriate return types where necessary)

Updates to this post:

2022 Sept 30 – removed data as a parameter to removeFirst and removeLast()

2023 Sept 28 – reworded the task from: “Write algorithms for functions to this linked list such that a stack can be implemented,” to “Write algorithms which use the only the above linked list functions to implement a stack,”

© 2022  Vedesh Kungebeharry. All rights reserved. 

Leave a comment