Swift Program to Implement LinkedList


A Linked list is a data structure which is used to store and manage data. It is a sequence of nodes where each not contains two things: data and the reference to the next node in the given sequence. Using a linked list we can easily insert or remove elements from any position inside the list.

Linked lists are of two types −

  • Singly-linked list− It moves in only one direction because each node has a reference to the next node. But the next pointer of the last node points to NULL.

  • Doubly linked list− It moves in both directions because each node has the references of both the next and previous nodes.

Although Swift does not support any in-built linked list data structure still we can implement linkedList using class or structure. You can use any of the methods to implement the linked list according to your requirements. In this article, we are going to discuss how to create a singly linked list in Swift.

LinkedList supports the following operations &minus

  • Delete − It is used to delete a node from the linked list.

  • Insert − It is used to insert new elements in the linked list.

  • Display − It is used to print the linked list.

  • Search − It is used to find elements in the given linked list.

Example

In the following Swift program, we will implement a singly linked list using class. Here we will create a single node using a class which contains a value and the reference of the next node. Then we will create a LinkedList class which manages the linked list and provide methods to insert new elements and display the linked list. Then we will create an instance of the LinkedList class to add new elements to the LinkedList. Finally, display the output by calling the display method of the LinkedList class.

import Foundation
import Glibc

class node<X> {
   var element: X
   var next: node<X>?
    
   init(element: X) {
       self.element = element
       self.next = nil
   }
}

// Implementing linkedlist using class
class linkedList<X> 
{
  var head: node<X>?
    
  var First: node<X>? {
      return head
   }
    
   var Last: node<X>? {
      if var n = head {
          while let nextVal = n.next {
              n = nextVal
          }
          return n
      }
      return nil
  }
    
   // Adding new elements
   func appendElement(item: X) 
   {
      let nNode = node(element: item)
      if let endNode = Last {
          endNode.next = nNode
      } else {
          head = nNode
      }
   }
    
   // Displaying linked list
   func showList() 
   {
      var cNode = head
      while cNode != nil {
          print("\(cNode!.element) ->", terminator: "")
          cNode = cNode?.next
      }
      print("nil")
   }
}

let myLinkedList = linkedList<Int>()
myLinkedList.appendElement(item: 34)
myLinkedList.appendElement(item: 12)
myLinkedList.appendElement(item: 84)
myLinkedList.appendElement(item: 92)

print("LinkedList elements are:")
myLinkedList.showList()

Output

LinkedList elements are:
34 ->12 ->84 ->92 ->nil

Conclusion

So this is how we can implement a linked list. A linked list is useful for dynamic size, insertion, deletion, implementing graphs, stacks and queues, etc. The flexibility and efficiency of linked list data structure make it more versatile for managing data and implementing some specific algorithms very effectively. It is also used to implement a hash table.

Updated on: 13-Jun-2023

569 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements