Playing with Data Structures in Swift

Abhishek Thapliyal
Mar 18, 2018 · 3 min read

We all have learned data structures and implemented in C/C++.
In this story, I am going to illustrate some of the basic Data Structures using Generics in swift. Let’s start with stack.

NOTE: This story is just an idea and focuses on fundamentals. Suggestions and improvements are welcome.

What is a Stack?

We all know it right ! Just for reference, here’s a simple illustration -

I’m going to define a class Stack which can push/pop any type of objects. Just for the convenience I am assuming that these objects will confirm to CustomStringConvertible protocol (So that i can print their descriptions)

Stack<T: CustomStringConvertible>: CustomStringConvertible

This is a simple class declaration that uses generic objects T

As shown above declare an array : private var array [T] = [].

Pop: This function will remove last element from the array according to standard stack operation.

Push: This function will add new element in the last.

Since stack is one sided so assume all operations are working from the last in backend.

isEmpty: If stack is empty then it will be true else false.

count: Returns the no. of elements in stack.

description: As we conform to CustomStringConvertible so we have to override description variable. In this I have reversed array to make it feel like stack so the back side will be visible like top of stack in console output.

Console Output:

Exercise:
1. Try out with numeric array 😝.
2. Try to make queue/dequeue 😅

Let’s move to link list 😄

Link List

In the single link list, just like in C/C++ we have to create structure of element called Node. A Node class contains Data and link i.e. address of next element (as shown code below).

Node Structure

In a single link list first node will be start/root node and last node next will be nil.
Create a class name

class LinkList<T>: CustomStringConvertible where T: Comparable, T: CustomStringConvertible

Q: Why T: Comparable is required here?
A:
In case of searching a particular node i need to compare the element. Hence for comparison an element in link list should conform Comparable protocol.

As shown above declare private node called startNode.

Note: Assuming ideal cases here you can manage empty/ nil cases

append: This function will add element at the end of list. If link list is empty so the first element that will be added, will be the start node.

insert: This function will add element in the beginning of the link list. If link list is empty so the first element that will be added, will be the start node.

insert (position): This will add node to a given position.

removeFirst: This function will remove node from beginning.

removeLast: This function will remove node at the end.

remove (position): This will remove node from a given position.

search: It will search node on the basis of data and return its position.

Exercise:
1. Try out with alphabetic Link List 😝.
2. Try to make doubly/circular link list😅

I hope you will get an idea how we can use generics in swift and implement basic data structure. I have added exercises as well and hoping that you will explore it.

If sounds good check Binary Search Tree in Swift 😄

Happy Coding 😃!!!

Nickelfox Tech

We love to share the knowledge :)

Abhishek Thapliyal

Written by

Mobile Application Developer | iOS + Swift | React-Native @NickelfoxStudio

Nickelfox Tech

We love to share the knowledge :)