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(data)

addLast(data)

removeFirst()

removeLast()

isEmpty()

Write algorithms for functions to this linked list such that a stack can be implemented,

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

  1. push(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()

© 2022  Vedesh Kungebeharry. All rights reserved. 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s