Stack And Queue Implementation with LinkedList in Swift

I believe the best way to learn a language is to implement data structures. Below is my attempt to implement stack and queue in swift with generics.

Here are the reason why I used LinkedList and Generics.

  1. Generics helps us to use the data-structure with different data types as it is not tied to one data type.
  2. LinkedList implementation saves memory as compared to using array for stack implementation.

Queue Implementation with Generics

We start by implementing class Node which has two properties value and next . Queue works on concept of first in first out (FIFO). Queue basically have two methods

1 Enqueue: This method add new nodes at the back of the list.

2. Dequeue: This method removes node from the beginning of the list.

class Node<T> {  var value:T  var next:Node?  init(value:T) {    self.value = value  }}class Queue<T> {  var tail:Node<T>?  var head:Node<T>?  var count:Int = 0  func dequeue () -> Node<T>? {    if let node = head {      head = head?.next      count -= 1      return node    }    return nil  }  func enqueue(value:T) {    let newNode = Node(value:value)    if let tailNode = tail {      tailNode.next = newNode      newNode.next = nil      tail = newNode    } else {      head = newNode      tail = newNode    }    count += 1  }}

Stack Implementation with generics

We start by implementing class Node which has two properties value and next . Stack works on concept of last in first out (LIFO). Stack basically have two methods

  1. Push: This method add new nodes at the back of the list.
  2. Pop: This method removes node from the back of the list.
class Node<T> {  var value:T  var next:Node?  init(value:T) {    self.value = value  }}class Stack<T> {  var head:Node<T>?  func push(value:T){    let node = Node<T>(value: value)    if let headNode = head {      node.next = headNode      head = node    }     else {      head = node    }  }  func pop() -> Node<T>? {    if let headNode = head {      let node = headNode      head = headNode.next      node.next = nil      return node    }    return nil  }  func peek () -> Node<T>? {    return head  }}

--

--

--

Find my Swift articles on https://www.shashankthakur.dev/

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Convenient way to use ViewBinding

Jetpack Compose Layouts

Build a Markdown Editor With SwiftUI in iOS 15

decorative: abstract pattern of colored lights

Why You Should Use Xcode Instruments in Your iOS Apps

iOS: How to Transition from Login Screen to Tab Bar Controller?

Announcing Flutter 1.22

SwiftUI Tricks: Part 1

How to do Swift Dependency Injection Property Based

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Shashank Thakur

Shashank Thakur

Find my Swift articles on https://www.shashankthakur.dev/

More from Medium

Collection of Data in Swift

How To Implement Cache LFU Algorithm With Swift

Sparse Arrays Swift HackerRank Solution

LeetCode #1 Two Sum — Swift solution