Posts

Showing posts with the label Data Structure using Swift

Stack implementation with Linked List using Swift

Image
Stack is a linear data structure which follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). Let's Code : First make a simple Node class , with a data and  a pointer: class Node {     let nodeVal : Int     var next : Node ?     init (value : Int ) {         self . nodeVal = value     } } Now make the Stack Class, As we know Stack follows LIFO. We will implement  Push() and Pop() method within the class to Push and Pop elements. class Stack {     var top : Node ?     func push( _ value: Int ){         let oldTop = top         top = Node (value: value)         top ?. next = oldTop         print ( "Push Item to Stack : \ ( value )" )     }     func pop() ->...

Append and Traverse a Doubly Linked List using Swift - iOS

Image
Let's Code : OK, let's see the result first , Adding some random city names to linked list and printing the linked list : Let's create the node now : Node.swift import Foundation class Node: NSObject {     var next: Node ?     weak var previous: Node ?     var value : String ?     init (value : String ) {         self . value = value     } } Now implement linked list in another swift file DoublyLinkedList. swift  import UIKit class DoublyLinkedList: NSObject {     fileprivate var head : Node ?     private var tail : Node ?     override init () {         super . init ()     }     public var isEmpty : Bool {         return head == nil     }     public var first : Node ? {         ...